use of org.checkerframework.checker.signature.qual.FullyQualifiedName in project checker-framework by typetools.
the class AnnotationFileParser 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.
*
* @param annotation syntax tree for an annotation
* @param allAnnotations map from simple name to annotation definition; side-effected by this
* method
* @return the AnnotationMirror for the annotation, or null if it cannot be built
*/
@Nullable
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, TypeElement> allAnnotations) {
// https://tinyurl.com/cfissue/3094
@SuppressWarnings("signature") @FullyQualifiedName String annoNameFq = annotation.getNameAsString();
TypeElement annoTypeElt = allAnnotations.get(annoNameFq);
if (annoTypeElt == null) {
// If the annotation was not imported, then #getImportedAnnotations did not add it to the
// allAnnotations field. This code adds the annotation when it is encountered (i.e. here).
// Note that this does not call AnnotationFileParser#getTypeElement to avoid a spurious
// diagnostic if the annotation is actually unknown.
annoTypeElt = elements.getTypeElement(annoNameFq);
if (annoTypeElt == null) {
// Not a supported annotation -> ignore
return null;
}
putAllNew(allAnnotations, createNameToAnnotationMap(Collections.singletonList(annoTypeElt)));
}
// not anonymous, so name is not empty
@SuppressWarnings("signature") @CanonicalName String annoName = annoTypeElt.getQualifiedName().toString();
if (annotation instanceof MarkerAnnotationExpr) {
return AnnotationBuilder.fromName(elements, annoName);
} else if (annotation instanceof NormalAnnotationExpr) {
NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
List<MemberValuePair> pairs = nrmanno.getPairs();
if (pairs != null) {
for (MemberValuePair mvp : pairs) {
String member = mvp.getNameAsString();
Expression exp = mvp.getValue();
try {
builderAddElement(builder, member, exp);
} catch (AnnotationFileParserException e) {
warn(exp, "For annotation %s, could not add %s=%s because %s", annotation, member, exp, e.getMessage());
return null;
}
}
}
return builder.build();
} else if (annotation instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
Expression valExpr = sglanno.getMemberValue();
try {
builderAddElement(builder, "value", valExpr);
} catch (AnnotationFileParserException e) {
warn(valExpr, "For annotation %s, could not add value=%s because %s", annotation, valExpr, e.getMessage());
return null;
}
return builder.build();
} else {
throw new BugInCF("AnnotationFileParser: unknown annotation type: " + annotation);
}
}
use of org.checkerframework.checker.signature.qual.FullyQualifiedName in project checker-framework by typetools.
the class AnnotationFileParser method getValueOfExpressionInAnnotation.
/**
* Returns the value of {@code expr}.
*
* @param name the name of an annotation element/argument, used for diagnostic messages
* @param expr the expression to determine the value of
* @param valueKind the type of the result
* @return the value of {@code expr}
* @throws AnnotationFileParserException if a problem occurred getting the value
*/
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) throws AnnotationFileParserException {
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) {
throw new AnnotationFileParserException(String.format("variable %s not found", expr));
}
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).asNumber(), valueKind);
} else if (expr instanceof LongLiteralExpr) {
return convert(((LongLiteralExpr) expr).asNumber(), valueKind);
} else if (expr instanceof UnaryExpr) {
switch(expr.toString()) {
// the smallest member of an integral type is larger than the largest value.
case "-9223372036854775808L":
case "-9223372036854775808l":
return convert(Long.MIN_VALUE, valueKind, false);
case "-2147483648":
return convert(Integer.MIN_VALUE, valueKind, false);
default:
if (((UnaryExpr) expr).getOperator() == UnaryExpr.Operator.MINUS) {
Object value = getValueOfExpressionInAnnotation(name, ((UnaryExpr) expr).getExpression(), valueKind);
if (value instanceof Number) {
return convert((Number) value, valueKind, true);
}
}
throw new AnnotationFileParserException("unexpected Unary annotation expression: " + expr);
}
} else if (expr instanceof ClassExpr) {
ClassExpr classExpr = (ClassExpr) expr;
// Type.toString(): @FullyQualifiedName
@SuppressWarnings("signature") @FullyQualifiedName String className = classExpr.getType().toString();
if (importedTypes.containsKey(className)) {
return importedTypes.get(className).asType();
}
TypeElement typeElement = findTypeOfName(className);
if (typeElement == null) {
throw new AnnotationFileParserException("unknown class name " + className);
}
return typeElement.asType();
} else if (expr instanceof NullLiteralExpr) {
throw new AnnotationFileParserException("Illegal annotation value null, for " + name);
} else {
throw new AnnotationFileParserException("Unexpected annotation expression: " + expr);
}
}
use of org.checkerframework.checker.signature.qual.FullyQualifiedName in project checker-framework by typetools.
the class SourceChecker method shouldSkipUses.
// /////////////////////////////////////////////////////////////////////////
// / Skipping uses and defs
// /
/**
* Tests whether the class owner of the passed element is an unannotated class and matches the
* pattern specified in the {@code checker.skipUses} property.
*
* @param element an element
* @return true iff the enclosing class of element should be skipped
*/
public final boolean shouldSkipUses(Element element) {
if (element == null) {
return false;
}
TypeElement typeElement = ElementUtils.enclosingTypeElement(element);
if (typeElement == null) {
throw new BugInCF("enclosingTypeElement(%s [%s]) => null%n", element, element.getClass());
}
// TypeElement.toString(): @FullyQualifiedName
@SuppressWarnings("signature:assignment") @FullyQualifiedName String name = typeElement.toString();
return shouldSkipUses(name);
}
use of org.checkerframework.checker.signature.qual.FullyQualifiedName in project checker-framework by typetools.
the class InsertAjavaAnnotations method getImportedAnnotations.
/**
* Returns all annotations imported by the annotation file as a mapping from simple and qualified
* names to TypeElements.
*
* @param cu compilation unit to extract imports from
* @return a map from names to TypeElement, for all annotations imported by the annotation file.
* Two entries for each annotation: one for the simple name and another for the
* fully-qualified name, with the same value.
*/
private Map<String, TypeElement> getImportedAnnotations(CompilationUnit cu) {
if (cu.getImports() == null) {
return Collections.emptyMap();
}
Map<String, TypeElement> result = new HashMap<>();
for (ImportDeclaration importDecl : cu.getImports()) {
if (importDecl.isAsterisk()) {
@// https://tinyurl.com/cfissue/3094:
SuppressWarnings(// https://tinyurl.com/cfissue/3094:
"signature") @DotSeparatedIdentifiers String imported = importDecl.getName().toString();
if (importDecl.isStatic()) {
// Wildcard import of members of a type (class or interface)
TypeElement element = elements.getTypeElement(imported);
if (element != null) {
// Find nested annotations
result.putAll(AnnotationFileParser.annosInType(element));
}
} else {
// Wildcard import of members of a package
PackageElement element = elements.getPackageElement(imported);
if (element != null) {
result.putAll(AnnotationFileParser.annosInPackage(element));
}
}
} else {
@// importDecl is non-wildcard, so its name is
SuppressWarnings(// importDecl is non-wildcard, so its name is
"signature") @FullyQualifiedName String imported = importDecl.getNameAsString();
TypeElement importType = elements.getTypeElement(imported);
if (importType != null && importType.getKind() == ElementKind.ANNOTATION_TYPE) {
TypeElement annoElt = elements.getTypeElement(imported);
if (annoElt != null) {
result.put(annoElt.getSimpleName().toString(), annoElt);
}
}
}
}
return result;
}
Aggregations