use of org.apache.camel.parser.ParserResult in project camel by apache.
the class CamelJavaParserHelper method extractEndpointUriFromArgument.
private static void extractEndpointUriFromArgument(String node, JavaClassSource clazz, Block block, List<ParserResult> uris, Object arg, boolean strings, boolean fields) {
if (strings) {
String uri = getLiteralValue(clazz, block, (Expression) arg);
if (!Strings.isBlank(uri)) {
int position = ((Expression) arg).getStartPosition();
// 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)) {
uri = uri.replaceAll("\\%s", "\\{\\{\\%s\\}\\}");
uri = uri.replaceAll("\\%d", "\\{\\{\\%d\\}\\}");
uri = uri.replaceAll("\\%b", "\\{\\{\\%b\\}\\}");
}
uris.add(new ParserResult(node, position, uri));
return;
}
}
if (fields && arg instanceof SimpleName) {
FieldSource field = getField(clazz, block, (SimpleName) arg);
if (field != null) {
// find the endpoint uri from the annotation
AnnotationSource annotation = field.getAnnotation("org.apache.camel.cdi.Uri");
if (annotation == null) {
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;
}
}
}
String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
if (!Strings.isBlank(uri)) {
int position = ((SimpleName) arg).getStartPosition();
uris.add(new ParserResult(node, position, uri));
}
} else {
// the field may be initialized using variables, so we need to evaluate those expressions
Object fi = field.getInternal();
if (fi instanceof VariableDeclaration) {
Expression exp = ((VariableDeclaration) fi).getInitializer();
String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
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));
}
}
}
}
}
// cannot parse it so add a failure
uris.add(new ParserResult(node, -1, arg.toString(), false));
}
use of org.apache.camel.parser.ParserResult in project camel by apache.
the class CamelJavaParserHelper method parseCamelSimpleExpressions.
public static List<ParserResult> parseCamelSimpleExpressions(MethodSource<JavaClassSource> method) {
List<ParserResult> answer = new ArrayList<ParserResult>();
MethodDeclaration md = (MethodDeclaration) method.getInternal();
Block block = md.getBody();
if (block != null) {
for (Object statement : block.statements()) {
// must be a method call expression
if (statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
Expression exp = es.getExpression();
List<ParserResult> expressions = new ArrayList<ParserResult>();
parseExpression(null, method.getOrigin(), block, exp, expressions);
if (!expressions.isEmpty()) {
// reverse the order as we will grab them from last->first
Collections.reverse(expressions);
answer.addAll(expressions);
}
}
}
}
return answer;
}
use of org.apache.camel.parser.ParserResult in project camel by apache.
the class CamelJavaParserHelper method doParseCamelUris.
private static List<ParserResult> doParseCamelUris(MethodSource<JavaClassSource> method, boolean consumers, boolean producers, boolean strings, boolean fields) {
List<ParserResult> answer = new ArrayList<ParserResult>();
if (method != null) {
MethodDeclaration md = (MethodDeclaration) method.getInternal();
Block block = md.getBody();
if (block != null) {
for (Object statement : md.getBody().statements()) {
// must be a method call expression
if (statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
Expression exp = es.getExpression();
List<ParserResult> uris = new ArrayList<ParserResult>();
parseExpression(method.getOrigin(), block, exp, uris, consumers, producers, strings, fields);
if (!uris.isEmpty()) {
// reverse the order as we will grab them from last->first
Collections.reverse(uris);
answer.addAll(uris);
}
}
}
}
}
return answer;
}
use of org.apache.camel.parser.ParserResult in project camel by apache.
the class RoasterMyNettyTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyNettyTest.java"));
MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Consumer: " + result.getElement());
}
Assert.assertEquals("netty-http:http://0.0.0.0:{{port}}/foo", list.get(0).getElement());
Assert.assertEquals("netty-http:http://0.0.0.0:{{getNextPort}}/bar", list.get(1).getElement());
list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Producer: " + result.getElement());
}
Assert.assertEquals("mock:input1", list.get(0).getElement());
Assert.assertEquals("netty-http:http://0.0.0.0:{{getNextPort}}/bar", list.get(1).getElement());
Assert.assertEquals("mock:input2", list.get(2).getElement());
}
use of org.apache.camel.parser.ParserResult in project camel by apache.
the class RoasterNewLineConstRouteBuilderConfigureTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyNewLineConstRouteBuilder.java"));
MethodSource<JavaClassSource> method = clazz.getMethod("configure");
List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, false);
for (ParserResult result : list) {
LOG.info("Consumer: " + result.getElement());
}
Assert.assertEquals("timer:foo", list.get(0).getElement());
list = CamelJavaParserHelper.parseCamelProducerUris(method, true, false);
for (ParserResult result : list) {
LOG.info("Producer: " + result.getElement());
}
Assert.assertEquals("file:output?fileExist=Append&chmod=777&allowNullBody=true", list.get(0).getElement());
Assert.assertEquals("log:b", list.get(1).getElement());
}
Aggregations