Search in sources :

Example 1 with VariableDeclarationStatement

use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement 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)1 List (java.util.List)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 FieldDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.FieldDeclaration)1 MethodDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration)1 SimpleName (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName)1 SimpleType (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleType)1 Type (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Type)1 VariableDeclarationFragment (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment)1 VariableDeclarationStatement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement)1