mklemm / jaxb-rich-contract-plugin

Implements some JAXB plugins to generate advanced contract scenarios from XSD
MIT License
67 stars 33 forks source link

compiler errors with builder in combination with xs:date to java.util.Calendar conversion #46

Closed techteam-bln closed 6 months ago

techteam-bln commented 5 years ago

If you define an xs:date field in an xml schema and define global bindings with:

The generated builder code contains errors.
davidsedlar commented 3 years ago

Same here. Problem is that it treats Calendar fields the same as complex objects, and it tried to make separate fields for Calendar fields. It should treat Calendar as a normal standard Java type, same as String or Integer.

For example for a simple test class with three fields, one is a date with the above java.util.Calendar conversion. This is pretty common when using XJC.

XSD:

    <xsd:complexType name="TestFieldType">
        <xsd:sequence>
            <xsd:element name="testString" type="xsd:string"></xsd:element>
            <xsd:element name="testDate" type="xsd:date"></xsd:element>
            <xsd:element name="testNumber" type="xsd:int"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>

Generated Java:

public class TestFieldType implements Serializable
{
    private final static long serialVersionUID = 1L;

    @XmlElement(required = true)
    protected String testString;
    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(Adapter2 .class)
    @XmlSchemaType(name = "date")
    protected Calendar testDate;
    protected int testNumber;

   ...

    public<_B >void copyTo(final TestFieldType.Builder<_B> _other) {
        _other.testString = this.testString;
        _other.testDate = ((this.testDate == null)?null:this.testDate.newCopyBuilder(_other)); // ERROR  - no newCopyBuilder for testDate 
        _other.testNumber = this.testNumber;
    }

    ... 

    public static class Builder<_B >implements Buildable
    {

        protected final _B _parentBuilder;
        protected final TestFieldType _storedValue;
        private String testString;
        private Calendar.Builder<TestFieldType.Builder<_B>> testDate;  // ERROR  - no Builder class for testDate
        private int testNumber;

        ...

    }
}
mklemm commented 6 months ago

It is designed to apply the builder patterns only to instances of classes defined within the current XML schema domain, so this is actually strange. I'll have a look into this...

mklemm commented 6 months ago

Fixed in 4.1.0

davidsedlar commented 6 months ago

Thanks a lot!

mklemm commented 6 months ago

Version 4.1.0 has just been released. It may take a while to appear on maven central, but it should be available soon.