Giter Site home page Giter Site logo

xjc-guava-plugin's Introduction

xjc-guava-plugin

Build Status Maven Central

Fell in love with Guavas Objects.toStringHelper(), .hashCode() and .equals()? Tired of writing StringBuilders for JAX-WS wsgen generated Beans? This XJC Compiler plugin comes to the rescue and creates yummie standards methods for your JAX-B/WS Beans - with a taste of Guava.

Profit!

Example

This plugin generates guava standard methods for toString, hashCode and equals:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "thunderbolt", propOrder = {
    "intensity"
})
public class Thunderbolt {

    protected Double intensity;

    public Double getIntensity() {
        return intensity;
    }

    public void setIntensity(Double value) {
        this.intensity = value;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("intensity", intensity).toString();
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(intensity);
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (other == null) {
            return false;
        }
        final Thunderbolt o = ((Thunderbolt) other);
        if (o == null) {
            return false;
        }
        return Objects.equal(intensity, o.intensity);
    }
}

Usage

In contract first scenarios webservice clients models are often generated with jaxws.wsgen or cxf-codegen-plugin

using jaxws-maven-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <wsdlFiles>
                            <wsdlFile>${basedir}/src/test/resources/test.wsdl</wsdlFile>
                        </wsdlFiles>
                        <args>
                            <arg>-B-Xguava</arg>
                        </args>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.github.danielwegener.xjc</groupId>
                    <artifactId>xjc-guava-plugin</artifactId>
                    <version>0.3.1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>
</dependencies>

using cxf-codegen-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>2.7.1</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                        <wsdlOptions>

                            <wsdlOption>
                                <extraargs>
                                    <extraarg>-xjc-Xguava</extraarg>
                                </extraargs>
                                <wsdl>${basedir}/src/test/resources/test.wsdl</wsdl>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.github.danielwegener.xjc</groupId>
                    <artifactId>xjc-guava-plugin</artifactId>
                    <version>0.3.1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>
</dependencies>

#jaxb2-maven-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <dependencies>
                <dependency>
                    <groupId>com.github.danielwegener.xjc</groupId>
                    <artifactId>xjc-guava-plugin</artifactId>
                    <version>0.3.1</version>
                </dependency>
                <dependency>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                    <version>2.11.0</version>
                </dependency>
            </dependencies>
            <configuration>
                <arguments>-Xguava</arguments>
            </configuration>
            <executions>
                <execution>
                    <id>generate-model</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
    </dependencies>
</build>

xjc-guava-plugin's People

Contributors

danielwegener avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xjc-guava-plugin's Issues

The plugin uses deprecated guava methods

The plugin uses the method Objects.toStringHelper which will be removed in June 2016 according to the Guava JavaDoc

It should probably be replaced with MoreObjects.toStringHelper

Also works with jaxb2-maven-plugin

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <dependencies>
                    <dependency>
                        <groupId>com.github.danielwegener.xjc</groupId>
                        <artifactId>xjc-guava-plugin</artifactId>
                        <version>0.3</version>
                    </dependency>
                    <dependency>
                        <groupId>xerces</groupId>
                        <artifactId>xercesImpl</artifactId>
                        <version>2.11.0</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <arguments>-Xguava</arguments>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-model</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

equals is wrong for byte[]

First of all thanks for the great plugin! Although there is the jaxb2-basics alternative, I prefer this one, because it doesn't require any additional runtime dependency (I use Guava most of the time therefore it's not really a new one for me) and the generated code is more readable.

Unfortunately there is a bug which causes me to switch back to the "jaxb2-basics" plugin which behaves correctly.

I have the following xsd field:
<element name="pdf" type="base64Binary"/>
which generates
protected byte[] pdf;
in the Java class

You are then using Objects.equal(this.pdf, o.pdf) for the comparison which results in false even if the arrays are equal.
I guess the proper way would be to compare arrays with Arrays.equals(this.pdf, o.pdf) instead, which returns the correct result.

When XML contains a field called 'other' then equals does not work.

Because the method is generated as equals(Object other), then when Objects.equal(other, o.other) is called then instead of comparing the two expected field it compares the whole object other with the field called other with in it.

This could be fix easily by changing the code to prefix this. before all object instance references for the current class - ie Objects.equal(this.other, o.other) instead of Objects.equal(this.other, o.other).

This should be as simple as changing the code so it uses JExpr._this().ref(superField) instead of superField as the first argument of Object.equal(...).
(likewise for thisField)

Private fields with accessible getters don't work

The problem can be reproduced with any xsd which contains a complexTypes with
<xs:anyAttribute namespace="##any" processContents="lax"/>
and there are other complexTypes which extend those.

JAXB generates the following code for the <xs:anyAttribute namespace="##any" processContents="lax"/> definition:

@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
...
public Map<QName, String> getOtherAttributes() {
    return otherAttributes;
}

The problem is that your plugin directly accesses the fields which only works for the top level class but not for the subclasses which cannot access the private field directly (for whatever reason JAXB is making this field private and not protected like the others)

The result is that hashcode+equals of subclasses include a non-accessible private field in their generated code which causes a compile error in the generated class.

Perhaps this can be fixed by always using the getters or by trying to find a getter if the field itself is not accessible (as a kind of fallback mechanism)

In my case I do not need this plugin for this specific xsd so I can live with the current behavior, but I still wanted to report the issue to let you (and other users) know it exists.

generated equals() contains dead code

I'm very happy with Guava plugin. The generated code is much cleaner than the cxf-xjc-ts plugin.

One issue:
The generated equals method contains dead code:
if (o == null) {
return false;
}

This is already checked with:
if (other == null)

Would be nice if you could remove the check. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.