Thank you for your answer Glen. I missed this plugin in my search.
Though this plugin offers a lot of flexibility, it generates too much
code for my needs.
In the meantime, I tried to make my own plugin which appear to be
quite simple. As I was inspired by the cxf-xjc-ts plugin, I post it
here so anyone can use it :
package com.package;
import java.util.logging.Logger;
import org.xml.sax.*;
import com.sun.codemodel.*;
import com.sun.tools.xjc.*;
import com.sun.tools.xjc.outline.*;
/**
* @author Simon B.
*/
public class ToStringPlugin extends Plugin {
private static final Logger LOG =
Logger.getLogger(ToStringPlugin.class.getName());
private static final String NAME = "toString() XJC Plugin";
@Override
public String getOptionName() {
return "Xto-string";
}
@Override
public String getUsage() {
return "-Xto-string : Activate plugin to add a toString()
method to
generated classes.";
}
@Override
public boolean run(Outline outline, Options opt, ErrorHandler
errorHandler) throws SAXException {
LOG.fine("Running " + NAME + ".");
JClass guavaObjects =
outline.getCodeModel().ref("com.google.common.base.Objects");
for (ClassOutline co : outline.getClasses()) {
addToStringMethod(co, guavaObjects);
}
return true;
}
private void addToStringMethod(ClassOutline co, JClass guavaObjects) {
JDefinedClass implementation = co.implClass;
//
// Create toString() method
//
JMethod toStringMethod = implementation.method(JMod.PUBLIC,
String.class, "toString");
JDocComment doc = toStringMethod.javadoc();
doc.add("Generated by " + NAME);
toStringMethod.annotate(Override.class);
//
// return Objects.toStringHelper(this)
// .add("propertyName0", propertyValue0)
// ...
// .add("propertyNameN", propertyValueN)
// .toString();
//
JInvocation toStringHelperInvocation =
guavaObjects.staticInvoke("toStringHelper");
toStringHelperInvocation.arg(JExpr._this());
JInvocation lastAddInvocation = toStringHelperInvocation;
for (JFieldVar var : implementation.fields().values()) {
if (isStatic(var.mods()) == false) {
JInvocation addInvocation =
JExpr.invoke(lastAddInvocation, "add");
addInvocation.arg(JExpr.lit(var.name()));
addInvocation.arg(JExpr.ref(var.name()));
lastAddInvocation = addInvocation;
}
}
JInvocation toStringInvocation =
JExpr.invoke(lastAddInvocation, "toString");
toStringMethod.body()._return(toStringInvocation);
}
private static boolean isStatic(JMods mods) {
return (mods.getValue() & JMod.STATIC) != 0;
}
}
2012/8/31 Glen Mazza <[email protected]>:
> Hi Simon, I haven't done this before, but the alternative ToString plugin
> (http://confluence.highsource.org/display/J2B/ToString+plugin) has a
> "-XtoString-toStringStrategyClass" option where you can apparently make up
> whatever toString() format you desire. This blog entry:
> http://www.jroller.com/gmazza/entry/enhancing_jaxb_artifacts#Plugin can show
> you how to use the plugin itself, but creating the toString() strategy
> itself may require some googling of source code to find out the process.
>
> HTH,
> Glen
>
>
> On 08/31/2012 06:09 AM, Simon wrote:
>>
>> Hi !
>>
>> I'am using CXF (2.6.1) for my projects and I would like to have a
>> toString() method for our generated JAXB classes.
>>
>> I'am using Maven as the build system.
>>
>> The easier solution seems to use the 'cxf-codegen-plugin' in
>> combination with the 'toString() XJC Plugin' like this :
>>
>> <plugin>
>> <groupId>org.apache.cxf</groupId>
>> <artifactId>cxf-codegen-plugin</artifactId>
>> <version>2.6.1</version>
>> <dependencies>
>> <dependency>
>> <groupId>org.apache.cxf.xjcplugins</groupId>
>> <artifactId>cxf-xjc-ts</artifactId>
>> <version>2.6.0</version>
>> </dependency>
>> </dependencies>
>> <configuration>
>> <defaultOptions>
>> <extraargs>
>>
>> <extraarg>-xjc-Xts:style:org.apache.commons.lang.builder.ToStringStyle.SIMPLE_STYLE</extraarg>
>> </extraargs>
>> </defaultOptions>
>> </configuration>
>> <executions>
>> <execution>
>> <id>generate-sources</id>
>> <goals>
>> <goal>wsdl2java</goal>
>> </goals>
>> </execution>
>> </executions>
>> </plugin>
>>
>> This will generate this :
>>
>> @Override
>> public String toString() {
>> return ToStringBuilder.reflectionToString(this,
>> ToStringStyle.SIMPLE_STYLE);
>> }
>>
>> However, as my projects are all using Guava, I would prefere to
>> generate the toString() method this way :
>>
>> @Override
>> public String toString() {
>> return Objects.toStringHelper(this)
>> .add("name", name)
>> .add("phone", phone)
>> .add("eMail", eMail)
>> .add("fax", fax)
>> .toString();
>> }
>>
>> Is there a plan to add this feature (using Guava) in the existing XJC
>> Plugin ?
>>
>> Thank you !
>
>
>
> --
> Glen Mazza
> Talend Community Coders - coders.talend.com
> blog: www.jroller.com/gmazza
>