diff --git a/pom.xml b/pom.xml index 9b99286..88769df 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 com.github.danielwegener.xjc xjc-guava-plugin @@ -16,13 +17,11 @@ Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 - - - - 1.6 + + 1.7 UTF-8 UTF-8 @@ -40,8 +39,8 @@ scm:git:git@github.com:danielwegener/xjc-guava-plugin.git scm:git:git@github.com:danielwegener/xjc-guava-plugin.git git@github.com:danielwegener/xjc-guava-plugin.git - HEAD - + HEAD + @@ -52,18 +51,17 @@ - com.sun.xml.bind jaxb-xjc - 2.0 + 2.2.11 provided com.google.guava guava - 13.0 + 20.0 junit @@ -83,7 +81,6 @@ selftest - org.jvnet.jax-ws-commons @@ -98,7 +95,6 @@ ${basedir}/src/test/resources/test.wsdl - -B-Xguava @@ -117,7 +113,7 @@ org.apache.cxf cxf-codegen-plugin - 2.7.1 + 3.1.8 generate-sources @@ -132,8 +128,6 @@ ${basedir}/src/test/resources/test.wsdl - - wsdl2java @@ -150,11 +144,8 @@ - - - @@ -162,7 +153,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.5 + 1.6 sign-artifacts @@ -176,13 +167,13 @@ org.apache.maven.plugins maven-site-plugin - 3.0 + 3.5.1 org.apache.maven.plugins maven-project-info-reports-plugin - 2.2 + 2.9 true true @@ -191,17 +182,17 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.7 + 2.10.4 org.apache.maven.plugins maven-surefire-report-plugin - 2.6 + 2.19.1 org.apache.maven.plugins maven-checkstyle-plugin - 2.6 + 2.17 @@ -209,16 +200,16 @@ org.apache.maven.plugins maven-compiler-plugin - 3.2 + 3.5.1 - 1.6 - 1.6 + ${targetJdk} + ${targetJdk} org.apache.maven.plugins maven-jar-plugin - 2.5 + 3.0.2 @@ -231,7 +222,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.10.1 + 2.10.4 @@ -243,7 +234,7 @@ org.apache.maven.plugins maven-source-plugin - 2.4 + 3.0.1 @@ -255,9 +246,8 @@ org.apache.maven.plugins maven-release-plugin - 2.5.1 + 2.5.3 - - + \ No newline at end of file diff --git a/src/main/java/com/github/danielwegener/xjcguava/XjcGuavaPlugin.java b/src/main/java/com/github/danielwegener/xjcguava/XjcGuavaPlugin.java index 9ccb44e..7247fe4 100644 --- a/src/main/java/com/github/danielwegener/xjcguava/XjcGuavaPlugin.java +++ b/src/main/java/com/github/danielwegener/xjcguava/XjcGuavaPlugin.java @@ -1,12 +1,9 @@ /* * Copyright 2013 Daniel Wegener - * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,27 +12,45 @@ */ package com.github.danielwegener.xjcguava; -import com.sun.codemodel.*; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.xml.sax.ErrorHandler; + +import com.sun.codemodel.JBlock; +import com.sun.codemodel.JClass; +import com.sun.codemodel.JCodeModel; +import com.sun.codemodel.JConditional; +import com.sun.codemodel.JDefinedClass; +import com.sun.codemodel.JExpr; +import com.sun.codemodel.JExpression; +import com.sun.codemodel.JFieldVar; +import com.sun.codemodel.JInvocation; +import com.sun.codemodel.JMethod; +import com.sun.codemodel.JMod; +import com.sun.codemodel.JType; +import com.sun.codemodel.JVar; import com.sun.tools.xjc.BadCommandLineException; import com.sun.tools.xjc.Options; import com.sun.tools.xjc.Plugin; import com.sun.tools.xjc.outline.ClassOutline; import com.sun.tools.xjc.outline.Outline; -import org.xml.sax.ErrorHandler; - -import java.io.IOException; -import java.util.*; /** - *

Generates hashCode, equals and toString methods using Guavas Objects helper class.

+ *

+ * Generates hashCode, equals and toString methods using Guavas Objects helper class. + *

* * @author Daniel Wegener */ public class XjcGuavaPlugin extends Plugin { public static final String OPTION_NAME = "Xguava"; - public static final String SKIP_TOSTRING_PARAM = "-"+OPTION_NAME + ":skipToString"; - + public static final String SKIP_TOSTRING_PARAM = "-" + OPTION_NAME + ":skipToString"; + private boolean skipToString = false; @Override public String getOptionName() { @@ -44,15 +59,15 @@ public String getOptionName() { @Override public String getUsage() { - return " -" + OPTION_NAME + "\t: enable generation of guava toString, equals and hashCode methods" - + "\n -" + SKIP_TOSTRING_PARAM + "\t: dont wrap collection parameters with Collections.unmodifiable..."; + return " -" + OPTION_NAME + "\t: enable generation of guava toString, equals and hashCode methods" + "\n -" + + SKIP_TOSTRING_PARAM + "\t: dont wrap collection parameters with Collections.unmodifiable..."; } @Override public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException { - final String arg = args[i].trim(); + if (SKIP_TOSTRING_PARAM.equals(arg)) { skipToString = true; return 1; @@ -60,25 +75,20 @@ public int parseArgument(Options opt, String[] args, int i) throws BadCommandLin return 0; } - private boolean skipToString = false; - @Override public boolean run(final Outline outline, final Options options, final ErrorHandler errorHandler) { // For each defined class final JCodeModel model = outline.getCodeModel(); for (final ClassOutline classOutline : outline.getClasses()) { - final JDefinedClass implClass = classOutline.implClass; - if (!skipToString && !implClass.isAbstract() && implClass.getMethod("toString", new JType[0]) == null) { generateToStringMethod(model, implClass); } - if (!implClass.isAbstract()) { - if (implClass.getMethod("hashCode",new JType[0]) == null) + if (implClass.getMethod("hashCode", new JType[0]) == null) generateHashCodeMethod(model, implClass); - if (implClass.getMethod("equals",new JType[]{model._ref(Object.class)}) == null) { - generateEqualsMethod(model,implClass); + if (implClass.getMethod("equals", new JType[] { model._ref(Object.class) }) == null) { + generateEqualsMethod(model, implClass); } } } @@ -86,61 +96,49 @@ public boolean run(final Outline outline, final Options options, final ErrorHand } protected void generateToStringMethod(JCodeModel model, JDefinedClass clazz) { - final JMethod toStringMethod = clazz.method(JMod.PUBLIC, String.class,"toString"); + final JMethod toStringMethod = clazz.method(JMod.PUBLIC, String.class, "toString"); toStringMethod.annotate(Override.class); - final JClass objects = model.ref("com.google.common.base.Objects"); + final JClass objects = model.ref("com.google.common.base.MoreObjects"); final Collection superClassInstanceFields = getInstanceFields(getSuperclassFields(clazz)); final Collection thisClassInstanceFields = getInstanceFields(clazz.fields().values()); - final JBlock content = toStringMethod.body(); - - final JInvocation toStringHelperCall = objects.staticInvoke("toStringHelper"); + toStringHelperCall.arg(JExpr._this()); JInvocation fluentCall = toStringHelperCall; - for (JFieldVar superField : superClassInstanceFields) { fluentCall = fluentCall.invoke("add"); fluentCall.arg(JExpr.lit(superField.name())); fluentCall.arg(superField); } - for (JFieldVar thisField : thisClassInstanceFields) { fluentCall = fluentCall.invoke("add"); fluentCall.arg(JExpr.lit(thisField.name())); fluentCall.arg(thisField); } - fluentCall = fluentCall.invoke("toString"); - content._return(fluentCall); - } protected void generateHashCodeMethod(JCodeModel model, JDefinedClass clazz) { - final JClass objects = model.ref(com.google.common.base.Objects.class); final Collection thisClassInstanceFields = getInstanceFields(clazz.fields().values()); final Collection superClassInstanceFields = getInstanceFields(getSuperclassFields(clazz)); - // Don't create hashCode for empty classes - if (thisClassInstanceFields.isEmpty() && superClassInstanceFields.isEmpty()) return; - - final JMethod hashCodeMethod = clazz.method(JMod.PUBLIC, model.INT ,"hashCode"); + // Don't create hashCode for empty classes + if (thisClassInstanceFields.isEmpty() && superClassInstanceFields.isEmpty()) { + return; + } + final JMethod hashCodeMethod = clazz.method(JMod.PUBLIC, model.INT, "hashCode"); final JBlock content = hashCodeMethod.body(); hashCodeMethod.annotate(Override.class); - final JInvocation hashCodeCall = objects.staticInvoke("hashCode"); - - for (JFieldVar superField : superClassInstanceFields) { hashCodeCall.arg(superField); } - for (JFieldVar thisField : thisClassInstanceFields) { hashCodeCall.arg(thisField); } - content._return(hashCodeCall); } @@ -148,11 +146,13 @@ protected void generateEqualsMethod(JCodeModel model, JDefinedClass clazz) { final Collection superClassInstanceFields = getInstanceFields(getSuperclassFields(clazz)); final Collection thisClassInstanceFields = getInstanceFields(clazz.fields().values()); // Don't create equals for empty classes - if (thisClassInstanceFields.isEmpty() && superClassInstanceFields.isEmpty()) return; + if (thisClassInstanceFields.isEmpty() && superClassInstanceFields.isEmpty()) { + return; + } - final JMethod equalsMethod = clazz.method(JMod.PUBLIC, model.BOOLEAN ,"equals"); + final JMethod equalsMethod = clazz.method(JMod.PUBLIC, model.BOOLEAN, "equals"); equalsMethod.annotate(Override.class); - final JVar other = equalsMethod.param(Object.class,"other"); + final JVar other = equalsMethod.param(Object.class, "other"); final JClass objects = model.ref(com.google.common.base.Objects.class); final JClass arrays = model.ref(java.util.Arrays.class); @@ -199,15 +199,13 @@ protected void generateEqualsMethod(JCodeModel model, JDefinedClass clazz) { content._return(equalsBuilder); } - - /** * Takes a collection of fields, and returns a new collection containing only the instance * (i.e. non-static) fields. */ protected Collection getInstanceFields(final Collection fields) { final List instanceFields = new ArrayList(); - for (final JFieldVar field : fields) { + for (JFieldVar field : fields) { if (!isStatic(field)) { instanceFields.add(field); } @@ -223,12 +221,10 @@ protected List getSuperclassFields(final JDefinedClass implClass) { fieldList.addAll(0, ((JDefinedClass) superclass).fields().values()); superclass = superclass._extends(); } - return fieldList; } protected boolean isStatic(final JFieldVar field) { - final JMods fieldMods = field.mods(); - return (fieldMods.getValue() & JMod.STATIC) > 0; + return (field.mods().getValue() & JMod.STATIC) > 0; } } \ No newline at end of file