Search in sources :

Example 1 with MethodDeclaration

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

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

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

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

Aggregations

ArrayList (java.util.ArrayList)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 Expression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression)3 ExpressionStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement)3 InfixExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression)3 ParenthesizedExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression)3 List (java.util.List)2 ParserResult (org.apache.camel.parser.ParserResult)2 SimpleType (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleType)2 VariableDeclarationStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 AnonymousMethodSource (org.apache.camel.parser.roaster.AnonymousMethodSource)1 StatementFieldSource (org.apache.camel.parser.roaster.StatementFieldSource)1 ASTNode (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode)1 AnonymousClassDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.AnonymousClassDeclaration)1 ClassInstanceCreation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ClassInstanceCreation)1 FieldDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.FieldDeclaration)1 MethodInvocation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation)1 ReturnStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ReturnStatement)1 SimpleName (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName)1