in dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java [177:307]
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
if (!active) {
return true;
}
LOG.fine("Running default value plugin.");
for (ClassOutline co : outline.getClasses()) {
for (FieldOutline f : co.getDeclaredFields()) {
// Use XML schema object model to determine if field is mapped
// from an element (attributes default values are handled
// natively) and get its default value.
XmlString xmlDefaultValue = null;
XSType xsType = null;
boolean isElement = false;
boolean isRequiredAttr = true;
XSParticle particle = null;
if (f.getPropertyInfo().getSchemaComponent() instanceof XSParticle) {
particle = (XSParticle)f.getPropertyInfo().getSchemaComponent();
XSTerm term = particle.getTerm();
if (term.isElementDecl()) {
XSElementDecl element = particle.getTerm().asElementDecl();
xmlDefaultValue = element.getDefaultValue();
xsType = element.getType();
isElement = true;
}
} else if (f.getPropertyInfo().getSchemaComponent() instanceof XSAttributeUse) {
XSAttributeUse attributeUse = (XSAttributeUse)f.getPropertyInfo().getSchemaComponent();
XSAttributeDecl decl = attributeUse.getDecl();
xmlDefaultValue = decl.getDefaultValue();
xsType = decl.getType();
isRequiredAttr = attributeUse.isRequired();
}
if (xsType != null
&& xsType.isComplexType()
&& !isAbstract(outline, f)
&& ((complexTypes && containsDefaultValue(outline, f))
|| isElementRequired(particle))) {
String varName = f.getPropertyInfo().getName(false);
JFieldVar var = co.implClass.fields().get(varName);
final JType rawType = f.getRawType();
if (var != null && !KNOWN_NO_DV_CLASSES.contains(rawType.erasure().fullName())) {
if (rawType instanceof JClass) {
final JClass jclazz = (JClass) rawType;
if (!jclazz.isAbstract() && !jclazz.isInterface()) {
var.init(JExpr._new(rawType));
}
} else {
var.init(JExpr._new(rawType));
}
}
}
JExpression dvExpr = null;
if (null != xmlDefaultValue && null != xmlDefaultValue.value) {
dvExpr = getDefaultValueExpression(f, co, outline, xsType, isElement,
xmlDefaultValue, false);
}
if (null == dvExpr
&& !isElement
&& !isRequiredAttr
&& xsType != null && xsType.getOwnerSchema() != null) {
//attribute, may still be able to convert it, but need to do
//a bunch more checks and changes to setters and isSet and such
dvExpr =
getDefaultValueExpression(f, co, outline, xsType, false,
xmlDefaultValue, true);
if (dvExpr != null) {
updateSetter(co, f, co.implClass);
updateGetter(co, f, co.implClass, dvExpr, true);
} else {
JType type = f.getRawType();
String typeName = type.fullName();
if ("javax.xml.datatype.Duration".equals(typeName)) {
updateDurationGetter(co, f, co.implClass, xmlDefaultValue, outline);
}
}
} else if (null == dvExpr) {
JType type = f.getRawType();
String typeName = type.fullName();
if ("javax.xml.datatype.Duration".equals(typeName)) {
updateDurationGetter(co, f, co.implClass, xmlDefaultValue, outline);
}
} else {
updateGetter(co, f, co.implClass, dvExpr, false);
}
}
}
for (PackageOutline po :outline.getAllPackageContexts()) {
//also fixup some unecessary casts
JDefinedClass cls = po.objectFactoryGenerator().getObjectFactory();
for (JMethod m : cls.methods()) {
String tn = m.type().fullName();
if (tn.startsWith("jakarta.xml.bind.JAXBElement<java.util.List<")
|| tn.startsWith("jakarta.xml.bind.JAXBElement<byte[]>")) {
JBlock b = m.body();
for (Object o : b.getContents()) {
try {
Field f = o.getClass().getDeclaredField("expr");
f.setAccessible(true);
JInvocation ji = (JInvocation)f.get(o);
f = JInvocation.class.getDeclaredField("args");
f.setAccessible(true);
@SuppressWarnings("unchecked")
List<JExpression> args = (List<JExpression>)f.get(ji);
JExpression cast = args.get(args.size() - 1);
if ("JCast".equals(cast.getClass().getSimpleName())) {
f = cast.getClass().getDeclaredField("object");
f.setAccessible(true);
JExpression exp = (JExpression)f.get(cast);
args.remove(args.size() - 1);
args.add(exp);
}
} catch (Throwable t) {
//ignore
}
}
}
}
}
return true;
}