Search in sources :

Example 1 with Expression

use of com.github.javaparser.ast.expr.Expression in project checker-framework by typetools.

the class StubParser method handleExpr.

/**
 * Adds an annotation element (argument) to {@code builder}. The element is a Java expression.
 *
 * @return true if the expression was parsed and added to {@code builder}, false otherwise
 */
private boolean handleExpr(AnnotationBuilder builder, String name, Expression expr) {
    ExecutableElement var = builder.findElement(name);
    TypeMirror expected = var.getReturnType();
    TypeKind valueKind;
    if (expected.getKind() == TypeKind.ARRAY) {
        valueKind = ((ArrayType) expected).getComponentType().getKind();
    } else {
        valueKind = expected.getKind();
    }
    if (expr instanceof ArrayInitializerExpr) {
        if (expected.getKind() != TypeKind.ARRAY) {
            stubWarn("unhandled annotation attribute type: " + expr + " and expected: " + expected);
            return false;
        }
        List<Expression> arrayExpressions = ((ArrayInitializerExpr) expr).getValues();
        Object[] values = new Object[arrayExpressions.size()];
        for (int i = 0; i < arrayExpressions.size(); ++i) {
            values[i] = getValueOfExpressionInAnnotation(name, arrayExpressions.get(i), valueKind);
            if (values[i] == null) {
                return false;
            }
        }
        builder.setValue(name, values);
    } else {
        Object value = getValueOfExpressionInAnnotation(name, expr, valueKind);
        if (value == null) {
            return false;
        }
        if (expected.getKind() == TypeKind.ARRAY) {
            Object[] valueArray = { value };
            builder.setValue(name, valueArray);
        } else {
            builderSetValue(builder, name, value);
        }
    }
    return true;
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) ArrayType(javax.lang.model.type.ArrayType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) Expression(com.github.javaparser.ast.expr.Expression) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind)

Example 2 with Expression

use of com.github.javaparser.ast.expr.Expression in project checker-framework by typetools.

the class StubParser method getAnnotation.

/**
 * Convert {@code annotation} into an AnnotationMirror. Returns null if the annotation isn't
 * supported by the checker or if some error occurred while converting it.
 */
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, AnnotationMirror> allStubAnnotations) {
    AnnotationMirror annoMirror;
    if (annotation instanceof MarkerAnnotationExpr) {
        String annoName = ((MarkerAnnotationExpr) annotation).getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
    } else if (annotation instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
        String annoName = nrmanno.getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
        if (annoMirror == null) {
            // Not a supported qualifier -> ignore
            return null;
        }
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
        List<MemberValuePair> pairs = nrmanno.getPairs();
        if (pairs != null) {
            for (MemberValuePair mvp : pairs) {
                String member = mvp.getNameAsString();
                Expression exp = mvp.getValue();
                boolean success = handleExpr(builder, member, exp);
                if (!success) {
                    stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", exp, annotation);
                    return null;
                }
            }
        }
        return builder.build();
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
        String annoName = sglanno.getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
        if (annoMirror == null) {
            // Not a supported qualifier -> ignore
            return null;
        }
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
        Expression valexpr = sglanno.getMemberValue();
        boolean success = handleExpr(builder, "value", valexpr);
        if (!success) {
            stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", valexpr, annotation);
            return null;
        }
        return builder.build();
    } else {
        ErrorReporter.errorAbort("StubParser: unknown annotation type: " + annotation);
        // dead code
        annoMirror = null;
    }
    return annoMirror;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 3 with Expression

use of com.github.javaparser.ast.expr.Expression in project vue-gwt by Axellience.

the class TemplateParser method processJavaExpression.

/**
 * Process the given string as a Java expression.
 * @param expressionString A valid Java expression
 * @return A processed expression, should be placed in the HTML in place of the original
 * expression
 */
private TemplateExpression processJavaExpression(String expressionString) {
    Expression expression;
    try {
        expression = JavaParser.parseExpression(expressionString);
    } catch (ParseProblemException parseException) {
        logger.error("Couldn't parse Expression, make sure it is valid Java.", expressionString);
        throw parseException;
    }
    resolveTypesUsingImports(expression);
    resolveStaticMethodsUsingImports(expression);
    checkMethodNames(expression);
    // Find the parameters used by the expression
    List<VariableInfo> expressionParameters = new LinkedList<>();
    findExpressionParameters(expression, expressionParameters);
    // If there is a cast first, we use this as the type of our expression
    if (currentProp == null)
        expression = getTypeFromCast(expression);
    // Update the expression as it might have been changed
    expressionString = expression.toString();
    // Add the resulting expression to our result
    return result.addExpression(expressionString, currentExpressionReturnType, currentProp == null, expressionParameters);
}
Also used : TemplateExpression(com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression) Expression(com.github.javaparser.ast.expr.Expression) VariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo) LocalVariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo) LinkedList(java.util.LinkedList) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 4 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class CloneVisitor method visit.

@Override
public Node visit(ArrayCreationLevel _n, Object _arg) {
    List<AnnotationExpr> ann = visit(_n.getAnnotations(), _arg);
    Expression dimension_ = cloneNodes(_n.getDimension(), _arg);
    ArrayCreationLevel r = new ArrayCreationLevel(_n.getRange(), dimension_, ann);
    Comment comment = cloneNodes(_n.getComment(), _arg);
    r.setComment(comment);
    return r;
}
Also used : Comment(com.github.javaparser.ast.comments.Comment) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) BlockComment(com.github.javaparser.ast.comments.BlockComment) LineComment(com.github.javaparser.ast.comments.LineComment) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Expression(com.github.javaparser.ast.expr.Expression)

Example 5 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class ModifierVisitorAdapter method visit.

@Override
public Node visit(final IfStmt n, final A arg) {
    visitComment(n, arg);
    final Expression condition = (Expression) n.getCondition().accept(this, arg);
    if (condition == null) {
        return null;
    }
    n.setCondition(condition);
    final Statement thenStmt = (Statement) n.getThenStmt().accept(this, arg);
    if (thenStmt == null) {
        // then-clause.
        return null;
    }
    n.setThenStmt(thenStmt);
    if (n.getElseStmt() != null) {
        n.setElseStmt((Statement) n.getElseStmt().accept(this, arg));
    }
    return n;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Statement(com.github.javaparser.ast.stmt.Statement)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43