You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/09/15 15:57:08 UTC

[camel] 19/22: CAMEL-15478: api-component should generate @ApiParam with more fine grained details so we know which parameter is for which api method.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch api
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 919fca17c8a41db340229c0e7dc7d39e6c11c2e4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Sep 15 16:26:17 2020 +0200

    CAMEL-15478: api-component should generate @ApiParam with more fine grained details so we know which parameter is for which api method.
---
 .../org/apache/camel/maven/JavaSourceParser.java   | 99 ++++++++++++++++------
 .../apache/camel/component/test/OuterChild.java    | 31 +++++++
 .../org/apache/camel/component/test/TestProxy.java |  4 +
 .../apache/camel/maven/JavaSourceParserTest.java   |  2 +-
 4 files changed, 110 insertions(+), 26 deletions(-)

diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java
index 48351cc..ec95b10 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceParser.java
@@ -123,42 +123,54 @@ public class JavaSourceParser {
                 ParameterSource ps = list.get(i);
                 String name = ps.getName();
                 String type = resolveType(rootClazz, clazz, ms, ps.getType());
-                if (type.startsWith("java.lang.")) {
-                    type = type.substring(10);
-                }
-                if (ps.isVarArgs() || ps.getType().isArray()) {
-                    // the old way with javadoc did not use varargs in the signature, so lets transform this to an array style
-                    type = type + "[]";
-                }
                 if (ps.getType().isParameterized()) {
                     // for parameterized types then it can get complex if they are variables (T, T extends Foo etc)
+                    // or if there are no bounds for these types which we then can't resolve.
                     List<Type> types = ps.getType().getTypeArguments();
-                    hasTypeVariables = false;
+                    boolean bounds = false;
+                    boolean found = false;
                     for (Type t : types) {
-                        hasTypeVariables |= ms.hasTypeVariable(t.getName()) || clazz.hasTypeVariable(t.getName());
+                        if (hasTypeVariableBounds(ms, clazz, t.getName())) {
+                            bounds = true;
+                            // okay now it gets complex as we have a type like T which is a type variable and we need to resolve that into
+                            // what base class that is
+                            String tn = resolveTypeVariable(ms, clazz, t.getName());
+                            if (tn != null) {
+                                type = type.replace(t.getName(), tn);
+                                found = true;
+                            }
+                        }
+                    }
+                    if (!bounds && !found) {
+                        // argh this is getting complex, it may be T or just java.lang.String but this **** generics and roaster
+                        // does not make this easy, so let see if we can resolve each type variable
+                        boolean fqn = types.stream().allMatch(t -> {
+                            // okay lets assume its a type variable if the name is upper case only
+                            boolean upperOnly = isUpperCaseOnly(t.getName());
+                            return !upperOnly && t.getQualifiedName().indexOf('.') != -1;
+                        });
+                        if (!fqn) {
+                            // remove generics we could not resolve that even if we have bounds information
+                            bounds = true;
+                            found = false;
+                        }
                     }
-                    if (hasTypeVariables) {
-                        // okay this gets to complex then remove the generics
+                    if (bounds && !found) {
+                        // remove generics we could not resolve that even if we have bounds information
                         type = ps.getType().getQualifiedName();
                     }
                 } else if (ms.hasTypeVariable(type) || clazz.hasTypeVariable(type)) {
                     // okay now it gets complex as we have a type like T which is a type variable and we need to resolve that into
                     // what base class that is
-                    TypeVariable tv = ms.getTypeVariable(type);
-                    if (tv == null) {
-                        tv = clazz.getTypeVariable(type);
-                    }
-                    List<Type> bounds = tv.getBounds();
-                    for (Type bt : bounds) {
-                        String bn = bt.getQualifiedName();
-                        if (!type.equals(bn)) {
-                            type = bn;
-                            break;
-                        }
-                    }
+                    type = resolveTypeVariable(ms, clazz, type);
+                }
+                if (ps.isVarArgs() || ps.getType().isArray()) {
+                    // the old way with javadoc did not use varargs in the signature, so lets transform this to an array style
+                    type = type + "[]";
+                }
+                if (type.startsWith("java.lang.")) {
+                    type = type.substring(10);
                 }
-                // remove java.lang. prefix as it should not be there
-                type = type.replaceAll("java.lang.", "");
 
                 sb.append(type);
                 sb.append(" ").append(name);
@@ -186,6 +198,43 @@ public class JavaSourceParser {
         }
     }
 
+    private static boolean isUpperCaseOnly(String name) {
+        for (int i = 0; i < name.length(); i++) {
+            if (!Character.isUpperCase(name.charAt(i))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static boolean hasTypeVariableBounds(MethodSource ms, AbstractGenericCapableJavaSource clazz, String type) {
+        TypeVariable tv = ms.getTypeVariable(type);
+        if (tv == null) {
+            tv = clazz.getTypeVariable(type);
+        }
+        if (tv != null) {
+            return !tv.getBounds().isEmpty();
+        }
+        return false;
+    }
+
+    private static String resolveTypeVariable(MethodSource ms, AbstractGenericCapableJavaSource clazz, String type) {
+        TypeVariable tv = ms.getTypeVariable(type);
+        if (tv == null) {
+            tv = clazz.getTypeVariable(type);
+        }
+        if (tv != null) {
+            List<Type> bounds = tv.getBounds();
+            for (Type bt : bounds) {
+                String bn = bt.getQualifiedName();
+                if (!type.equals(bn)) {
+                    return bn;
+                }
+            }
+        }
+        return null;
+    }
+
     private static AbstractGenericCapableJavaSource findInnerClass(
             AbstractGenericCapableJavaSource rootClazz, String innerClass) {
         String[] parts = innerClass.split("\\$");
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/OuterChild.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/OuterChild.java
new file mode 100644
index 0000000..faf2e72
--- /dev/null
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/OuterChild.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.test;
+
+public class OuterChild {
+
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/TestProxy.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/TestProxy.java
index ea94d23..fd7eae4 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/TestProxy.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/component/test/TestProxy.java
@@ -104,6 +104,10 @@ public class TestProxy {
         return result.toArray(new String[result.size()]);
     }
 
+    public final <K extends OuterChild> String damnGenerics(K someStuff) {
+        return null;
+    }
+
     public final String greetInnerChild(InnerChild child) {
         return sayHi(child.getName());
     }
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java
index 52b7125..0f5a111 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java
+++ b/tooling/maven/camel-api-component-maven-plugin/src/test/java/org/apache/camel/maven/JavaSourceParserTest.java
@@ -77,7 +77,7 @@ public class JavaSourceParserTest {
         final JavaSourceParser parser = new JavaSourceParser();
 
         parser.parse(new FileInputStream("src/test/java/org/apache/camel/component/test/TestProxy.java"), null);
-        assertEquals(10, parser.getMethods().size());
+        assertEquals(11, parser.getMethods().size());
 
         // varargs is transformed to an array type as that is what works
         assertEquals(