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 2023/11/09 17:33:28 UTC
(camel) branch main updated: CAMEL-20093: cleaned up duplicated code in camel-route-parser
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 3d476afb6b6 CAMEL-20093: cleaned up duplicated code in camel-route-parser
3d476afb6b6 is described below
commit 3d476afb6b69600ff0228b6b896857462c704c55
Author: Nikita Konovalov <nk...@redhat.com>
AuthorDate: Thu Nov 9 17:29:49 2023 +0100
CAMEL-20093: cleaned up duplicated code in camel-route-parser
---
.../camel/parser/helper/CamelJavaParserHelper.java | 53 ++++++++++++----------
.../parser/helper/CamelJavaTreeParserHelper.java | 26 +----------
2 files changed, 31 insertions(+), 48 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 41317a2cb18..a7c9414b419 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
@@ -532,29 +532,7 @@ public final class CamelJavaParserHelper {
}
}
// is the field an org.apache.camel.Endpoint type?
- if ("Endpoint".equals(field.getType().getSimpleName())) {
- // then grab the uri from the first argument
- VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
- expression = vdf.getInitializer();
- if (expression instanceof MethodInvocation mi) {
- List<?> args = mi.arguments();
- if (args != null && !args.isEmpty()) {
- // the first argument has the endpoint uri
- expression = (Expression) args.get(0);
- return getLiteralValue(clazz, block, expression);
- }
- }
- } else {
- // no annotations so try its initializer
- VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
- expression = vdf.getInitializer();
- if (expression == null) {
- // it's a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
- return "{{" + field.getName() + "}}";
- } else {
- return getLiteralValue(clazz, block, expression);
- }
- }
+ return endpointTypeCheck(clazz, block, field);
} else {
// we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
final String fieldName = ((SimpleName) expression).getIdentifier();
@@ -563,7 +541,6 @@ public final class CamelJavaParserHelper {
} else if (expression instanceof InfixExpression ie) {
return getValueFromExpression(clazz, block, ie);
}
-
return null;
}
@@ -608,4 +585,32 @@ public final class CamelJavaParserHelper {
return answer;
}
+ static String endpointTypeCheck(JavaClassSource clazz, Block block, FieldSource<JavaClassSource> field) {
+ Expression expression;
+ if ("Endpoint".equals(field.getType().getSimpleName())) {
+ // then grab the uri from the first argument
+ VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
+ expression = vdf.getInitializer();
+ if (expression instanceof MethodInvocation mi) {
+ List<?> args = mi.arguments();
+ if (args != null && !args.isEmpty()) {
+ // the first argument has the endpoint uri
+ expression = (Expression) args.get(0);
+ return getLiteralValue(clazz, block, expression);
+ }
+ }
+ } else {
+ // no annotations so try its initializer
+ VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
+ expression = vdf.getInitializer();
+ if (expression == null) {
+ // it's a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
+ return "{{" + field.getName() + "}}";
+ } else {
+ return getLiteralValue(clazz, block, expression);
+ }
+ }
+ return null;
+ }
+
}
diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
index dcabd60e94f..0c0599a1bb1 100644
--- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
+++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
@@ -40,12 +40,12 @@ import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName;
import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.StringLiteral;
import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.TextBlock;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.jboss.forge.roaster.model.Annotation;
import org.jboss.forge.roaster.model.source.FieldSource;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.jboss.forge.roaster.model.source.MethodSource;
+import static org.apache.camel.parser.helper.CamelJavaParserHelper.endpointTypeCheck;
import static org.apache.camel.parser.helper.ParserCommon.findLineNumber;
/**
@@ -333,29 +333,7 @@ public final class CamelJavaTreeParserHelper {
}
}
// is the field an org.apache.camel.Endpoint type?
- if ("Endpoint".equals(field.getType().getSimpleName())) {
- // then grab the uri from the first argument
- VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
- expression = vdf.getInitializer();
- if (expression instanceof MethodInvocation mi) {
- List<?> args = mi.arguments();
- if (args != null && !args.isEmpty()) {
- // the first argument has the endpoint uri
- expression = (Expression) args.get(0);
- return getLiteralValue(clazz, block, expression);
- }
- }
- } else {
- // no annotations so try its initializer
- VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
- expression = vdf.getInitializer();
- if (expression == null) {
- // it's a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
- return "{{" + field.getName() + "}}";
- } else {
- return getLiteralValue(clazz, block, expression);
- }
- }
+ return endpointTypeCheck(clazz, block, field);
} else {
// we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
final String fieldName = ((SimpleName) expression).getIdentifier();