Builder-Generation for elements with no scope uses wrong methods
Closed this issue · 2 comments
Generating builder classes for elements with no scope results in using object factory methods that does not exist.
Using this xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" attributeFormDefault="unqualified" id="response"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2.1">
<xsd:complexType name="WithNoScopeElement">
<xsd:all>
<xsd:element ref="NoScopeElement" minOccurs="0"/>
</xsd:all>
</xsd:complexType>
<xsd:element name="OtherWithNoScopeElement" substitutionGroup="NoScopeElement"/>
<xsd:element name="NoScopeElement" type="Name"/>
<xsd:simpleType name="Name">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:complexType name="WithScopeElement">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" nillable="true" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
to generate build classes results in generating a method
public WithNoScopeElement.Builder<_B> withNoScopeElement(final String noScopeElement) {
this.noScopeElement =
new ObjectFactory().createWithNoScopeElementNoScopeElement(noScopeElement);
return this;
}
The method createWithNoScopeElementNoScopeElement in ObjectFactory does not exist, the method createNoScopeElement must be used instead.
The method for generating the builder is generateSingularProperty in class BuilderGenerator. It creates the name of the ObjectFactory methode like this
final JInvocation val = JExpr._new(objectFactory).invoke("create" + this.definedClass.name() + propertyName).arg(param);
This works only for elements with a scope, since the xjc class ObjectFactoryGeneratorImpl for generating the object factory generates create-methods like this:
JMethod m = this.objectFactory.method(1, exposedElementType, "create" +
ei.getSqueezedName());
where ei is of type CElementInfo and the implementation of getSqueezedName looks like this:
public String getSqueezedName() {
if (this.squeezedName != null) {
return this.squeezedName;
} else {
StringBuilder b = new StringBuilder();
CClassInfo s = this.getScope();
if (s != null) {
b.append(s.getSqueezedName());
}
if (this.className != null) {
b.append(this.className);
} else {
b.append(this.model.getNameConverter().toClassName(this.tagName.getLocalPart()));
}
return b.toString();
}
}
The corresponding commit has been reverted in version 2.0.1