Search in sources :

Example 1 with ParserResult

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));
}
Also used : SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) SimpleName(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName) ParserResult(org.apache.camel.parser.ParserResult) MemberValuePair(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair) Expression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression) InfixExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) ArrayList(java.util.ArrayList) List(java.util.List) VariableDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclaration) FieldSource(org.jboss.forge.roaster.model.source.FieldSource) StatementFieldSource(org.apache.camel.parser.roaster.StatementFieldSource)

Example 2 with ParserResult

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;
}
Also used : ParserResult(org.apache.camel.parser.ParserResult) Expression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression) InfixExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression) MethodDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration) ExpressionStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement) ArrayList(java.util.ArrayList) Block(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block)

Example 3 with ParserResult

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;
}
Also used : ParserResult(org.apache.camel.parser.ParserResult) Expression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression) InfixExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression) MethodDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration) ExpressionStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement) ArrayList(java.util.ArrayList) Block(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block)

Example 4 with ParserResult

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());
}
Also used : ParserResult(org.apache.camel.parser.ParserResult) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) File(java.io.File) Test(org.junit.Test)

Example 5 with ParserResult

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());
}
Also used : ParserResult(org.apache.camel.parser.ParserResult) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) File(java.io.File) Test(org.junit.Test)

Aggregations

ParserResult (org.apache.camel.parser.ParserResult)23 File (java.io.File)19 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)19 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)9 CamelEndpointDetails (org.apache.camel.parser.model.CamelEndpointDetails)5 Expression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression)4 InfixExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression)4 ParenthesizedExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression)4 List (java.util.List)2 Block (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block)2 ExpressionStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement)2 MethodDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration)2 StatementFieldSource (org.apache.camel.parser.roaster.StatementFieldSource)1 ASTNode (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode)1 MemberValuePair (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair)1 MethodInvocation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation)1 NormalAnnotation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation)1 SimpleName (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName)1 SingleMemberAnnotation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation)1