Search in sources :

Example 1 with ClassExpr

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

the class StubParser method getValueOfExpressionInAnnotation.

/**
 * Returns the value of {@code expr}, or null if some problem occurred getting the value.
 */
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) {
    if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
        VariableElement elem;
        if (expr instanceof NameExpr) {
            elem = findVariableElement((NameExpr) expr);
        } else {
            elem = findVariableElement((FieldAccessExpr) expr);
        }
        if (elem == null) {
            stubWarn("Field not found: " + expr);
            return null;
        }
        Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
        if (value instanceof Number) {
            return convert((Number) value, valueKind);
        } else {
            return value;
        }
    } else if (expr instanceof StringLiteralExpr) {
        return ((StringLiteralExpr) expr).asString();
    } else if (expr instanceof BooleanLiteralExpr) {
        return ((BooleanLiteralExpr) expr).getValue();
    } else if (expr instanceof CharLiteralExpr) {
        return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
    } else if (expr instanceof DoubleLiteralExpr) {
        // double, too.
        return ((DoubleLiteralExpr) expr).asDouble();
    } else if (expr instanceof IntegerLiteralExpr) {
        return convert(((IntegerLiteralExpr) expr).asInt(), valueKind);
    } else if (expr instanceof LongLiteralExpr) {
        return convert(((LongLiteralExpr) expr).asLong(), valueKind);
    } else if (expr instanceof ClassExpr) {
        ClassExpr classExpr = (ClassExpr) expr;
        String className = classExpr.getType().toString();
        if (importedTypes.containsKey(className)) {
            return importedTypes.get(className).asType();
        }
        TypeElement typeElement = findTypeOfName(className);
        if (typeElement == null) {
            stubWarn("StubParser: unknown class name " + className);
            return null;
        }
        return typeElement.asType();
    } else if (expr instanceof NullLiteralExpr) {
        stubWarn("Null found as value for %s. Null isn't allowed as an annotation value", name);
        return null;
    } else {
        stubWarn("Unexpected annotation expression: " + expr);
        return null;
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Example 2 with ClassExpr

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

the class PrettyPrintVisitorTest method printGenericClassExpr.

@Test
public void printGenericClassExpr() {
    ClassExpr expr = JavaParser.parseExpression("Foo<String>.class");
    assertEquals("Foo<String>.class", print(expr));
}
Also used : ClassExpr(com.github.javaparser.ast.expr.ClassExpr) Test(org.junit.Test)

Example 3 with ClassExpr

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

the class ConcreteSyntaxModelTest method printArrayClassExpr.

@Test
public void printArrayClassExpr() {
    ClassExpr expr = JavaParser.parseExpression("Foo[].class");
    assertEquals("Foo[].class", print(expr));
}
Also used : ClassExpr(com.github.javaparser.ast.expr.ClassExpr) Test(org.junit.Test)

Example 4 with ClassExpr

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

the class ConcreteSyntaxModelTest method printGenericClassExpr.

@Test
public void printGenericClassExpr() {
    ClassExpr expr = JavaParser.parseExpression("Foo<String>.class");
    assertEquals("Foo<String>.class", print(expr));
}
Also used : ClassExpr(com.github.javaparser.ast.expr.ClassExpr) Test(org.junit.Test)

Example 5 with ClassExpr

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

the class ConcreteSyntaxModelTest method printSimpleClassExpr.

@Test
public void printSimpleClassExpr() {
    ClassExpr expr = JavaParser.parseExpression("Foo.class");
    assertEquals("Foo.class", print(expr));
}
Also used : ClassExpr(com.github.javaparser.ast.expr.ClassExpr) Test(org.junit.Test)

Aggregations

ClassExpr (com.github.javaparser.ast.expr.ClassExpr)7 Test (org.junit.Test)6 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1