Search in sources :

Example 1 with Block

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

the class CamelJavaParserHelper method extractEndpointUriFromArgument.

private static void extractEndpointUriFromArgument(String node, JavaClassSource clazz, Block block, List<ParserResult> uris, Object arg, boolean strings, boolean fields) {
    if (strings) {
        String uri = getLiteralValue(clazz, block, (Expression) arg);
        if (!Strings.isBlank(uri)) {
            int position = ((Expression) arg).getStartPosition();
            // if the node is fromF or toF, then replace all %X with {{%X}} as we cannot parse that value
            if ("fromF".equals(node) || "toF".equals(node)) {
                uri = uri.replaceAll("\\%s", "\\{\\{\\%s\\}\\}");
                uri = uri.replaceAll("\\%d", "\\{\\{\\%d\\}\\}");
                uri = uri.replaceAll("\\%b", "\\{\\{\\%b\\}\\}");
            }
            uris.add(new ParserResult(node, position, uri));
            return;
        }
    }
    if (fields && arg instanceof SimpleName) {
        FieldSource field = getField(clazz, block, (SimpleName) arg);
        if (field != null) {
            // find the endpoint uri from the annotation
            AnnotationSource annotation = field.getAnnotation("org.apache.camel.cdi.Uri");
            if (annotation == null) {
                annotation = field.getAnnotation("org.apache.camel.EndpointInject");
            }
            if (annotation != null) {
                Expression exp = (Expression) annotation.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;
                        }
                    }
                }
                String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
                if (!Strings.isBlank(uri)) {
                    int position = ((SimpleName) arg).getStartPosition();
                    uris.add(new ParserResult(node, position, uri));
                }
            } else {
                // the field may be initialized using variables, so we need to evaluate those expressions
                Object fi = field.getInternal();
                if (fi instanceof VariableDeclaration) {
                    Expression exp = ((VariableDeclaration) fi).getInitializer();
                    String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp);
                    if (!Strings.isBlank(uri)) {
                        // we want the position of the field, and not in the route
                        int position = ((VariableDeclaration) fi).getStartPosition();
                        uris.add(new ParserResult(node, position, uri));
                    }
                }
            }
        }
    }
    // cannot parse it so add a failure
    uris.add(new ParserResult(node, -1, arg.toString(), false));
}
Also used : SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) SimpleName(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName) ParserResult(org.apache.camel.parser.ParserResult) MemberValuePair(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair) 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) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) ArrayList(java.util.ArrayList) List(java.util.List) VariableDeclaration(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclaration) FieldSource(org.jboss.forge.roaster.model.source.FieldSource) StatementFieldSource(org.apache.camel.parser.roaster.StatementFieldSource)

Example 2 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(String node, JavaClassSource clazz, Block block, Expression exp, List<ParserResult> expressions) {
    if (exp == null) {
        return;
    }
    if (exp instanceof MethodInvocation) {
        MethodInvocation mi = (MethodInvocation) exp;
        doParseCamelSimple(node, clazz, block, mi, expressions);
        // if the method was called on another method, then recursive
        exp = mi.getExpression();
        parseExpression(node, clazz, block, exp, expressions);
    }
}
Also used : MethodInvocation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation)

Example 3 with Block

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

the class CamelJavaParserHelper method getLiteralValue.

public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) {
    // unwrap parenthesis
    if (expression instanceof ParenthesizedExpression) {
        expression = ((ParenthesizedExpression) expression).getExpression();
    }
    if (expression instanceof StringLiteral) {
        return ((StringLiteral) expression).getLiteralValue();
    } else if (expression instanceof BooleanLiteral) {
        return "" + ((BooleanLiteral) expression).booleanValue();
    } else if (expression instanceof NumberLiteral) {
        return ((NumberLiteral) expression).getToken();
    }
    // if it a method invocation then add a dummy value assuming the method invocation will return a valid response
    if (expression instanceof MethodInvocation) {
        String name = ((MethodInvocation) expression).getName().getIdentifier();
        return "{{" + name + "}}";
    }
    // source code we have access to
    if (expression instanceof QualifiedName) {
        QualifiedName qn = (QualifiedName) expression;
        String name = qn.getFullyQualifiedName();
        return "{{" + name + "}}";
    }
    if (expression instanceof SimpleName) {
        FieldSource<JavaClassSource> field = getField(clazz, block, (SimpleName) expression);
        if (field != null) {
            // is the field annotated with a Camel endpoint
            if (field.getAnnotations() != 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) {
                        Expression 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;
                                }
                            }
                        }
                        if (exp != null) {
                            return getLiteralValue(clazz, block, exp);
                        }
                    }
                }
            }
            // is the field an org.apache.camel.Endpoint type?
            if ("Endpoint".equals(field.getType().getSimpleName())) {
                // then grab the uri from the first argument
                VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
                expression = vdf.getInitializer();
                if (expression instanceof MethodInvocation) {
                    MethodInvocation mi = (MethodInvocation) expression;
                    List args = mi.arguments();
                    if (args != null && args.size() > 0) {
                        // the first argument has the endpoint uri
                        expression = (Expression) args.get(0);
                        return getLiteralValue(clazz, block, expression);
                    }
                }
            } else {
                // no annotations so try its initializer
                VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
                expression = vdf.getInitializer();
                if (expression == null) {
                    // its a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
                    return "{{" + field.getName() + "}}";
                } else {
                    return getLiteralValue(clazz, block, expression);
                }
            }
        } else {
            // we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
            final String fieldName = ((SimpleName) expression).getIdentifier();
            return "{{" + fieldName + "}}";
        }
    } else if (expression instanceof InfixExpression) {
        String answer = null;
        // is it a string that is concat together?
        InfixExpression ie = (InfixExpression) expression;
        if (InfixExpression.Operator.PLUS.equals(ie.getOperator())) {
            String val1 = getLiteralValue(clazz, block, ie.getLeftOperand());
            String val2 = getLiteralValue(clazz, block, ie.getRightOperand());
            // if numeric then we plus the values, otherwise we string concat
            boolean numeric = isNumericOperator(clazz, block, ie.getLeftOperand()) && isNumericOperator(clazz, block, ie.getRightOperand());
            if (numeric) {
                Long num1 = val1 != null ? Long.valueOf(val1) : 0;
                Long num2 = val2 != null ? Long.valueOf(val2) : 0;
                answer = "" + (num1 + num2);
            } else {
                answer = (val1 != null ? val1 : "") + (val2 != null ? val2 : "");
            }
            if (!answer.isEmpty()) {
                // include extended when we concat on 2 or more lines
                List extended = ie.extendedOperands();
                if (extended != null) {
                    for (Object ext : extended) {
                        String val3 = getLiteralValue(clazz, block, (Expression) ext);
                        if (numeric) {
                            Long num3 = val3 != null ? Long.valueOf(val3) : 0;
                            Long num = Long.valueOf(answer);
                            answer = "" + (num + num3);
                        } else {
                            answer += val3 != null ? val3 : "";
                        }
                    }
                }
            }
        }
        return answer;
    }
    return null;
}
Also used : ParenthesizedExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression) BooleanLiteral(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.BooleanLiteral) SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) QualifiedName(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) MethodInvocation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation) SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) Annotation(org.jboss.forge.roaster.model.Annotation) StringLiteral(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.StringLiteral) MemberValuePair(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair) 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) VariableDeclarationFragment(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment) InfixExpression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) ArrayList(java.util.ArrayList) List(java.util.List) NumberLiteral(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NumberLiteral)

Example 4 with Block

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

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

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