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/17 07:07:47 UTC

[camel] 01/02: CAMEL-15478: Include aliases in generted api source

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 d6ff249ad00a8bb0b83abe6d7ae3ba4e540e3819
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Sep 17 08:36:39 2020 +0200

    CAMEL-15478: Include aliases in generted api source
---
 .../java/org/apache/camel/spi/ApiParams.java       |  8 ++
 .../maven/AbstractApiMethodGeneratorMojo.java      | 87 ++++------------------
 .../src/main/resources/api-endpoint-config.vm      |  2 +-
 .../main/java/org/apache/camel/spi/ApiParams.java  |  8 ++
 4 files changed, 33 insertions(+), 72 deletions(-)

diff --git a/core/camel-api/src/generated/java/org/apache/camel/spi/ApiParams.java b/core/camel-api/src/generated/java/org/apache/camel/spi/ApiParams.java
index a9be59a..037970d 100644
--- a/core/camel-api/src/generated/java/org/apache/camel/spi/ApiParams.java
+++ b/core/camel-api/src/generated/java/org/apache/camel/spi/ApiParams.java
@@ -51,4 +51,12 @@ public @interface ApiParams {
      */
     ApiMethod[] apiMethods();
 
+    /**
+     * Returns the method alias(s) of this api method. The syntax for an alias is pattern=name where pattern is a
+     * regular expression.
+     * <p/>
+     * This is used for documentation and tooling only.
+     */
+    String[] aliases() default "";
+
 }
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 38b1f92..d3b94dc 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
@@ -303,6 +303,18 @@ public abstract class AbstractApiMethodGeneratorMojo extends AbstractApiMethodBa
         }
     }
 
+    public String getAliases() {
+        StringBuilder sb = new StringBuilder();
+        sb.append("{");
+        if (!aliases.isEmpty()) {
+            StringJoiner sj = new StringJoiner(", ");
+            aliases.forEach(a -> sj.add("\"" + a.getMethodPattern() + "=" + a.getMethodAlias() + "\""));
+            sb.append(sj.toString());
+        }
+        sb.append("}");
+        return sb.toString();
+    }
+
     public static String getApiMethodsForParam(List<ApiMethodParser.ApiMethodModel> models, ApiMethodArg argument) {
         StringBuilder sb = new StringBuilder();
 
@@ -415,8 +427,6 @@ public abstract class AbstractApiMethodGeneratorMojo extends AbstractApiMethodBa
     }
 
     public String getApiMethods(List<ApiMethodParser.ApiMethodModel> models) {
-        // TODO: we should include alias information as well
-
         models.sort((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
 
         // avoid duplicate methods as we only want them listed once
@@ -488,74 +498,9 @@ public abstract class AbstractApiMethodGeneratorMojo extends AbstractApiMethodBa
             return getCanonicalName(ClassUtils.primitiveToWrapper(type));
         }
 
-        if (argument.getRawTypeArgs() != null) {
-            String fqn = argument.getRawTypeArgs();
-            // the type may use $ for classloader, so replace it back with dot
-            fqn = fqn.replace('$', '.');
-            return fqn;
-        }
-
-        // TODO: Remove below when no longer needed
-
-        // get default name prefix
-        String canonicalName = getCanonicalName(type);
-
-        final String typeArgs = argument.getTypeArgs();
-        if (typeArgs != null) {
-
-            // add generic type arguments
-            StringBuilder parameterizedType = new StringBuilder(canonicalName);
-            parameterizedType.append('<');
-
-            // Note: its ok to split, since we don't support parsing nested type arguments
-            final String[] argTypes = typeArgs.split(",");
-            final int nTypes = argTypes.length;
-            int i = 0;
-            for (String argType : argTypes) {
-
-                // javadoc sometimes contains zero-width spaces
-                if (argType.charAt(0) == '\u200b') {
-                    argType = argType.substring(1);
-                }
-
-                if ("URL".equals(argType)) {
-                    parameterizedType.append("java.net.URL");
-                } else if ("URI".equals(argType)) {
-                    parameterizedType.append("java.net.URI");
-                } else {
-                    // try loading as is first
-                    try {
-                        parameterizedType.append(getCanonicalName(getProjectClassLoader().loadClass(argType)));
-                    } catch (ClassNotFoundException e) {
-                        // try loading with default java.lang package prefix
-                        try {
-                            if (log.isDebugEnabled()) {
-                                log.debug("Could not load " + argType + ", trying to load java.lang." + argType);
-                            }
-                            parameterizedType.append(
-                                    getCanonicalName(getProjectClassLoader().loadClass("java.lang." + argType)));
-                        } catch (ClassNotFoundException e1) {
-                            parameterizedType.append("?");
-                            // if the length of the artType is 1 or 2, we think that it's variable type parameter (like T in List<T>)
-                            // not perfect solution, but should work in most of the cases
-                            if (argType.trim().length() > 2) {
-                                log.warn("Ignoring type parameters <" + typeArgs + "> for argument " + argument.getName()
-                                         + ", unable to load parametric type argument " + argType,
-                                        e1);
-                            }
-                        }
-                    }
-                }
-
-                if (++i < nTypes) {
-                    parameterizedType.append(",");
-                }
-            }
-
-            parameterizedType.append('>');
-            canonicalName = parameterizedType.toString();
-        }
-
-        return canonicalName;
+        String fqn = argument.getRawTypeArgs();
+        // the type may use $ for classloader, so replace it back with dot
+        fqn = fqn.replace('$', '.');
+        return fqn;
     }
 }
diff --git a/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-endpoint-config.vm b/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-endpoint-config.vm
index 5dd8b2f..af423f0 100644
--- a/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-endpoint-config.vm
+++ b/tooling/maven/camel-api-component-maven-plugin/src/main/resources/api-endpoint-config.vm
@@ -47,7 +47,7 @@ import org.apache.camel.spi.UriParams;
  * Camel endpoint configuration for {@link $proxyType.Name}.
  */
 @ApiParams(apiName = "$helper.getApiName($apiName)", description = "$helper.getApiDescription($apiDescription)",
-           apiMethods = $helper.getApiMethods($models))
+           apiMethods = $helper.getApiMethods($models), aliases = $helper.getAliases())
 @UriParams
 @Configurer
 public final class $configName extends ${componentName}Configuration {
diff --git a/tooling/spi-annotations/src/main/java/org/apache/camel/spi/ApiParams.java b/tooling/spi-annotations/src/main/java/org/apache/camel/spi/ApiParams.java
index a9be59a..037970d 100644
--- a/tooling/spi-annotations/src/main/java/org/apache/camel/spi/ApiParams.java
+++ b/tooling/spi-annotations/src/main/java/org/apache/camel/spi/ApiParams.java
@@ -51,4 +51,12 @@ public @interface ApiParams {
      */
     ApiMethod[] apiMethods();
 
+    /**
+     * Returns the method alias(s) of this api method. The syntax for an alias is pattern=name where pattern is a
+     * regular expression.
+     * <p/>
+     * This is used for documentation and tooling only.
+     */
+    String[] aliases() default "";
+
 }