JClass scope=null;
if(ei.getScope()!=null)
scope = outline.getClazz(ei.getScope()).implClass;
JMethod m;
if(ei.isAbstract()) {
// TODO: see the "Abstract elements and mighty IXmlElement" e-mail
// that I sent to jaxb-tech
TODO.checkSpec();
}
{// collision check
CElementInfo existing = elementFactoryNames.put(ei.getSqueezedName(),ei);
if( existing!=null ) {
outline.getErrorReceiver().error(existing.getLocator(),
Messages.OBJECT_FACTORY_CONFLICT.format(ei.getSqueezedName()));
outline.getErrorReceiver().error(ei.getLocator(),
Messages.OBJECT_FACTORY_CONFLICT_RELATED.format());
return;
}
}
// no arg constructor
// [RESULT] if the element doesn't have its own class, something like:
//
// @XmlElementMapping(uri = "", name = "foo")
// public JAXBElement<Foo> createFoo( Foo value ) {
// return new JAXBElement<Foo>(
// new QName("","foo"),(Class)FooImpl.class,scope,(FooImpl)value);
// }
// NOTE: when we generate value classes Foo==FooImpl
//
// [RESULT] otherwise
//
// @XmlElementMapping(uri = "", name = "foo")
// public Foo createFoo( FooType value ) {
// return new Foo((FooTypeImpl)value);
// }
// NOTE: when we generate value classes FooType==FooTypeImpl
//
// to deal with
// new JAXBElement<List<String>>( ..., List.class, ... );
// we sometimes have to produce (Class)List.class instead of just List.class
m = objectFactory.method( JMod.PUBLIC, exposedElementType, "create" + ei.getSqueezedName() );
JVar $value = m.param(exposedType,"value");
JExpression declaredType;
if(implType.boxify().isParameterized() || !exposedType.equals(implType))
declaredType = JExpr.cast(classRef,implType.boxify().dotclass());
else
declaredType = implType.boxify().dotclass();
JExpression scopeClass = scope==null?JExpr._null():scope.dotclass();
// build up the return extpression
JInvocation exp = JExpr._new(exposedElementType);
if(!ei.hasClass()) {
exp.arg(getQNameInvocation(ei));
exp.arg(declaredType);
exp.arg(scopeClass);
}
if(implType==exposedType)
exp.arg($value);
else
exp.arg(JExpr.cast(implType,$value));
m.body()._return( exp );
m.javadoc()
.append("Create an instance of ")
.append(exposedElementType)
.append("}");
XmlElementDeclWriter xemw = m.annotate2(XmlElementDeclWriter.class);
xemw.namespace(namespaceURI).name(localPart);
if(scope!=null)
xemw.scope(scope);
if(ei.getSubstitutionHead()!=null) {
QName n = ei.getSubstitutionHead().getElementName();
xemw.substitutionHeadNamespace(n.getNamespaceURI());
xemw.substitutionHeadName(n.getLocalPart());
}
if(ei.getDefaultValue()!=null)
xemw.defaultValue(ei.getDefaultValue());
if(ei.getProperty().inlineBinaryData())
m.annotate(XmlInlineBinaryData.class);
// if the element is adapter, put that annotation on the factory method
outline.generateAdapterIfNecessary(ei.getProperty(),m);
}