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;
}
}
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());
}
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);
}
Aggregations