You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dh...@apache.org on 2014/06/10 21:52:03 UTC

[35/35] git commit: Added replaceWithType feature to argument Substitution to allow replacing argument name with substituted type string

Added replaceWithType feature to argument Substitution to allow replacing argument name with substituted type string


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

Branch: refs/heads/master
Commit: 420fed02190d00416480e4df809cb3e343e22497
Parents: 4b07979
Author: Dhiraj Bokde <dh...@yahoo.com>
Authored: Mon Jun 9 14:25:59 2014 -0700
Committer: Dhiraj Bokde <dh...@yahoo.com>
Committed: Tue Jun 10 12:48:35 2014 -0700

----------------------------------------------------------------------
 .../component/ArgumentSubstitutionParser.java   | 76 ++++++++++++++++----
 .../ArgumentSubstitutionParserTest.java         |  8 ++-
 .../__artifactId__-component/pom.xml            |  2 +
 .../src/it/all-it/pom.xml                       |  7 ++
 .../maven/AbstractApiMethodGeneratorMojo.java   |  6 +-
 .../org/apache/camel/maven/Substitution.java    | 12 +++-
 .../maven/FileApiMethodGeneratorMojoTest.java   |  5 +-
 .../JavadocApiMethodGeneratorMojoTest.java      |  2 +-
 8 files changed, 95 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/camel-core/src/main/java/org/apache/camel/util/component/ArgumentSubstitutionParser.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/component/ArgumentSubstitutionParser.java b/camel-core/src/main/java/org/apache/camel/util/component/ArgumentSubstitutionParser.java
index 0ebf63c..2544dff 100644
--- a/camel-core/src/main/java/org/apache/camel/util/component/ArgumentSubstitutionParser.java
+++ b/camel-core/src/main/java/org/apache/camel/util/component/ArgumentSubstitutionParser.java
@@ -23,24 +23,25 @@ public class ArgumentSubstitutionParser<T> extends ApiMethodParser<T> {
         super(proxyType);
         Map<String, Map<String, List<NameReplacement>>> regexMap = new LinkedHashMap<String, Map<String, List<NameReplacement>>>();
 
-        for (Substitution tuple : substitutions) {
-            tuple.validate();
+        for (Substitution substitution : substitutions) {
+            substitution.validate();
 
             final NameReplacement nameReplacement = new NameReplacement();
-            nameReplacement.replacement = tuple.replacement;
-            if (tuple.argType != null) {
-                nameReplacement.type = forName(tuple.argType);
+            nameReplacement.replacement = substitution.replacement;
+            if (substitution.argType != null) {
+                nameReplacement.typePattern = Pattern.compile(substitution.argType);
             }
+            nameReplacement.replaceWithType = substitution.replaceWithType;
 
-            Map<String, List<NameReplacement>> replacementMap = regexMap.get(tuple.method);
+            Map<String, List<NameReplacement>> replacementMap = regexMap.get(substitution.method);
             if (replacementMap == null) {
                 replacementMap = new LinkedHashMap<String, List<NameReplacement>>();
-                regexMap.put(tuple.method, replacementMap);
+                regexMap.put(substitution.method, replacementMap);
             }
-            List<NameReplacement> replacements = replacementMap.get(tuple.argName);
+            List<NameReplacement> replacements = replacementMap.get(substitution.argName);
             if (replacements == null) {
                 replacements = new ArrayList<NameReplacement>();
-                replacementMap.put(tuple.argName, replacements);
+                replacementMap.put(substitution.argName, replacements);
             }
             replacements.add(nameReplacement);
         }
@@ -63,19 +64,40 @@ public class ArgumentSubstitutionParser<T> extends ApiMethodParser<T> {
         for (ApiMethodModel model : parseResult) {
             // look for method name matches
             for (Map.Entry<Pattern, Map<Pattern, List<NameReplacement>>> methodEntry : methodMap.entrySet()) {
+                // match the whole method name
                 if (methodEntry.getKey().matcher(model.getName()).matches()) {
 
                     // look for arg name matches
                     final List<Argument> updatedArguments = new ArrayList<Argument>();
                     final Map<Pattern, List<NameReplacement>> argMap = methodEntry.getValue();
                     for (Argument argument : model.getArguments()) {
+                        final Class<?> argType = argument.getType();
+                        final String argTypeName = argType.getCanonicalName();
+
                         for (Map.Entry<Pattern, List<NameReplacement>> argEntry : argMap.entrySet()) {
                             final Matcher matcher = argEntry.getKey().matcher(argument.getName());
+
+                            // match argument name substring
                             if (matcher.find()) {
                                 final List<NameReplacement> adapters = argEntry.getValue();
                                 for (NameReplacement adapter : adapters) {
-                                    if (adapter.type == null || adapter.type == argument.getType()) {
-                                        argument = new Argument(matcher.replaceAll(adapter.replacement), argument.getType());
+                                    if (adapter.typePattern == null) {
+                                        // no type pattern
+                                        final String newName = getJavaArgName(matcher.replaceAll(adapter.replacement));
+                                        argument = new Argument(newName, argType);
+                                    } else {
+                                        final Matcher typeMatcher = adapter.typePattern.matcher(argTypeName);
+                                        if (typeMatcher.find()) {
+                                            if (!adapter.replaceWithType) {
+                                                // replace argument name
+                                                final String newName = getJavaArgName(matcher.replaceAll(adapter.replacement));
+                                                argument = new Argument(newName, argType);
+                                            } else {
+                                                // replace name with argument type name
+                                                final String newName = getJavaArgName(typeMatcher.replaceAll(adapter.replacement));
+                                                argument = new Argument(newName, argType);
+                                            }
+                                        }
                                     }
                                 }
                             }
@@ -95,12 +117,24 @@ public class ArgumentSubstitutionParser<T> extends ApiMethodParser<T> {
         return result;
     }
 
+    private String getJavaArgName(String name) {
+        // make sure the first character is lowercase
+        // useful for replacement using type names
+        char firstChar = name.charAt(0);
+        if (Character.isLowerCase(firstChar)) {
+            return name;
+        } else {
+            return Character.toLowerCase(firstChar) + name.substring(1);
+        }
+    }
+
     public static class Substitution {
 
         private String method;
         private String argName;
         private String argType;
         private String replacement;
+        private boolean replaceWithType;
 
         /**
          * Creates a substitution for all argument types.
@@ -122,10 +156,21 @@ public class ArgumentSubstitutionParser<T> extends ApiMethodParser<T> {
          * @param replacement replacement text for argument name
          */
         public Substitution(String method, String argName, String argType, String replacement) {
-            this.method = method;
-            this.argName = argName;
+            this(method, argName, replacement);
             this.argType = argType;
-            this.replacement = replacement;
+        }
+
+        /**
+         * Create a substitution for a specific argument type and flag to indicate whether the replacement uses
+         * @param method
+         * @param argName
+         * @param argType
+         * @param replacement
+         * @param replaceWithType
+         */
+        public Substitution(String method, String argName, String argType, String replacement, boolean replaceWithType) {
+            this(method, argName, argType, replacement);
+            this.replaceWithType = replaceWithType;
         }
 
         public void validate() {
@@ -137,6 +182,7 @@ public class ArgumentSubstitutionParser<T> extends ApiMethodParser<T> {
 
     private static class NameReplacement {
         private String replacement;
-        private Class<?> type;
+        private Pattern typePattern;
+        private boolean replaceWithType;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/camel-core/src/test/java/org/apache/camel/util/component/ArgumentSubstitutionParserTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/component/ArgumentSubstitutionParserTest.java b/camel-core/src/test/java/org/apache/camel/util/component/ArgumentSubstitutionParserTest.java
index ce0153d..a9487c0 100644
--- a/camel-core/src/test/java/org/apache/camel/util/component/ArgumentSubstitutionParserTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/component/ArgumentSubstitutionParserTest.java
@@ -14,10 +14,11 @@ public class ArgumentSubstitutionParserTest {
     @Test
     public void testParse() throws Exception {
 
-        final Substitution[] adapters = new Substitution[3];
+        final Substitution[] adapters = new Substitution[4];
         adapters[0] = new Substitution(".+", "name", PERSON);
         adapters[1] = new Substitution("greet.+", "person([0-9]+)", "astronaut$1");
         adapters[2] = new Substitution(".+", "(.+)", "java.util.List", "$1List");
+        adapters[3] = new Substitution(".+", "(.+)", ".*?(\\w++)\\[\\]", "$1Array", true);
 
         final ApiMethodParser<TestProxy> parser = new ArgumentSubstitutionParser<TestProxy>(TestProxy.class, adapters);
 
@@ -45,8 +46,11 @@ public class ArgumentSubstitutionParserTest {
         assertEquals("astronaut1", greetUs.getArguments().get(0).getName());
         assertEquals("astronaut2", greetUs.getArguments().get(1).getName());
 
-        final ApiMethodParser.ApiMethodModel greetAll = methodModels.get(1);
+        final ApiMethodParser.ApiMethodModel greetAll = methodModels.get(0);
         assertEquals("personsList", greetAll.getArguments().get(0).getName());
+
+        final ApiMethodParser.ApiMethodModel greetAll1 = methodModels.get(1);
+        assertEquals("stringArray", greetAll1.getArguments().get(0).getName());
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/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 31cf249..8e66c2c 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
@@ -157,6 +157,7 @@
                   <argName>^.+$</argName>
                   <argType>java.lang.String</argType>
                   <replacement>$1Param</replacement>
+                  <replaceWithType>false</replaceWithType>
                 </substitution>
               </substitutions>
               -->
@@ -181,6 +182,7 @@
                   <argName>^.+$</argName>
                   <argType>java.lang.String</argType>
                   <replacement>$1Arg</replacement>
+                  <replaceWithType>false</replaceWithType>
                 </substitution>
               </substitutions>
               -->

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/tooling/maven/camel-component-util-maven-plugin/src/it/all-it/pom.xml
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/it/all-it/pom.xml b/tooling/maven/camel-component-util-maven-plugin/src/it/all-it/pom.xml
index 9f6f877..d13ef72 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/it/all-it/pom.xml
+++ b/tooling/maven/camel-component-util-maven-plugin/src/it/all-it/pom.xml
@@ -89,6 +89,13 @@
                   <argType>java.util.List</argType>
                   <replacement>$1List</replacement>
                 </substitution>
+                <substitution>
+                  <method>.+</method>
+                  <argName>(.+)</argName>
+                  <argType>.*?(\w++)\[\]</argType>
+                  <replacement>$1Array</replacement>
+                  <replaceWithType>true</replaceWithType>
+                </substitution>
               </substitutions>
               <signatureFile>../../../src/test/resources/test-proxy-signatures.txt</signatureFile>
             </configuration>

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java b/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
index 445ceda..852b09e 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
+++ b/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/AbstractApiMethodGeneratorMojo.java
@@ -174,8 +174,10 @@ public abstract class AbstractApiMethodGeneratorMojo extends AbstractGeneratorMo
     public ArgumentSubstitutionParser.Substitution[] getArgumentSubstitutions() {
         ArgumentSubstitutionParser.Substitution[] subs = new ArgumentSubstitutionParser.Substitution[substitutions.length];
         for (int i = 0; i < substitutions.length; i++) {
-            subs[i] = new ArgumentSubstitutionParser.Substitution(substitutions[i].getMethod(),
-                    substitutions[i].getArgName(), substitutions[i].getArgType(), substitutions[i].getReplacement());
+            final Substitution substitution = substitutions[i];
+            subs[i] = new ArgumentSubstitutionParser.Substitution(substitution.getMethod(),
+                    substitution.getArgName(), substitution.getArgType(),
+                    substitution.getReplacement(), substitution.isReplaceWithType());
         }
         return subs;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/Substitution.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/Substitution.java b/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/Substitution.java
index 0e7b2a8..d16e041 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/Substitution.java
+++ b/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/Substitution.java
@@ -25,15 +25,17 @@ public class Substitution {
     private String argName;
     private String argType;
     private String replacement;
+    private boolean replaceWithType;
 
     public Substitution() {
     }
 
-    public Substitution(String method, String argName, String argType, String replacement) {
+    public Substitution(String method, String argName, String argType, String replacement, boolean replaceWithType) {
         this.method = method;
         this.argName = argName;
         this.argType = argType;
         this.replacement = replacement;
+        this.replaceWithType = replaceWithType;
     }
 
     public String getMethod() {
@@ -67,4 +69,12 @@ public class Substitution {
     public void setReplacement(String replacement) {
         this.replacement = replacement;
     }
+
+    public boolean isReplaceWithType() {
+        return replaceWithType;
+    }
+
+    public void setReplaceWithType(boolean replaceWithType) {
+        this.replaceWithType = replaceWithType;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java b/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
index b59aed1..d77bad7 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
+++ b/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/FileApiMethodGeneratorMojoTest.java
@@ -40,8 +40,9 @@ public class FileApiMethodGeneratorMojoTest extends AbstractGeneratorMojoTest {
         }
 
         final FileApiMethodGeneratorMojo mojo = new FileApiMethodGeneratorMojo();
-        mojo.substitutions = new Substitution[1];
-        mojo.substitutions[0] = new Substitution(".+", "(.+)", "java.util.List", "$1List");
+        mojo.substitutions = new Substitution[2];
+        mojo.substitutions[0] = new Substitution(".+", "(.+)", "java.util.List", "$1List", false);
+        mojo.substitutions[1] = new Substitution(".+", "(.+)", ".*?(\\w++)\\[\\]", "$1Array", true);
 
         configureMojo(mojo);
         mojo.proxyClass = TestProxy.class.getCanonicalName();

http://git-wip-us.apache.org/repos/asf/camel/blob/420fed02/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java b/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
index 0292111..ded66dd 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
+++ b/tooling/maven/camel-component-util-maven-plugin/src/test/java/org/apache/camel/maven/JavadocApiMethodGeneratorMojoTest.java
@@ -44,7 +44,7 @@ public class JavadocApiMethodGeneratorMojoTest extends AbstractGeneratorMojoTest
 
         // use VelocityEngine javadoc
         mojo.proxyClass = VelocityContext.class.getCanonicalName();
-        Substitution substitution = new Substitution(".*", "key", "java.lang.Object", "applicationKey");
+        Substitution substitution = new Substitution(".*", "key", "java.lang.Object", "applicationKey", false);
         mojo.substitutions = new Substitution[]{ substitution };
         mojo.excludePackages = JavadocApiMethodGeneratorMojo.DEFAULT_EXCLUDE_PACKAGES;
         mojo.excludeMethods = "clone|Current|internal|icache";