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
The following commit(s) were added to refs/heads/main by this push:
new 7cebb93bee5 (chores) camel-route-parser: break complex methods
7cebb93bee5 is described below
commit 7cebb93bee5d1081e08bc8bbada3c08eef07025f
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Fri Apr 26 14:40:27 2024 +0200
(chores) camel-route-parser: break complex methods
---
.../apache/camel/parser/RouteBuilderParser.java | 29 ++++---
.../org/apache/camel/parser/XmlRouteParser.java | 84 ++++++++++++--------
.../camel/parser/helper/CamelJavaParserHelper.java | 79 +++++++++++--------
.../helper/CamelJavaRestDslParserHelper.java | 91 +++++++++++++---------
4 files changed, 173 insertions(+), 110 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 c90e1374f6e..f9843e3c961 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
@@ -209,21 +209,13 @@ public final class RouteBuilderParser {
String uri;
Expression exp;
for (Annotation<JavaClassSource> ann : field.getAnnotations()) {
- boolean valid =
"org.apache.camel.EndpointInject".equals(ann.getQualifiedName())
- ||
"org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
+ boolean valid = isValid(ann);
if (valid) {
exp = (Expression) ann.getInternal();
if (exp instanceof SingleMemberAnnotation
singleMemberAnnotation) {
exp = singleMemberAnnotation.getValue();
} else if (exp instanceof NormalAnnotation normalAnnotation) {
- List<?> values = normalAnnotation.values();
- for (Object value : values) {
- MemberValuePair pair = (MemberValuePair) value;
- if ("uri".equals(pair.getName().toString())) {
- exp = pair.getValue();
- break;
- }
- }
+ exp = evalNormalAnnotation(normalAnnotation, exp);
}
uri = CamelJavaParserHelper.getLiteralValue(clazz, null, exp);
endpointUri = new EndpointUri(uri, exp);
@@ -232,6 +224,23 @@ public final class RouteBuilderParser {
return endpointUri;
}
+ private static Expression evalNormalAnnotation(NormalAnnotation
normalAnnotation, Expression exp) {
+ List<?> values = normalAnnotation.values();
+ for (Object value : values) {
+ MemberValuePair pair = (MemberValuePair) value;
+ if ("uri".equals(pair.getName().toString())) {
+ exp = pair.getValue();
+ break;
+ }
+ }
+ return exp;
+ }
+
+ private static boolean isValid(Annotation<JavaClassSource> ann) {
+ return "org.apache.camel.EndpointInject".equals(ann.getQualifiedName())
+ || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
+ }
+
static List<MethodSource<JavaClassSource>> findAllConfigureMethods(
JavaClassSource clazz, boolean includeInlinedRouteBuilders) {
List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
diff --git
a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
index 740b27a4a61..5e219140b51 100644
---
a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
+++
b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
@@ -132,43 +132,65 @@ public final class XmlRouteParser {
uri = trimEndpointUri(uri);
}
if (!Strings.isNullOrEmpty(uri)) {
- String id = getSafeAttribute(node, "id");
- String lineNumber = (String)
node.getUserData(XmlLineNumberParser.LINE_NUMBER);
- String lineNumberEnd = (String)
node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
-
- // we only want the relative dir name from the resource
directory, eg META-INF/spring/foo.xml
- String fileName = getFileName(baseDir,
fullyQualifiedFileName);
-
- boolean consumerOnly = false;
- boolean producerOnly = false;
- String nodeName = node.getNodeName();
- if ("from".equals(nodeName) ||
"pollEnrich".equals(nodeName)) {
- consumerOnly = true;
- } else if ("to".equals(nodeName) ||
"enrich".equals(nodeName) || "wireTap".equals(nodeName)) {
- producerOnly = true;
- }
-
- CamelEndpointDetails detail = new CamelEndpointDetails();
- detail.setFileName(fileName);
- detail.setLineNumber(lineNumber);
- detail.setLineNumberEnd(lineNumberEnd);
-
- String column = (String)
node.getUserData(XmlLineNumberParser.COLUMN_NUMBER);
- if (column != null) {
- detail.setLinePosition(Integer.parseInt(column));
- }
-
- detail.setEndpointInstance(id);
- detail.setEndpointUri(uri);
-
detail.setEndpointComponentName(endpointComponentName(uri));
- detail.setConsumerOnly(consumerOnly);
- detail.setProducerOnly(producerOnly);
+ final CamelEndpointDetails detail =
+ toCamelEndpointDetails(baseDir,
fullyQualifiedFileName, node, uri);
endpoints.add(detail);
}
}
}
}
+ private static CamelEndpointDetails toCamelEndpointDetails(
+ String baseDir, String fullyQualifiedFileName, Node node, String
uri) {
+ String id = getSafeAttribute(node, "id");
+ String lineNumber = (String)
node.getUserData(XmlLineNumberParser.LINE_NUMBER);
+ String lineNumberEnd = (String)
node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
+
+ // we only want the relative dir name from the resource directory, eg
META-INF/spring/foo.xml
+ String fileName = getFileName(baseDir, fullyQualifiedFileName);
+
+ boolean consumerOnly = false;
+ boolean producerOnly = false;
+ String nodeName = node.getNodeName();
+ if (isConsumerOnly(nodeName)) {
+ consumerOnly = true;
+ } else if (isProducerOnly(nodeName)) {
+ producerOnly = true;
+ }
+
+ return toEndpointDetails(node, fileName, lineNumber, lineNumberEnd,
id, uri, consumerOnly,
+ producerOnly);
+ }
+
+ private static boolean isProducerOnly(String nodeName) {
+ return "to".equals(nodeName) || "enrich".equals(nodeName) ||
"wireTap".equals(nodeName);
+ }
+
+ private static boolean isConsumerOnly(String nodeName) {
+ return "from".equals(nodeName) || "pollEnrich".equals(nodeName);
+ }
+
+ private static CamelEndpointDetails toEndpointDetails(
+ Node node, String fileName, String lineNumber, String
lineNumberEnd, String id, String uri, boolean consumerOnly,
+ boolean producerOnly) {
+ CamelEndpointDetails detail = new CamelEndpointDetails();
+ detail.setFileName(fileName);
+ detail.setLineNumber(lineNumber);
+ detail.setLineNumberEnd(lineNumberEnd);
+
+ String column = (String)
node.getUserData(XmlLineNumberParser.COLUMN_NUMBER);
+ if (column != null) {
+ detail.setLinePosition(Integer.parseInt(column));
+ }
+
+ detail.setEndpointInstance(id);
+ detail.setEndpointUri(uri);
+ detail.setEndpointComponentName(endpointComponentName(uri));
+ detail.setConsumerOnly(consumerOnly);
+ detail.setProducerOnly(producerOnly);
+ return detail;
+ }
+
/**
* Parses the XML source to discover Camel simple languages.
*
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 a7c9414b419..4485d4f875c 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
@@ -116,26 +116,9 @@ public final class CamelJavaParserHelper {
if (block != null) {
List<?> statements = block.statements();
for (Object statement : statements) {
- Statement stmt = (Statement) statement;
- Expression exp = null;
- if (stmt instanceof ReturnStatement rs) {
- exp = rs.getExpression();
- } else if (stmt instanceof ExpressionStatement es) {
- exp = es.getExpression();
- if (exp instanceof MethodInvocation mi) {
- for (Object arg : mi.arguments()) {
- if (arg instanceof ClassInstanceCreation) {
- exp = (Expression) arg;
- break;
- }
- }
- }
- }
+ final Expression exp = getExpression((Statement) statement);
if (exp instanceof ClassInstanceCreation cic) {
- boolean isRouteBuilder = false;
- if (cic.getType() instanceof SimpleType st) {
- isRouteBuilder =
"RouteBuilder".equals(st.getName().toString());
- }
+ final boolean isRouteBuilder = isRouteBuilderCheck(cic);
if (isRouteBuilder && cic.getAnonymousClassDeclaration()
!= null) {
List<?> body =
cic.getAnonymousClassDeclaration().bodyDeclarations();
for (Object line : body) {
@@ -153,6 +136,33 @@ public final class CamelJavaParserHelper {
return null;
}
+ private static boolean isRouteBuilderCheck(ClassInstanceCreation cic) {
+ boolean isRouteBuilder = false;
+ if (cic.getType() instanceof SimpleType st) {
+ isRouteBuilder = "RouteBuilder".equals(st.getName().toString());
+ }
+ return isRouteBuilder;
+ }
+
+ private static Expression getExpression(Statement statement) {
+ Statement stmt = statement;
+ Expression exp = null;
+ if (stmt instanceof ReturnStatement rs) {
+ exp = rs.getExpression();
+ } else if (stmt instanceof ExpressionStatement es) {
+ exp = es.getExpression();
+ if (exp instanceof MethodInvocation mi) {
+ for (Object arg : mi.arguments()) {
+ if (arg instanceof ClassInstanceCreation) {
+ exp = (Expression) arg;
+ break;
+ }
+ }
+ }
+ }
+ return exp;
+ }
+
public static List<ParserResult>
parseCamelRouteIds(MethodSource<JavaClassSource> method) {
return doParseCamelUris(method, true, false, true, false, true);
}
@@ -220,19 +230,7 @@ public final class CamelJavaParserHelper {
if (routeIdsOnly) {
// include route id for consumers
if ("routeId".equals(name)) {
- List<?> args = mi.arguments();
- if (args != null) {
- for (Object arg : args) {
- if (isValidArgument(arg)) {
- String routeId = getLiteralValue(clazz, block,
(Expression) arg);
- if (!Strings.isNullOrEmpty(routeId)) {
- int position = ((Expression)
arg).getStartPosition();
- int len = ((Expression) arg).getLength();
- uris.add(new ParserResult(name, position, len,
routeId));
- }
- }
- }
- }
+ addRouteId(clazz, block, mi, uris, name);
}
// we only want route ids so return here
return;
@@ -272,6 +270,23 @@ public final class CamelJavaParserHelper {
}
}
+ private static void addRouteId(
+ JavaClassSource clazz, Block block, MethodInvocation mi,
List<ParserResult> uris, String name) {
+ List<?> args = mi.arguments();
+ if (args != null) {
+ for (Object arg : args) {
+ if (isValidArgument(arg)) {
+ String routeId = getLiteralValue(clazz, block,
(Expression) arg);
+ if (!Strings.isNullOrEmpty(routeId)) {
+ int position = ((Expression) arg).getStartPosition();
+ int len = ((Expression) arg).getLength();
+ uris.add(new ParserResult(name, position, len,
routeId));
+ }
+ }
+ }
+ }
+ }
+
private static void parseFirstArgument(
JavaClassSource clazz, Block block, MethodInvocation mi,
List<ParserResult> uris, boolean strings, boolean fields,
String name) {
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 7f741fbe970..986dfa66ff8 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
@@ -105,51 +105,64 @@ public final class CamelJavaRestDslParserHelper {
JavaClassSource clazz, String fullyQualifiedFileName,
MethodSource<JavaClassSource> configureMethod) {
- List<RestServiceDetails> answer = new ArrayList<>();
-
- if (configureMethod != null) {
- MethodDeclaration md = (MethodDeclaration)
configureMethod.getInternal();
- Block block = md.getBody();
- if (block != null) {
- for (Object statement : md.getBody().statements()) {
- // must be a method call expression
- if (statement instanceof ExpressionStatement es) {
- Expression exp = es.getExpression();
- boolean valid = isRest(exp);
- if (valid) {
- RestServiceDetails node = new RestServiceDetails();
- answer.add(node);
-
- // include source code details
- int pos = exp.getStartPosition();
- int line = findLineNumber(fullyQualifiedFileName,
pos);
- if (line > -1) {
- node.setLineNumber(Integer.toString(line));
- }
- pos = exp.getStartPosition() + exp.getLength();
- line = findLineNumber(fullyQualifiedFileName, pos);
- if (line > -1) {
- node.setLineNumberEnd(Integer.toString(line));
- }
- node.setFileName(fullyQualifiedFileName);
- node.setClassName(clazz.getQualifiedName());
- node.setMethodName(configureMethod.getName());
+ if (configureMethod == null) {
+ return Collections.emptyList();
+ }
- parseExpression(node, null,
fullyQualifiedFileName, clazz, block, exp);
+ List<RestServiceDetails> answer = new ArrayList<>();
- // flip order of verbs as we parse bottom-up
- if (node.getVerbs() != null) {
- Collections.reverse(node.getVerbs());
- }
- }
+ MethodDeclaration md = (MethodDeclaration)
configureMethod.getInternal();
+ Block block = md.getBody();
+ if (block != null) {
+ for (Object statement : md.getBody().statements()) {
+ // must be a method call expression
+ if (statement instanceof ExpressionStatement es) {
+ Expression exp = es.getExpression();
+ boolean valid = isRest(exp);
+ if (valid) {
+ final RestServiceDetails node =
+ doParse(clazz, fullyQualifiedFileName,
configureMethod, exp, block);
+
+ answer.add(node);
}
}
}
}
+
return answer;
}
+ private RestServiceDetails doParse(
+ JavaClassSource clazz, String fullyQualifiedFileName,
MethodSource<JavaClassSource> configureMethod,
+ Expression exp, Block block) {
+ RestServiceDetails node = new RestServiceDetails();
+
+ // include source code details
+ int pos = exp.getStartPosition();
+ int line = findLineNumber(fullyQualifiedFileName, pos);
+ if (line > -1) {
+ node.setLineNumber(Integer.toString(line));
+ }
+ pos = exp.getStartPosition() + exp.getLength();
+ line = findLineNumber(fullyQualifiedFileName, pos);
+ if (line > -1) {
+ node.setLineNumberEnd(Integer.toString(line));
+ }
+ node.setFileName(fullyQualifiedFileName);
+ node.setClassName(clazz.getQualifiedName());
+ node.setMethodName(configureMethod.getName());
+
+ parseExpression(node, null, fullyQualifiedFileName, clazz, block, exp);
+
+ // flip order of verbs as we parse bottom-up
+ if (node.getVerbs() != null) {
+ Collections.reverse(node.getVerbs());
+ }
+
+ return node;
+ }
+
private boolean isRestConfiguration(Expression exp) {
final String rootMethodName = findRootMethodName(exp);
@@ -522,8 +535,7 @@ public final class CamelJavaRestDslParserHelper {
// is the field annotated with a Camel endpoint
if (field.getAnnotations() != null) {
for (Annotation<JavaClassSource> ann :
field.getAnnotations()) {
- boolean valid =
"org.apache.camel.EndpointInject".equals(ann.getQualifiedName())
- ||
"org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
+ boolean valid = isValid(ann);
if (valid) {
Expression exp = (Expression) ann.getInternal();
exp = ParserCommon.evalExpression(exp);
@@ -607,6 +619,11 @@ public final class CamelJavaRestDslParserHelper {
return null;
}
+ private static boolean isValid(Annotation<JavaClassSource> ann) {
+ return "org.apache.camel.EndpointInject".equals(ann.getQualifiedName())
+ || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
+ }
+
private static boolean isNumericOperator(JavaClassSource clazz, Block
block, Expression expression) {
if (expression instanceof NumberLiteral) {
return true;