[
https://issues.apache.org/jira/browse/CXF-4899?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Alec Bickerton updated CXF-4899:
--------------------------------
Attachment: BooleanGetterPlugin.java.patch
Patch added as attachment.
> Boolean getter plugin does not leave isXXX() method.
> ----------------------------------------------------
>
> Key: CXF-4899
> URL: https://issues.apache.org/jira/browse/CXF-4899
> Project: CXF
> Issue Type: Improvement
> Components: Tooling
> Affects Versions: 2.6.5
> Reporter: Alec Bickerton
> Labels: patch
> Attachments: BooleanGetterPlugin.java.patch
>
> Original Estimate: 24h
> Remaining Estimate: 24h
>
> When using the Boolean getter plugin to generate a Boolean getter method in
> the generated code, the original isXXX method is replaced.
> In small code bases this would be fine, unfortunately I need those is methods
> as there is a signiicant amount of legacy code that relies on them.
> JIRA doesn't give me the option to add an attachment, so here's my patch.
> ===================================================================
> --- boolean/src/main/java/org/apache/cxf/xjc/bg/BooleanGetterPlugin.java
> (revision 1430827)
> +++ boolean/src/main/java/org/apache/cxf/xjc/bg/BooleanGetterPlugin.java
> (working copy)
> @@ -19,16 +19,22 @@
> package org.apache.cxf.xjc.bg;
>
> import java.util.Collection;
> +import java.util.HashMap;
> +import java.util.Iterator;
> +import java.util.Map;
> +import java.util.Map.Entry;
> import java.util.logging.Logger;
>
> import org.xml.sax.ErrorHandler;
>
> +import com.sun.codemodel.JBlock;
> import com.sun.codemodel.JMethod;
> +import com.sun.codemodel.JMod;
> +import com.sun.codemodel.JType;
> import com.sun.tools.xjc.Options;
> import com.sun.tools.xjc.outline.ClassOutline;
> import com.sun.tools.xjc.outline.Outline;
>
> -
> /**
> * Generate getters named getXXX() for Booleans instead of isXXX(). Useful
> to use generated beans with tools
> * that needs introspections, like dozer.
> @@ -36,7 +42,8 @@
> public class BooleanGetterPlugin {
>
> private static final Logger LOG =
> Logger.getLogger(BooleanGetterPlugin.class.getName()); //NOPMD
> -
> + private static final String IS_PREFIX = "is";
> +
> public BooleanGetterPlugin() {
> }
>
> @@ -49,21 +56,40 @@
> }
>
> public boolean run(Outline outline, Options opt, ErrorHandler
> errorHandler) {
> - LOG.info("Running boolean getter plugin.");
> for (ClassOutline classOutline : outline.getClasses()) {
> - Collection<JMethod> methods = classOutline.implClass.methods();
> - for (JMethod method : methods) {
> - if (method.name().startsWith("is")) {
> - String newName = "get" + method.name().substring(2);
> - LOG.info("Changing method name from " + method.name() +
> " to " + newName);
> - method.javadoc().add(
> - "\nThis getter has been renamed
> from " + method.name() + "() to "
> - + newName + "() by
> cxf-xjc-boolean plugin.");
> - method.name(newName);
> - }
> + processClass(classOutline);
> + }
> + return true;
> + }
> +
> + private void processClass(ClassOutline clazz) {
> + Collection<JMethod> methods = clazz.implClass.methods();
> + Map<String , JType> methodsToAdd = new HashMap<String , JType>();
> + for (JMethod method : methods) {
> + if (method.name().startsWith(IS_PREFIX) &&
> requiresGetter(methods, method)) {
> + methodsToAdd.put(method.name(), method.type());
> }
> }
> +
> + Iterator<Entry<String, JType>> todo =
> methodsToAdd.entrySet().iterator();
> + while (todo.hasNext()) {
> + Entry<String, JType> entry = todo.next();
> + String newName = "get" + entry.getKey().substring(2);
> + //LOG.info("Adding method " + newName);
> + JMethod newMethod = clazz.implClass.method(JMod.PUBLIC,
> entry.getValue(), newName);
> + JBlock body = newMethod.body();
> + body.directStatement("return " + entry.getKey() + "();");
> + }
> + }
> +
> + private boolean requiresGetter(Collection<JMethod> methods, JMethod
> method) {
> + String newName = "get" + method.name().substring(2);
> + // Check if already exists.
> + for (JMethod cursor : methods) {
> + if (newName.equals(cursor.name())) {
> + return false;
> + }
> + }
> return true;
> }
> -
> }
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira