Search in sources :

Example 6 with Block

use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block in project camel by apache.

the class CamelJavaParserHelper method findConfigureMethodInCreateRouteBuilder.

private static MethodSource<JavaClassSource> findConfigureMethodInCreateRouteBuilder(JavaClassSource clazz, MethodSource<JavaClassSource> method) {
    // find configure inside the code
    MethodDeclaration md = (MethodDeclaration) method.getInternal();
    Block block = md.getBody();
    if (block != null) {
        List statements = block.statements();
        for (int i = 0; i < statements.size(); i++) {
            Statement stmt = (Statement) statements.get(i);
            Expression exp = null;
            if (stmt instanceof ReturnStatement) {
                ReturnStatement rs = (ReturnStatement) stmt;
                exp = rs.getExpression();
            } else if (stmt instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) stmt;
                exp = es.getExpression();
                if (exp instanceof MethodInvocation) {
                    MethodInvocation mi = (MethodInvocation) exp;
                    for (Object arg : mi.arguments()) {
                        if (arg instanceof ClassInstanceCreation) {
                            exp = (Expression) arg;
                            break;
                        }
                    }
                }
            }
            if (exp != null && exp instanceof ClassInstanceCreation) {
                ClassInstanceCreation cic = (ClassInstanceCreation) exp;
                boolean isRouteBuilder = false;
                if (cic.getType() instanceof SimpleType) {
                    SimpleType st = (SimpleType) cic.getType();
                    isRouteBuilder = "RouteBuilder".equals(st.getName().toString());
                }
                if (isRouteBuilder && cic.getAnonymousClassDeclaration() != null) {
                    List body = cic.getAnonymousClassDeclaration().bodyDeclarations();
                    for (int j = 0; j < body.size(); j++) {
                        Object line = body.get(j);
                        if (line instanceof MethodDeclaration) {
                            MethodDeclaration amd = (MethodDeclaration) line;
                            if ("configure".equals(amd.getName().toString())) {
                                return new AnonymousMethodSource(clazz, amd);
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : ClassInstanceCreation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement) ReturnStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ReturnStatement) Statement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement) MethodInvocation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation) SimpleType(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleType) 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) ExpressionStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ReturnStatement) Block(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) AnonymousMethodSource(org.apache.camel.parser.roaster.AnonymousMethodSource)

Example 7 with Block

use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block in project camel by apache.

the class CamelJavaParserHelper method findFieldInBlock.

@SuppressWarnings("unchecked")
private static FieldSource<JavaClassSource> findFieldInBlock(JavaClassSource clazz, Block block, String fieldName) {
    for (Object statement : block.statements()) {
        // try local statements first in the block
        if (statement instanceof VariableDeclarationStatement) {
            final Type type = ((VariableDeclarationStatement) statement).getType();
            for (Object obj : ((VariableDeclarationStatement) statement).fragments()) {
                if (obj instanceof VariableDeclarationFragment) {
                    VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
                    SimpleName name = fragment.getName();
                    if (name != null && fieldName.equals(name.getIdentifier())) {
                        return new StatementFieldSource(clazz, fragment, type);
                    }
                }
            }
        }
        // okay the field may be burried inside an anonymous inner class as a field declaration
        // outside the configure method, so lets go back to the parent and see what we can find
        ASTNode node = block.getParent();
        if (node instanceof MethodDeclaration) {
            node = node.getParent();
        }
        if (node instanceof AnonymousClassDeclaration) {
            List declarations = ((AnonymousClassDeclaration) node).bodyDeclarations();
            for (Object dec : declarations) {
                if (dec instanceof FieldDeclaration) {
                    FieldDeclaration fd = (FieldDeclaration) dec;
                    final Type type = fd.getType();
                    for (Object obj : fd.fragments()) {
                        if (obj instanceof VariableDeclarationFragment) {
                            VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
                            SimpleName name = fragment.getName();
                            if (name != null && fieldName.equals(name.getIdentifier())) {
                                return new StatementFieldSource(clazz, fragment, type);
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : SimpleType(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleType) Type(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Type) VariableDeclarationFragment(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment) MethodDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.AnonymousClassDeclaration) VariableDeclarationStatement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement) StatementFieldSource(org.apache.camel.parser.roaster.StatementFieldSource) ArrayList(java.util.ArrayList) List(java.util.List) FieldDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.FieldDeclaration)

Example 8 with Block

use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block 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)

Example 9 with Block

use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block in project camel by apache.

the class CamelJavaParserHelper method parseExpression.

private static void parseExpression(JavaClassSource clazz, Block block, Expression exp, List<ParserResult> uris, boolean consumers, boolean producers, boolean strings, boolean fields) {
    if (exp == null) {
        return;
    }
    if (exp instanceof MethodInvocation) {
        MethodInvocation mi = (MethodInvocation) exp;
        doParseCamelUris(clazz, block, mi, uris, consumers, producers, strings, fields);
        // if the method was called on another method, then recursive
        exp = mi.getExpression();
        parseExpression(clazz, block, exp, uris, consumers, producers, strings, fields);
    }
}
Also used : MethodInvocation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

ArrayList (java.util.ArrayList)7 Expression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression)6 InfixExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression)6 ParenthesizedExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression)6 List (java.util.List)5 MethodInvocation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation)5 ParserResult (org.apache.camel.parser.ParserResult)4 MethodDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration)4 Block (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block)3 ExpressionStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement)3 SimpleName (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName)3 StatementFieldSource (org.apache.camel.parser.roaster.StatementFieldSource)2 ASTNode (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode)2 MemberValuePair (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair)2 NormalAnnotation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation)2 SimpleType (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleType)2 SingleMemberAnnotation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation)2 VariableDeclarationFragment (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 VariableDeclarationStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 AnonymousMethodSource (org.apache.camel.parser.roaster.AnonymousMethodSource)1