Search in sources :

Example 1 with ArrayInitializerExpr

use of com.github.javaparser.ast.expr.ArrayInitializerExpr 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 ArrayInitializerExpr

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

the class TreeVisitorTest method issue743ConcurrentModificationProblem.

@Test
public void issue743ConcurrentModificationProblem() {
    Expression expression = JavaParser.parseExpression("new int[]{1,2,3,4}");
    StringBuilder result = new StringBuilder();
    TreeVisitor visitor = new TreeVisitor() {

        @Override
        public void process(Node node) {
            if (node instanceof IntegerLiteralExpr) {
                node.getParentNode().ifPresent(parent -> ((ArrayInitializerExpr) parent).getValues().add(new IntegerLiteralExpr("1")));
            }
            result.append("<").append(node).append("> ");
        }
    };
    visitor.visitPreOrder(expression);
    System.out.println(result);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Aggregations

ArrayInitializerExpr (com.github.javaparser.ast.expr.ArrayInitializerExpr)2 Expression (com.github.javaparser.ast.expr.Expression)2 Node (com.github.javaparser.ast.Node)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 ArrayType (javax.lang.model.type.ArrayType)1 TypeKind (javax.lang.model.type.TypeKind)1 TypeMirror (javax.lang.model.type.TypeMirror)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)1 Test (org.junit.Test)1