You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/06/30 12:01:23 UTC

[camel] branch main updated (41311564fff -> 9dfa4657983)

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

orpiske pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


    from 41311564fff (chores) camel-report-maven-plugin: remove duplicated code for building error messages
     new 91554c5fb18 (chores) camel-route-parser: remove duplicated parsing-related code
     new 8dfc47b00d2 (chores) camel-route-parser: remove duplicated expression-extracting code
     new 9dfa4657983 (chores) camel-route-parser: remove duplicated code for finding the root method the Java DSL parser

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/camel/parser/RouteBuilderParser.java    | 145 +++++++++------------
 .../camel/parser/helper/CamelJavaParserHelper.java |  45 +++----
 .../helper/CamelJavaRestDslParserHelper.java       |  26 +---
 3 files changed, 85 insertions(+), 131 deletions(-)


[camel] 01/03: (chores) camel-route-parser: remove duplicated parsing-related code

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 91554c5fb18b16966e05f7883ae43d1c01f1ca41
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Thu Jun 30 13:02:11 2022 +0200

    (chores) camel-route-parser: remove duplicated parsing-related code
---
 .../apache/camel/parser/RouteBuilderParser.java    | 145 +++++++++------------
 1 file changed, 59 insertions(+), 86 deletions(-)

diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index 098532aff3f..18fe868d61e 100644
--- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -61,17 +61,7 @@ public final class RouteBuilderParser {
             JavaClassSource clazz, String fullyQualifiedFileName,
             boolean includeInlinedRouteBuilders) {
 
-        List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
-        MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
-        if (method != null) {
-            methods.add(method);
-        }
-        if (includeInlinedRouteBuilders) {
-            List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
-            if (!inlinedMethods.isEmpty()) {
-                methods.addAll(inlinedMethods);
-            }
-        }
+        List<MethodSource<JavaClassSource>> methods = findAllConfigureMethods(clazz, includeInlinedRouteBuilders);
 
         CamelJavaTreeParserHelper parser = new CamelJavaTreeParserHelper();
         List<CamelNodeDetails> list = new ArrayList<>();
@@ -146,10 +136,7 @@ public final class RouteBuilderParser {
             if (!Strings.isNullOrEmpty(uri) && findEndpointByUri(endpoints, uri) == null) {
 
                 // we only want the relative dir name from the
-                String fileName = fullyQualifiedFileName;
-                if (fileName.startsWith(baseDir)) {
-                    fileName = fileName.substring(baseDir.length() + 1);
-                }
+                String fileName = parseFileName(baseDir, fullyQualifiedFileName);
                 String id = field.getName();
 
                 CamelEndpointDetails detail = new CamelEndpointDetails();
@@ -187,17 +174,7 @@ public final class RouteBuilderParser {
         }
 
         // find all the configure methods
-        List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
-        MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
-        if (method != null) {
-            methods.add(method);
-        }
-        if (includeInlinedRouteBuilders) {
-            List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
-            if (!inlinedMethods.isEmpty()) {
-                methods.addAll(inlinedMethods);
-            }
-        }
+        List<MethodSource<JavaClassSource>> methods = findAllConfigureMethods(clazz, includeInlinedRouteBuilders);
 
         // look if any of these fields are used in the route only as consumer or producer, as then we can
         // determine this to ensure when we edit the endpoint we should only the options accordingly
@@ -210,31 +187,9 @@ public final class RouteBuilderParser {
                         unparsable.add(result.getElement());
                     }
                 } else {
-                    String fileName = fullyQualifiedFileName;
-                    if (fileName.startsWith(baseDir)) {
-                        fileName = fileName.substring(baseDir.length() + 1);
-                    }
+                    String fileName = parseFileName(baseDir, fullyQualifiedFileName);
 
-                    CamelEndpointDetails detail = new CamelEndpointDetails();
-                    detail.setFileName(fileName);
-                    detail.setClassName(clazz.getQualifiedName());
-                    detail.setMethodName(configureMethod.getName());
-                    detail.setEndpointInstance(null);
-                    detail.setEndpointUri(result.getElement());
-                    int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
-                    if (line > -1) {
-                        detail.setLineNumber("" + line);
-                    }
-                    int lineEnd = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
-                    if (lineEnd > -1) {
-                        detail.setLineNumberEnd("" + lineEnd);
-                    }
-                    detail.setAbsolutePosition(result.getPosition());
-                    int linePos = findLinePosition(clazz.toUnformattedString(), result.getPosition());
-                    if (linePos > -1) {
-                        detail.setLinePosition(linePos);
-                    }
-                    detail.setEndpointComponentName(endpointComponentName(result.getElement()));
+                    CamelEndpointDetails detail = buildCamelEndpointDetails(clazz, configureMethod, result, fileName);
                     detail.setConsumerOnly(true);
                     detail.setProducerOnly(false);
                     endpoints.add(detail);
@@ -250,31 +205,9 @@ public final class RouteBuilderParser {
                 } else {
                     // the same endpoint uri may be used in multiple places in the same route
                     // so we should maybe add all of them
-                    String fileName = fullyQualifiedFileName;
-                    if (fileName.startsWith(baseDir)) {
-                        fileName = fileName.substring(baseDir.length() + 1);
-                    }
+                    String fileName = parseFileName(baseDir, fullyQualifiedFileName);
 
-                    CamelEndpointDetails detail = new CamelEndpointDetails();
-                    detail.setFileName(fileName);
-                    detail.setClassName(clazz.getQualifiedName());
-                    detail.setMethodName(configureMethod.getName());
-                    detail.setEndpointInstance(null);
-                    detail.setEndpointUri(result.getElement());
-                    int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
-                    if (line > -1) {
-                        detail.setLineNumber("" + line);
-                    }
-                    int endLine = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
-                    if (endLine > -1) {
-                        detail.setLineNumberEnd("" + endLine);
-                    }
-                    detail.setAbsolutePosition(result.getPosition());
-                    int linePos = findLinePosition(clazz.toUnformattedString(), result.getPosition());
-                    if (linePos > -1) {
-                        detail.setLinePosition(linePos);
-                    }
-                    detail.setEndpointComponentName(endpointComponentName(result.getElement()));
+                    CamelEndpointDetails detail = buildCamelEndpointDetails(clazz, configureMethod, result, fileName);
                     detail.setConsumerOnly(false);
                     detail.setProducerOnly(true);
                     endpoints.add(detail);
@@ -283,6 +216,55 @@ public final class RouteBuilderParser {
         }
     }
 
+    private static List<MethodSource<JavaClassSource>> findAllConfigureMethods(
+            JavaClassSource clazz, boolean includeInlinedRouteBuilders) {
+        List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
+        MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
+        if (method != null) {
+            methods.add(method);
+        }
+        if (includeInlinedRouteBuilders) {
+            List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
+            if (!inlinedMethods.isEmpty()) {
+                methods.addAll(inlinedMethods);
+            }
+        }
+        return methods;
+    }
+
+    private static CamelEndpointDetails buildCamelEndpointDetails(
+            JavaClassSource clazz, MethodSource<JavaClassSource> configureMethod, ParserResult result, String fileName) {
+        CamelEndpointDetails detail = new CamelEndpointDetails();
+        detail.setFileName(fileName);
+        detail.setClassName(clazz.getQualifiedName());
+        detail.setMethodName(configureMethod.getName());
+        detail.setEndpointInstance(null);
+        detail.setEndpointUri(result.getElement());
+        int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
+        if (line > -1) {
+            detail.setLineNumber("" + line);
+        }
+        int lineEnd = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
+        if (lineEnd > -1) {
+            detail.setLineNumberEnd("" + lineEnd);
+        }
+        detail.setAbsolutePosition(result.getPosition());
+        int linePos = findLinePosition(clazz.toUnformattedString(), result.getPosition());
+        if (linePos > -1) {
+            detail.setLinePosition(linePos);
+        }
+        detail.setEndpointComponentName(endpointComponentName(result.getElement()));
+        return detail;
+    }
+
+    private static String parseFileName(String baseDir, String fullyQualifiedFileName) {
+        String fileName = fullyQualifiedFileName;
+        if (fileName.startsWith(baseDir)) {
+            fileName = fileName.substring(baseDir.length() + 1);
+        }
+        return fileName;
+    }
+
     /**
      * Parses the java source class to discover Camel simple expressions.
      *
@@ -299,10 +281,7 @@ public final class RouteBuilderParser {
             List<ParserResult> expressions = CamelJavaParserHelper.parseCamelLanguageExpressions(method, "simple");
             for (ParserResult result : expressions) {
                 if (result.isParsed()) {
-                    String fileName = fullyQualifiedFileName;
-                    if (fileName.startsWith(baseDir)) {
-                        fileName = fileName.substring(baseDir.length() + 1);
-                    }
+                    String fileName = parseFileName(baseDir, fullyQualifiedFileName);
 
                     CamelSimpleExpressionDetails detail = new CamelSimpleExpressionDetails();
                     detail.setFileName(fileName);
@@ -350,10 +329,7 @@ public final class RouteBuilderParser {
             List<ParserResult> expressions = CamelJavaParserHelper.parseCamelLanguageExpressions(method, "csimple");
             for (ParserResult result : expressions) {
                 if (result.isParsed()) {
-                    String fileName = fullyQualifiedFileName;
-                    if (fileName.startsWith(baseDir)) {
-                        fileName = fileName.substring(baseDir.length() + 1);
-                    }
+                    String fileName = parseFileName(baseDir, fullyQualifiedFileName);
 
                     CamelCSimpleExpressionDetails detail = new CamelCSimpleExpressionDetails();
                     detail.setFileName(fileName);
@@ -403,10 +379,7 @@ public final class RouteBuilderParser {
             for (ParserResult result : expressions) {
                 // route ids is assigned in java dsl using the routeId method
                 if (result.isParsed()) {
-                    String fileName = fullyQualifiedFileName;
-                    if (fileName.startsWith(baseDir)) {
-                        fileName = fileName.substring(baseDir.length() + 1);
-                    }
+                    String fileName = parseFileName(baseDir, fullyQualifiedFileName);
 
                     CamelRouteDetails detail = new CamelRouteDetails();
                     detail.setFileName(fileName);


[camel] 02/03: (chores) camel-route-parser: remove duplicated expression-extracting code

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8dfc47b00d234ddd9fe988d8099130dab783574a
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Thu Jun 30 13:08:35 2022 +0200

    (chores) camel-route-parser: remove duplicated expression-extracting code
---
 .../camel/parser/helper/CamelJavaParserHelper.java | 45 +++++++++-------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java
index 30d4df5903b..a214cc93a60 100644
--- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java
+++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java
@@ -375,19 +375,7 @@ public final class CamelJavaParserHelper {
                     annotation = field.getAnnotation("org.apache.camel.EndpointInject");
                 }
                 if (annotation != null) {
-                    Expression exp = (Expression) annotation.getInternal();
-                    if (exp instanceof SingleMemberAnnotation) {
-                        exp = ((SingleMemberAnnotation) exp).getValue();
-                    } else if (exp instanceof NormalAnnotation) {
-                        List values = ((NormalAnnotation) exp).values();
-                        for (Object value : values) {
-                            MemberValuePair pair = (MemberValuePair) value;
-                            if ("uri".equals(pair.getName().toString())) {
-                                exp = pair.getValue();
-                                break;
-                            }
-                        }
-                    }
+                    Expression exp = extractExpression(annotation.getInternal());
                     String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
                     if (!Strings.isNullOrEmpty(uri)) {
                         int position = ((SimpleName) arg).getStartPosition();
@@ -415,6 +403,23 @@ public final class CamelJavaParserHelper {
         uris.add(new ParserResult(node, -1, -1, arg.toString(), false));
     }
 
+    private static Expression extractExpression(Object annotation) {
+        Expression exp = (Expression) annotation;
+        if (exp instanceof SingleMemberAnnotation) {
+            exp = ((SingleMemberAnnotation) exp).getValue();
+        } else if (exp instanceof NormalAnnotation) {
+            List values = ((NormalAnnotation) exp).values();
+            for (Object value : values) {
+                MemberValuePair pair = (MemberValuePair) value;
+                if ("uri".equals(pair.getName().toString())) {
+                    exp = pair.getValue();
+                    break;
+                }
+            }
+        }
+        return exp;
+    }
+
     public static List<ParserResult> parseCamelLanguageExpressions(MethodSource<JavaClassSource> method, String language) {
         List<ParserResult> answer = new ArrayList<>();
 
@@ -630,19 +635,7 @@ public final class CamelJavaParserHelper {
                         boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName())
                                 || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
                         if (valid) {
-                            Expression exp = (Expression) ann.getInternal();
-                            if (exp instanceof SingleMemberAnnotation) {
-                                exp = ((SingleMemberAnnotation) exp).getValue();
-                            } else if (exp instanceof NormalAnnotation) {
-                                List values = ((NormalAnnotation) exp).values();
-                                for (Object value : values) {
-                                    MemberValuePair pair = (MemberValuePair) value;
-                                    if ("uri".equals(pair.getName().toString())) {
-                                        exp = pair.getValue();
-                                        break;
-                                    }
-                                }
-                            }
+                            Expression exp = extractExpression(ann.getInternal());
                             if (exp != null) {
                                 return getLiteralValue(clazz, block, exp);
                             }


[camel] 03/03: (chores) camel-route-parser: remove duplicated code for finding the root method the Java DSL parser

Posted by or...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 9dfa4657983d8de0751f5b0fa47a5e386fa5d8e8
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Thu Jun 30 13:13:35 2022 +0200

    (chores) camel-route-parser: remove duplicated code for finding the root method the Java DSL parser
---
 .../helper/CamelJavaRestDslParserHelper.java       | 26 ++++++----------------
 1 file changed, 7 insertions(+), 19 deletions(-)

diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
index 08a7d24b525..e4d8e7a14b9 100644
--- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
+++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaRestDslParserHelper.java
@@ -155,30 +155,13 @@ public final class CamelJavaRestDslParserHelper {
     }
 
     private boolean isRestConfiguration(Expression exp) {
-        String rootMethodName = null;
-
-        // find out if this is from a Camel route (eg from, route etc.)
-        Expression sub = exp;
-        while (sub instanceof MethodInvocation) {
-            sub = ((MethodInvocation) sub).getExpression();
-            if (sub instanceof MethodInvocation) {
-                Expression parent = ((MethodInvocation) sub).getExpression();
-                if (parent == null) {
-                    break;
-                }
-            }
-        }
-        if (sub instanceof MethodInvocation) {
-            rootMethodName = ((MethodInvocation) sub).getName().getIdentifier();
-        } else if (sub instanceof SimpleName) {
-            rootMethodName = ((SimpleName) sub).getIdentifier();
-        }
+        final String rootMethodName = findRootMethodName(exp);
 
         // must be from rest configuration
         return "restConfiguration".equals(rootMethodName);
     }
 
-    private boolean isRest(Expression exp) {
+    private String findRootMethodName(Expression exp) {
         String rootMethodName = null;
 
         // find out if this is from a Camel route (eg from, route etc.)
@@ -197,6 +180,11 @@ public final class CamelJavaRestDslParserHelper {
         } else if (sub instanceof SimpleName) {
             rootMethodName = ((SimpleName) sub).getIdentifier();
         }
+        return rootMethodName;
+    }
+
+    private boolean isRest(Expression exp) {
+        final String rootMethodName = findRootMethodName(exp);
 
         // must be from rest
         return "rest".equals(rootMethodName);