Repository: camel
Updated Branches:
  refs/heads/master 7616b4356 -> f6114d52d


Fixed JavadocApiMethodGeneratorMojo to support multiple type arguments, added 
includeStaticMethods parameter to support adding static method to API model


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f6114d52
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f6114d52
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f6114d52

Branch: refs/heads/master
Commit: f6114d52de9a037a7bc530d4f7d0c260a371e00a
Parents: 909077a
Author: Dhiraj Bokde <dhira...@yahoo.com>
Authored: Fri Jun 20 14:05:43 2014 -0700
Committer: Dhiraj Bokde <dhira...@yahoo.com>
Committed: Fri Jun 20 14:09:20 2014 -0700

----------------------------------------------------------------------
 .../__artifactId__-component/pom.xml            |  1 +
 .../maven/AbstractApiMethodGeneratorMojo.java   |  3 +-
 .../camel/maven/ApiComponentGeneratorMojo.java  |  2 +
 .../org/apache/camel/maven/FromJavadoc.java     | 10 +++
 .../maven/JavadocApiMethodGeneratorMojo.java    | 65 +++++++++++++++-----
 5 files changed, 66 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f6114d52/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/pom.xml
----------------------------------------------------------------------
diff --git 
a/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/pom.xml
 
b/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/pom.xml
index f86474d..b605a57 100644
--- 
a/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/pom.xml
+++ 
b/tooling/archetypes/camel-archetype-api-component/src/main/resources/archetype-resources/__artifactId__-component/pom.xml
@@ -202,6 +202,7 @@
                     <excludePackages>package-name-patterns</excludePackages>
                     <excludeClasses>class-name-patterns</excludeClasses>
                     <excludeMethods>method-name-patterns</excludeMethods>
+                    <includeStaticMethods>use 'true' to include static 
methods, false by default<includeStaticMethods>
                     -->
                   </fromJavadoc>
                 </api>

http://git-wip-us.apache.org/repos/asf/camel/blob/f6114d52/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
----------------------------------------------------------------------
diff --git 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
index 3cb3e8f..ec17364 100644
--- 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
+++ 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
@@ -97,7 +97,7 @@ public abstract class AbstractApiMethodGeneratorMojo extends 
AbstractApiMethodBa
 
     public abstract List<String> getSignatureList() throws 
MojoExecutionException;
 
-    public Class getProxyType() throws MojoExecutionException {
+    public Class<?> getProxyType() throws MojoExecutionException {
         if (proxyType == null) {
             // load proxy class from Project runtime dependencies
             try {
@@ -290,6 +290,7 @@ public abstract class AbstractApiMethodGeneratorMojo 
extends AbstractApiMethodBa
             StringBuilder parameterizedType = new StringBuilder(canonicalName);
             parameterizedType.append('<');
 
+            // Note: its ok to split, since we don't support parsing nested 
type arguments
             String[] argTypes = typeArgs.split(",");
             boolean ignore = false;
             for (String argType : argTypes) {

http://git-wip-us.apache.org/repos/asf/camel/blob/f6114d52/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
----------------------------------------------------------------------
diff --git 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
index c924cdf..dabdcd5 100644
--- 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
+++ 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
@@ -163,6 +163,8 @@ public class ApiComponentGeneratorMojo extends 
AbstractApiMethodBaseMojo {
                     apiFromJavadoc.getExcludeClasses() : 
fromJavadoc.getExcludeClasses();
                 javadocMojo.excludeMethods = 
apiFromJavadoc.getExcludeMethods() != null ?
                     apiFromJavadoc.getExcludeMethods() : 
fromJavadoc.getExcludeMethods();
+                javadocMojo.includeStaticMethods = 
apiFromJavadoc.getIncludeStaticMethods() != null ?
+                    apiFromJavadoc.getIncludeStaticMethods() : 
fromJavadoc.getIncludeStaticMethods();
 
                 apiMethodGenerator = javadocMojo;
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/f6114d52/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/FromJavadoc.java
----------------------------------------------------------------------
diff --git 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/FromJavadoc.java
 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/FromJavadoc.java
index fb2ce5f..f23a301 100644
--- 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/FromJavadoc.java
+++ 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/FromJavadoc.java
@@ -27,6 +27,8 @@ public class FromJavadoc {
 
     protected String excludeMethods;
 
+    protected Boolean includeStaticMethods;
+
     public String getExcludePackages() {
         return excludePackages;
     }
@@ -50,4 +52,12 @@ public class FromJavadoc {
     public void setExcludeMethods(String excludeMethods) {
         this.excludeMethods = excludeMethods;
     }
+
+    public Boolean getIncludeStaticMethods() {
+        return includeStaticMethods;
+    }
+
+    public void setIncludeStaticMethods(Boolean includeStaticMethods) {
+        this.includeStaticMethods = includeStaticMethods;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f6114d52/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojo.java
----------------------------------------------------------------------
diff --git 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojo.java
 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojo.java
index 8b02c3e..a8a9beb 100644
--- 
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojo.java
+++ 
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojo.java
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javax.swing.text.ChangedCharSetException;
 import javax.swing.text.SimpleAttributeSet;
@@ -55,6 +56,8 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
     }
 
     protected static final String DEFAULT_EXCLUDE_PACKAGES = "javax?\\.lang.*";
+    private static final Pattern ARGTYPES_PATTERN = 
Pattern.compile("\\s*([^<\\s,]+\\s*(<[^>]+>)?)\\s*,?");
+    private static final Pattern RAW_ARGTYPES_PATTERN = 
Pattern.compile("\\s*([^<\\s,]+)\\s*(<[^>]+>)?\\s*,?");
 
     @Parameter(property = PREFIX + "excludePackages", defaultValue = 
DEFAULT_EXCLUDE_PACKAGES)
     protected String excludePackages;
@@ -65,6 +68,9 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
     @Parameter(property = PREFIX + "excludeMethods")
     protected String excludeMethods;
 
+    @Parameter(property = PREFIX + "includeStaticMethods")
+    protected Boolean includeStaticMethods;
+
     @Override
     public List<String> getSignatureList() throws MojoExecutionException {
         // signatures as map from signature with no arg names to arg names 
from JavadocParser
@@ -116,7 +122,13 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
                         if (args.isEmpty()) {
                             types = new String[0];
                         } else {
-                            types = args.split(",");
+                            // get raw types from args
+                            final List<String> rawTypes = new 
ArrayList<String>();
+                            final Matcher argTypesMatcher = 
RAW_ARGTYPES_PATTERN.matcher(args);
+                            while (argTypesMatcher.find()) {
+                                rawTypes.add(argTypesMatcher.group(1));
+                            }
+                            types = rawTypes.toArray(new 
String[rawTypes.size()]);
                         }
                         final String resultType = getResultType(aClass, name, 
types);
                         if (resultType != null) {
@@ -146,7 +158,7 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
         for (int i = 0; i < types.length; i++) {
             try {
                 try {
-                    argTypes[i] = ApiMethodParser.forName(types[i].trim(), 
classLoader);
+                    argTypes[i] = ApiMethodParser.forName(types[i], 
classLoader);
                 } catch (ClassNotFoundException e) {
                     throw new MojoExecutionException(e.getMessage(), e);
                 }
@@ -154,13 +166,13 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
                 throw new MojoExecutionException(e.getCause().getMessage(), 
e.getCause());
             }
         }
-        // return null for non-public and non-static methods
+
+        // return null for non-public methods, and for non-static methods if 
includeStaticMethods is null or false
         String result = null;
         try {
             final Method method = aClass.getMethod(name, argTypes);
-            // only include non-static public methods
             int modifiers = method.getModifiers();
-            if (!Modifier.isStatic(modifiers)) {
+            if (!Modifier.isStatic(modifiers) || 
Boolean.TRUE.equals(includeStaticMethods)) {
                 result = method.getReturnType().getCanonicalName();
             }
         } catch (NoSuchMethodException e) {
@@ -175,7 +187,8 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
         return result;
     }
 
-    private class JavadocParser extends Parser {
+    private static class JavadocParser extends Parser {
+        private static final String NON_BREAKING_SPACE = "\u00A0";
         private String hrefPattern;
 
         private ParserState parserState;
@@ -211,7 +224,8 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
                             if (href != null) {
                                 String hrefAttr = (String) href;
                                 if (hrefAttr.contains(hrefPattern)) {
-                                    methodWithTypes = 
StringEscapeUtils.unescapeHtml(hrefAttr.substring(hrefAttr.indexOf('#') + 1));
+                                    // unescape HTML
+                                    methodWithTypes = 
unescapeHtml(hrefAttr.substring(hrefAttr.indexOf('#') + 1));
                                 }
                             }
                         }
@@ -222,6 +236,10 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
             }
         }
 
+        private static String unescapeHtml(String htmlString) {
+            return 
StringEscapeUtils.unescapeHtml(htmlString).replaceAll(NON_BREAKING_SPACE, " ");
+        }
+
         @Override
         protected void handleEmptyTag(TagElement tag) {
             if (parserState == ParserState.METHOD && 
HTML.Tag.CODE.equals(tag.getHTMLTag())) {
@@ -244,15 +262,34 @@ public class JavadocApiMethodGeneratorMojo extends 
AbstractApiMethodGeneratorMoj
             if (typeString.isEmpty()) {
                 return "()";
             }
-            final String[] types = typeString.split(",");
-            // use HTTP decode
-            String argText = 
StringEscapeUtils.unescapeHtml(methodTextBuilder.toString());
-            final String[] args = argText.substring(argText.indexOf('(') + 1, 
argText.indexOf(')')).split(",");
+
+            // split types list
+            final List<String> typeList = new ArrayList<String>();
+            final Matcher typeMatcher = ARGTYPES_PATTERN.matcher(typeString);
+            while (typeMatcher.find()) {
+                typeList.add(typeMatcher.group(1).replaceAll(" ", ""));
+            }
+
+            // unescape HTML method text
+            final String plainText = 
unescapeHtml(methodTextBuilder.toString());
+            final String argsString = 
plainText.substring(plainText.indexOf('(') + 1, plainText.indexOf(')'));
+            final Matcher argMatcher = 
ApiMethodParser.ARGS_PATTERN.matcher(argsString);
+            final List<String> argNames = new ArrayList<String>();
+            while (argMatcher.find()) {
+                argNames.add(argMatcher.group(3));
+            }
+
+            // make sure number of types and names match
+            if (typeList.size() != argNames.size()) {
+                throw new IllegalArgumentException("Unexpected Javadoc error, 
different number of arg types and names");
+            }
+
+            final String[] names = argNames.toArray(new 
String[argNames.size()]);
             StringBuilder builder = new StringBuilder("(");
-            for (int i = 0; i < types.length; i++) {
+            int i = 0;
+            for (String type : typeList) {
                 // split on space or non-breaking space
-                final String[] arg = args[i].trim().split(" |\u00A0");
-                builder.append(types[i]).append(" 
").append(arg[1].trim()).append(",");
+                builder.append(type).append(" ").append(names[i]).append(",");
             }
             builder.deleteCharAt(builder.length() - 1);
             builder.append(")");

Reply via email to