This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.21.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit ebf4a7b2d1f03e58d396c230750710f0c06854c8 Author: Claus Ibsen <[email protected]> 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 ef75a96..57aae9b 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 @@ -238,7 +238,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)); } } } @@ -347,6 +348,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)) { @@ -355,7 +357,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; } } @@ -384,7 +386,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 @@ -395,7 +398,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)); } } } @@ -403,7 +407,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) { @@ -484,7 +488,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()); }
