Search in sources :

Example 1 with ASTNode

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

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

the class RouteBuilderParser method parseRouteBuilderEndpoints.

/**
     * Parses the java source class to discover Camel endpoints.
     *
     * @param clazz                        the java source class
     * @param baseDir                      the base of the source code
     * @param fullyQualifiedFileName       the fully qualified source code file name
     * @param endpoints                    list to add discovered and parsed endpoints
     * @param unparsable                   list of unparsable nodes
     * @param includeInlinedRouteBuilders  whether to include inlined route builders in the parsing
     */
public static void parseRouteBuilderEndpoints(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName, List<CamelEndpointDetails> endpoints, List<String> unparsable, boolean includeInlinedRouteBuilders) {
    // look for fields which are not used in the route
    for (FieldSource<JavaClassSource> field : clazz.getFields()) {
        // is the field annotated with a Camel endpoint
        String uri = null;
        Expression exp = null;
        for (Annotation ann : field.getAnnotations()) {
            boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
            if (valid) {
                exp = (Expression) ann.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;
                        }
                    }
                }
                uri = CamelJavaParserHelper.getLiteralValue(clazz, null, exp);
            }
        }
        // we only want to add fields which are not used in the route
        if (!Strings.isBlank(uri) && findEndpointByUri(endpoints, uri) == null) {
            // we only want the relative dir name from the
            String fileName = fullyQualifiedFileName;
            if (fileName.startsWith(baseDir)) {
                fileName = fileName.substring(baseDir.length() + 1);
            }
            String id = field.getName();
            CamelEndpointDetails detail = new CamelEndpointDetails();
            detail.setFileName(fileName);
            detail.setClassName(clazz.getQualifiedName());
            detail.setEndpointInstance(id);
            detail.setEndpointUri(uri);
            detail.setEndpointComponentName(endpointComponentName(uri));
            // favor the position of the expression which had the actual uri
            Object internal = exp != null ? exp : field.getInternal();
            // find position of field/expression
            if (internal instanceof ASTNode) {
                int pos = ((ASTNode) internal).getStartPosition();
                int line = findLineNumber(fullyQualifiedFileName, pos);
                if (line > -1) {
                    detail.setLineNumber("" + line);
                }
            }
            // we do not know if this field is used as consumer or producer only, but we try
            // to find out by scanning the route in the configure method below
            endpoints.add(detail);
        }
    }
    // find all the configure methods
    List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
    MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
    if (method != null) {
        methods.add(method);
    }
    if (includeInlinedRouteBuilders) {
        List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
        if (!inlinedMethods.isEmpty()) {
            methods.addAll(inlinedMethods);
        }
    }
    // determine this to ensure when we edit the endpoint we should only the options accordingly
    for (MethodSource<JavaClassSource> configureMethod : methods) {
        // consumers only
        List<ParserResult> uris = CamelJavaParserHelper.parseCamelConsumerUris(configureMethod, true, true);
        for (ParserResult result : uris) {
            if (!result.isParsed()) {
                if (unparsable != null) {
                    unparsable.add(result.getElement());
                }
            } else {
                CamelEndpointDetails detail = findEndpointByUri(endpoints, result.getElement());
                if (detail != null) {
                    // its a consumer only
                    detail.setConsumerOnly(true);
                } else {
                    String fileName = fullyQualifiedFileName;
                    if (fileName.startsWith(baseDir)) {
                        fileName = fileName.substring(baseDir.length() + 1);
                    }
                    detail = new CamelEndpointDetails();
                    detail.setFileName(fileName);
                    detail.setClassName(clazz.getQualifiedName());
                    detail.setMethodName(configureMethod.getName());
                    detail.setEndpointInstance(null);
                    detail.setEndpointUri(result.getElement());
                    int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
                    if (line > -1) {
                        detail.setLineNumber("" + line);
                    }
                    detail.setEndpointComponentName(endpointComponentName(result.getElement()));
                    detail.setConsumerOnly(true);
                    detail.setProducerOnly(false);
                    endpoints.add(detail);
                }
            }
        }
        // producer only
        uris = CamelJavaParserHelper.parseCamelProducerUris(configureMethod, true, true);
        for (ParserResult result : uris) {
            if (!result.isParsed()) {
                if (unparsable != null) {
                    unparsable.add(result.getElement());
                }
            } else {
                CamelEndpointDetails detail = findEndpointByUri(endpoints, result.getElement());
                if (detail != null) {
                    if (detail.isConsumerOnly()) {
                        // its both a consumer and producer
                        detail.setConsumerOnly(false);
                        detail.setProducerOnly(false);
                    } else {
                        // its a producer only
                        detail.setProducerOnly(true);
                    }
                }
                // the same endpoint uri may be used in multiple places in the same route
                // so we should maybe add all of them
                String fileName = fullyQualifiedFileName;
                if (fileName.startsWith(baseDir)) {
                    fileName = fileName.substring(baseDir.length() + 1);
                }
                detail = new CamelEndpointDetails();
                detail.setFileName(fileName);
                detail.setClassName(clazz.getQualifiedName());
                detail.setMethodName(configureMethod.getName());
                detail.setEndpointInstance(null);
                detail.setEndpointUri(result.getElement());
                int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
                if (line > -1) {
                    detail.setLineNumber("" + line);
                }
                detail.setEndpointComponentName(endpointComponentName(result.getElement()));
                detail.setConsumerOnly(false);
                detail.setProducerOnly(true);
                endpoints.add(detail);
            }
        }
    }
}
Also used : CamelEndpointDetails(org.apache.camel.parser.model.CamelEndpointDetails) SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) Annotation(org.jboss.forge.roaster.model.Annotation) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) MemberValuePair(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair) Expression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression) ASTNode(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with ASTNode

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

ArrayList (java.util.ArrayList)3 List (java.util.List)3 ASTNode (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode)3 Expression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression)2 ParserResult (org.apache.camel.parser.ParserResult)1 CamelEndpointDetails (org.apache.camel.parser.model.CamelEndpointDetails)1 StatementFieldSource (org.apache.camel.parser.roaster.StatementFieldSource)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 InfixExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression)1 MemberValuePair (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair)1 MethodDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration)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 ParenthesizedExpression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression)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 SingleMemberAnnotation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation)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