Search in sources :

Example 21 with ParserResult

use of org.apache.camel.parser.ParserResult in project camel by apache.

the class RoasterFieldRouteBuilderConfigureTest method parse.

@Test
public void parse() throws Exception {
    JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyFieldRouteBuilder.java"));
    MethodSource<JavaClassSource> method = clazz.getMethod("configure");
    List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Consumer: " + result.getElement());
    }
    Assert.assertEquals("timer:foo", list.get(0).getElement());
    list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Producer: " + result.getElement());
    }
    Assert.assertEquals("file:output?fileExist=Override", 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)

Example 22 with ParserResult

use of org.apache.camel.parser.ParserResult in project camel by apache.

the class RoasterMyFieldMethodCallRouteBuilderConfigureTest method parse.

@Test
public void parse() throws Exception {
    JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyFieldMethodCallRouteBuilder.java"));
    MethodSource<JavaClassSource> method = clazz.getMethod("configure");
    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 23 with ParserResult

use of org.apache.camel.parser.ParserResult in project camel by apache.

the class CamelJavaParserHelper method doParseCamelSimple.

private static void doParseCamelSimple(String node, JavaClassSource clazz, Block block, MethodInvocation mi, List<ParserResult> expressions) {
    String name = mi.getName().getIdentifier();
    if ("simple".equals(name)) {
        List args = mi.arguments();
        // the first argument is a string parameter for the simple expression
        if (args != null && args.size() >= 1) {
            // it is a String type
            Object arg = args.get(0);
            String simple = getLiteralValue(clazz, block, (Expression) arg);
            if (!Strings.isBlank(simple)) {
                // is this a simple expression that is called as a predicate or expression
                boolean predicate = false;
                Expression parent = mi.getExpression();
                if (parent == null) {
                    // maybe its an argument
                    // simple maybe be passed in as an argument
                    List list = mi.arguments();
                    // must be a single argument
                    if (list != null && list.size() == 1) {
                        ASTNode o = (ASTNode) list.get(0);
                        ASTNode p = o.getParent();
                        if (p instanceof MethodInvocation) {
                            // this is simple
                            String pName = ((MethodInvocation) p).getName().getIdentifier();
                            if ("simple".equals(pName)) {
                                // okay find the parent of simple which is the method that uses simple
                                parent = (Expression) p.getParent();
                            }
                        }
                    }
                }
                if (parent != null && parent instanceof MethodInvocation) {
                    MethodInvocation emi = (MethodInvocation) parent;
                    String parentName = emi.getName().getIdentifier();
                    predicate = isSimplePredicate(parentName);
                }
                int position = ((Expression) arg).getStartPosition();
                ParserResult result = new ParserResult(node, position, simple);
                result.setPredicate(predicate);
                expressions.add(result);
            }
        }
    }
    // simple maybe be passed in as an argument
    List args = mi.arguments();
    if (args != null) {
        for (Object arg : args) {
            if (arg instanceof MethodInvocation) {
                MethodInvocation ami = (MethodInvocation) arg;
                doParseCamelSimple(node, clazz, block, ami, expressions);
            }
        }
    }
}
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) ASTNode(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) MethodInvocation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation)

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