Search in sources :

Example 1 with FieldAccessExpr

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

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

the class Issue300 method fieldAccessIssue.

@Test
public void fieldAccessIssue() throws ParseException, FileNotFoundException {
    String pathToSourceFile = adaptPath("src/test/resources/issue300/Issue300.java");
    CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile));
    final FieldAccessExpr fieldAccess = Navigator.findNodeOfGivenClass(cu, FieldAccessExpr.class);
    assertNotNull(fieldAccess);
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(adaptPath(new File("src/test/resources/issue300"))));
    final JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    final SymbolReference<? extends ResolvedValueDeclaration> ref = javaParserFacade.solve(fieldAccess);
    assertEquals(ResolvedPrimitiveType.INT, ref.getCorrespondingDeclaration().getType().asPrimitive());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 3 with FieldAccessExpr

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

the class AbstractJavaParserContext method getParent.

@Override
public final Context getParent() {
    Node parent = wrappedNode.getParentNode().orElse(null);
    if (parent instanceof MethodCallExpr) {
        MethodCallExpr parentCall = (MethodCallExpr) parent;
        boolean found = false;
        if (parentCall.getArguments() != null) {
            for (Expression expression : parentCall.getArguments()) {
                if (expression == wrappedNode) {
                    found = true;
                }
            }
        }
        if (found) {
            Node notMethod = parent;
            while (notMethod instanceof MethodCallExpr) {
                notMethod = requireParentNode(notMethod);
            }
            return JavaParserFactory.getContext(notMethod, typeSolver);
        }
    }
    Node notMethod = parent;
    while (notMethod instanceof MethodCallExpr || notMethod instanceof FieldAccessExpr) {
        notMethod = notMethod.getParentNode().orElse(null);
    }
    if (notMethod == null) {
        return null;
    }
    return JavaParserFactory.getContext(notMethod, typeSolver);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Navigator.getParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode) Navigator.requireParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode) Node(com.github.javaparser.ast.Node) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 Node (com.github.javaparser.ast.Node)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)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 Navigator.getParentNode (com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode)1 Navigator.requireParentNode (com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode)1 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)1 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)1 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)1 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)1