Search in sources :

Example 1 with FieldSource

use of org.jboss.forge.roaster.model.source.FieldSource 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ParserResult (org.apache.camel.parser.ParserResult)1 StatementFieldSource (org.apache.camel.parser.roaster.StatementFieldSource)1 Expression (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression)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 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 SingleMemberAnnotation (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation)1 VariableDeclaration (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclaration)1 AnnotationSource (org.jboss.forge.roaster.model.source.AnnotationSource)1 FieldSource (org.jboss.forge.roaster.model.source.FieldSource)1