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 2018/07/13 07:18:46 UTC

[camel] branch camel-2.22.x updated (b8b8333 -> 012b2ea)

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

davsclaus pushed a change to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from b8b8333  CAMEL-12653: Avoid NPE in case of null attributes
     new b724a68  CAMEL-12639: camel-route-parser should obtain line number from class content instead of reading from file, in-case you parse a class that has not been saved to file etc.
     new 012b2ea  CAMEL-12639: camel-route-parser should include line number end for Java DSL.

The 2 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:
 .../java/org/apache/camel/parser/ParserResult.java | 15 +++++++--
 .../apache/camel/parser/RouteBuilderParser.java    | 36 +++++++++++++++++-----
 .../camel/parser/helper/CamelJavaParserHelper.java | 17 ++++++----
 .../camel/parser/java/MyCdiRouteBuilder.java       |  4 ++-
 .../parser/java/RoasterEndpointInjectTest.java     | 12 ++++++--
 5 files changed, 65 insertions(+), 19 deletions(-)


[camel] 02/02: CAMEL-12639: camel-route-parser should include line number end for Java DSL.

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

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

commit 012b2eaf01f0a2173f57bb7324fc1373f173d937
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jul 13 09:17:20 2018 +0200

    CAMEL-12639: camel-route-parser should include line number end for Java DSL.
---
 .../java/org/apache/camel/parser/ParserResult.java  | 15 ++++++++++++---
 .../org/apache/camel/parser/RouteBuilderParser.java | 21 +++++++++++++++++++++
 .../camel/parser/helper/CamelJavaParserHelper.java  | 17 +++++++++++------
 .../apache/camel/parser/java/MyCdiRouteBuilder.java |  4 +++-
 .../parser/java/RoasterEndpointInjectTest.java      | 12 ++++++++++--
 5 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/ParserResult.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/ParserResult.java
index 71088d2..1cdb89b 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/ParserResult.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/ParserResult.java
@@ -24,16 +24,18 @@ public class ParserResult {
     private final String node;
     private boolean parsed;
     private int position;
+    private int length;
     private String element;
     private Boolean predicate;
 
-    public ParserResult(String node, int position, String element) {
-        this(node, position, element, true);
+    public ParserResult(String node, int position, int length, String element) {
+        this(node, position, length, element, true);
     }
 
-    public ParserResult(String node, int position, String element, boolean parsed) {
+    public ParserResult(String node, int position, int length, String element, boolean parsed) {
         this.node = node;
         this.position = position;
+        this.length = length;
         this.element = element;
         this.parsed = parsed;
     }
@@ -46,6 +48,13 @@ public class ParserResult {
     }
 
     /**
+     * Length of node in the source code (not line based).
+     */
+    public int getLength() {
+        return length;
+    }
+
+    /**
      * The element such as a Camel endpoint uri
      */
     public String getElement() {
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index d56fe8f..b5290df 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -162,10 +162,15 @@ public final class RouteBuilderParser {
                 // find position of field/expression
                 if (internal instanceof ASTNode) {
                     int pos = ((ASTNode) internal).getStartPosition();
+                    int len = ((ASTNode) internal).getLength();
                     int line = findLineNumber(clazz.toUnformattedString(), pos);
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
+                    int endLine = findLineNumber(clazz.toUnformattedString(), pos + len);
+                    if (endLine > -1) {
+                        detail.setLineNumberEnd("" + endLine);
+                    }
                 }
                 // we do not know if this field is used as consumer or producer only, but we try
                 // to find out by scanning the route in the configure method below
@@ -217,6 +222,10 @@ public final class RouteBuilderParser {
                         if (line > -1) {
                             detail.setLineNumber("" + line);
                         }
+                        int lineEnd = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
+                        if (lineEnd > -1) {
+                            detail.setLineNumberEnd("" + lineEnd);
+                        }
                         detail.setEndpointComponentName(endpointComponentName(result.getElement()));
                         detail.setConsumerOnly(true);
                         detail.setProducerOnly(false);
@@ -260,6 +269,10 @@ public final class RouteBuilderParser {
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
+                    int endLine = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
+                    if (endLine > -1) {
+                        detail.setLineNumberEnd("" + endLine);
+                    }
                     detail.setEndpointComponentName(endpointComponentName(result.getElement()));
                     detail.setConsumerOnly(false);
                     detail.setProducerOnly(true);
@@ -298,6 +311,10 @@ public final class RouteBuilderParser {
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
+                    int endLine = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
+                    if (endLine > -1) {
+                        detail.setLineNumberEnd("" + endLine);
+                    }
                     detail.setSimple(result.getElement());
 
                     boolean predicate = result.getPredicate() != null ? result.getPredicate() : false;
@@ -341,6 +358,10 @@ public final class RouteBuilderParser {
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
+                    int endLine = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
+                    if (endLine > -1) {
+                        detail.setLineNumberEnd("" + endLine);
+                    }
                     detail.setRouteId(result.getElement());
 
                     routes.add(detail);
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java
index 95338cf..89b665c 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java
@@ -235,7 +235,8 @@ public final class CamelJavaParserHelper {
                             String routeId = getLiteralValue(clazz, block, (Expression) arg);
                             if (!Strings.isBlank(routeId)) {
                                 int position = ((Expression) arg).getStartPosition();
-                                uris.add(new ParserResult(name, position, routeId));
+                                int len = ((Expression) arg).getLength();
+                                uris.add(new ParserResult(name, position, len, routeId));
                             }
                         }
                     }
@@ -344,6 +345,7 @@ public final class CamelJavaParserHelper {
             String uri = getLiteralValue(clazz, block, (Expression) arg);
             if (!Strings.isBlank(uri)) {
                 int position = ((Expression) arg).getStartPosition();
+                int len = ((Expression) arg).getLength();
 
                 // if the node is fromF or toF, then replace all %X with {{%X}} as we cannot parse that value
                 if ("fromF".equals(node) || "toF".equals(node)) {
@@ -352,7 +354,7 @@ public final class CamelJavaParserHelper {
                     uri = uri.replaceAll("\\%b", "\\{\\{\\%b\\}\\}");
                 }
 
-                uris.add(new ParserResult(node, position, uri));
+                uris.add(new ParserResult(node, position, len, uri));
                 return;
             }
         }
@@ -381,7 +383,8 @@ public final class CamelJavaParserHelper {
                     String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
                     if (!Strings.isBlank(uri)) {
                         int position = ((SimpleName) arg).getStartPosition();
-                        uris.add(new ParserResult(node, position, uri));
+                        int len = ((SimpleName) arg).getLength();
+                        uris.add(new ParserResult(node, position, len, uri));
                     }
                 } else {
                     // the field may be initialized using variables, so we need to evaluate those expressions
@@ -392,7 +395,8 @@ public final class CamelJavaParserHelper {
                         if (!Strings.isBlank(uri)) {
                             // we want the position of the field, and not in the route
                             int position = ((VariableDeclaration) fi).getStartPosition();
-                            uris.add(new ParserResult(node, position, uri));
+                            int len = ((VariableDeclaration) fi).getLength();
+                            uris.add(new ParserResult(node, position, len, uri));
                         }
                     }
                 }
@@ -400,7 +404,7 @@ public final class CamelJavaParserHelper {
         }
 
         // cannot parse it so add a failure
-        uris.add(new ParserResult(node, -1, arg.toString(), false));
+        uris.add(new ParserResult(node, -1, -1, arg.toString(), false));
     }
 
     public static List<ParserResult> parseCamelSimpleExpressions(MethodSource<JavaClassSource> method) {
@@ -481,7 +485,8 @@ public final class CamelJavaParserHelper {
                     }
 
                     int position = ((Expression) arg).getStartPosition();
-                    ParserResult result = new ParserResult(node, position, simple);
+                    int len = ((Expression) arg).getLength();
+                    ParserResult result = new ParserResult(node, position, len, simple);
                     result.setPredicate(predicate);
                     expressions.add(result);
                 }
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyCdiRouteBuilder.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyCdiRouteBuilder.java
index 2bf3e78..2ecf367 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyCdiRouteBuilder.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyCdiRouteBuilder.java
@@ -41,6 +41,8 @@ public class MyCdiRouteBuilder extends RouteBuilder {
         from(inputEndpoint)
             .log("I was here")
             .to(loga)
-            .to(mynetty);
+            .to(mynetty)
+            .to("mock:foo"
+                + "?retainFirst=1");
     }
 }
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterEndpointInjectTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterEndpointInjectTest.java
index 8ffb23b..8e872fd 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterEndpointInjectTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterEndpointInjectTest.java
@@ -47,12 +47,20 @@ public class RoasterEndpointInjectTest {
 
         Assert.assertEquals("timer:foo?period=4999", details.get(0).getEndpointUri());
         Assert.assertEquals("28", details.get(0).getLineNumber());
+        Assert.assertEquals("28", details.get(0).getLineNumberEnd());
 
         Assert.assertEquals("log:a", details.get(1).getEndpointUri());
         Assert.assertEquals("32", details.get(1).getLineNumber());
+        Assert.assertEquals("32", details.get(1).getLineNumberEnd());
 
         Assert.assertEquals("netty4-http:http:someserver:80/hello", details.get(2).getEndpointUri());
         Assert.assertEquals("36", details.get(2).getLineNumber());
+        Assert.assertEquals("36", details.get(2).getLineNumberEnd());
+
+        // spans 2 lines
+        Assert.assertEquals("mock:foo?retainFirst=1", details.get(5).getEndpointUri());
+        Assert.assertEquals("45", details.get(5).getLineNumber());
+        Assert.assertEquals("46", details.get(5).getLineNumberEnd());
 
         List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
         for (ParserResult result : list) {
@@ -64,9 +72,9 @@ public class RoasterEndpointInjectTest {
         for (ParserResult result : list) {
             LOG.info("Producer: " + result.getElement());
         }
-        Assert.assertEquals(2, list.size());
+        Assert.assertEquals(3, list.size());
 
-        Assert.assertEquals(5, details.size());
+        Assert.assertEquals(6, details.size());
         Assert.assertEquals("log:a", details.get(3).getEndpointUri());
     }
 


[camel] 01/02: CAMEL-12639: camel-route-parser should obtain line number from class content instead of reading from file, in-case you parse a class that has not been saved to file etc.

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

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

commit b724a688554cd2226951a676da4e2398c6768fb6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jul 13 09:07:44 2018 +0200

    CAMEL-12639: camel-route-parser should obtain line number from class content instead of reading from file, in-case you parse a class that has not been saved to file etc.
---
 .../java/org/apache/camel/parser/RouteBuilderParser.java  | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index 703d54e..d56fe8f 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -19,6 +19,7 @@ package org.apache.camel.parser;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
+import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -161,7 +162,7 @@ public final class RouteBuilderParser {
                 // find position of field/expression
                 if (internal instanceof ASTNode) {
                     int pos = ((ASTNode) internal).getStartPosition();
-                    int line = findLineNumber(fullyQualifiedFileName, pos);
+                    int line = findLineNumber(clazz.toUnformattedString(), pos);
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
@@ -212,7 +213,7 @@ public final class RouteBuilderParser {
                         detail.setMethodName(configureMethod.getName());
                         detail.setEndpointInstance(null);
                         detail.setEndpointUri(result.getElement());
-                        int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
+                        int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
                         if (line > -1) {
                             detail.setLineNumber("" + line);
                         }
@@ -255,7 +256,7 @@ public final class RouteBuilderParser {
                     detail.setMethodName(configureMethod.getName());
                     detail.setEndpointInstance(null);
                     detail.setEndpointUri(result.getElement());
-                    int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
+                    int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
@@ -293,7 +294,7 @@ public final class RouteBuilderParser {
                     detail.setFileName(fileName);
                     detail.setClassName(clazz.getQualifiedName());
                     detail.setMethodName("configure");
-                    int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
+                    int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
@@ -336,7 +337,7 @@ public final class RouteBuilderParser {
                     detail.setFileName(fileName);
                     detail.setClassName(clazz.getQualifiedName());
                     detail.setMethodName("configure");
-                    int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
+                    int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
                     if (line > -1) {
                         detail.setLineNumber("" + line);
                     }
@@ -357,12 +358,12 @@ public final class RouteBuilderParser {
         return null;
     }
 
-    private static int findLineNumber(String fullyQualifiedFileName, int position) {
+    private static int findLineNumber(String content, int position) {
         int lines = 0;
 
         try {
             int current = 0;
-            try (BufferedReader br = new BufferedReader(new FileReader(new File(fullyQualifiedFileName)))) {
+            try (BufferedReader br = new BufferedReader(new StringReader(content))) {
                 String line;
                 while ((line = br.readLine()) != null) {
                     lines++;