highsource / jaxb2-basics

Useful plugins and tools for JAXB2.
BSD 2-Clause "Simplified" License
109 stars 54 forks source link

incompatible types: org.jvnet.jaxb2_commons.lang.ToStringStrategy cannot be converted to org.jvnet.jaxb2_commons.lang.ToStringStrategy2 #112

Closed n1t4chi closed 4 years ago

n1t4chi commented 4 years ago

Hello,

I've found an issue with toString generation when using maven-jaxb2-plugin. Instead of generating JAXBToStringStrategy.INSTANCE2, it uses INSTANCE which is incompatible with ToStringStrategy2 as seen below:

    public String toString() {
        final ToStringStrategy2 strategy = JAXBToStringStrategy.INSTANCE;
        final StringBuilder buffer = new StringBuilder();
        append(null, buffer, strategy);
        return buffer.toString();
    }

Because of this, the maven build fails on compilation with example error below:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test: Compilation failure
[ERROR] /D:/test/jaxb2/target/generated-sources/xjc/xsd/file/File.java:[73,64] incompatible types: org.jvnet.jaxb2_commons.lang.ToStringStrategy cannot be converted to org.jvnet.jaxb2_commons.lang.ToStringStrategy2

For some reason, when I use same configuration with org.jvnet.jax-ws-commons.jaxws-maven-plugin to generate classes from WSDL, the correct INSTANCE2 is used. I also tried to explicitly give which class to use via: -XtoString-toStringBuilder=org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy or -XtoString-toStringStrategyClass=org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy but nothing changed

Is this some bug or just configuration problem? Could you help please?

POM and XSD file below: pom.xml

    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <id>generate-raml</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>./</schemaDirectory>
                            <extension>true</extension>
                            <args>
                                <arg>-XtoString</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.12.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

XSD [should be placed next to pom]:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:file="file.xsd"
  targetNamespace="file.xsd"
    elementFormDefault="qualified">
  <xsd:element name="file">
    <xsd:complexType>
      <xsd:attribute name="id" type="xsd:string" use="optional"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
rburgst commented 4 years ago

having the same problem, is there any solution?

rburgst commented 4 years ago

fixed it by upgrading basics to 1.11.1

        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>1.11.1</version>
        </dependency>
highsource commented 4 years ago

1.11.1 was published by mistake. Please try 0.11.1 or the latest version.

rburgst commented 4 years ago

the latest version is 0.12 and there I had the problem

marekmaciejewski commented 3 years ago

The issue still occurs on the latest 0.12.0 version and in advised 0.11.1 version the problem is simply ignored so you still end up with mismatched declarations and assignments within the generated classes as it was stated at the beginning of this thread:

    final EqualsStrategy2 strategy = JAXBEqualsStrategy.INSTANCE;
    final HashCodeStrategy2 strategy = JAXBHashCodeStrategy.INSTANCE;
    final ToStringStrategy2 strategy = JAXBToStringStrategy.INSTANCE;

But I was able to narrow the source of the problem - the dependency within the plugin. It seems that the issue happens when you add any dependency to the plugin - instead of a plugin section within the plugin.