use of scenelib.annotations.Annotation in project checker-framework by typetools.
the class AnnotationConverter method annotationMirrorToAnnotation.
/**
* Converts an {@link javax.lang.model.element.AnnotationMirror} into an {@link
* scenelib.annotations.Annotation}.
*/
protected static Annotation annotationMirrorToAnnotation(AnnotationMirror am) {
AnnotationDef def = new AnnotationDef(AnnotationUtils.annotationName(am));
Map<String, AnnotationFieldType> fieldTypes = new HashMap<>();
// Handling cases where there are fields in annotations.
for (ExecutableElement ee : am.getElementValues().keySet()) {
AnnotationFieldType aft = getAnnotationFieldType(ee, am.getElementValues().get(ee).getValue());
if (aft == null)
return null;
// Here we just add the type of the field into fieldTypes.
fieldTypes.put(ee.getSimpleName().toString(), aft);
}
def.setFieldTypes(fieldTypes);
// Now, we handle the values of those types below
Map<? extends ExecutableElement, ? extends AnnotationValue> values = am.getElementValues();
Map<String, Object> newValues = new HashMap<>();
for (ExecutableElement ee : values.keySet()) {
Object value = values.get(ee).getValue();
if (value instanceof List) {
@SuppressWarnings("unchecked") List<Object> valueList = (List<Object>) value;
List<Object> newList = new ArrayList<>();
// Converting each AnnotatedValue to its respective Java type:
for (Object o : valueList) {
newList.add(((AnnotationValue) o).getValue());
}
value = newList;
}
newValues.put(ee.getSimpleName().toString(), value);
}
Annotation out = new Annotation(def, newValues);
return out;
}
use of scenelib.annotations.Annotation in project checker-framework by typetools.
the class WholeProgramInferenceScenesHelper method updateTypeElementFromATM.
/**
* Updates an {@link scenelib.annotations.el.ATypeElement} to have the annotations of an {@link
* org.checkerframework.framework.type.AnnotatedTypeMirror} passed as argument. Annotations in
* the original set that should be ignored (see {@link #shouldIgnore}) are not added to the
* resulting set. This method also checks if the AnnotatedTypeMirror has explicit annotations in
* source code, and if that is the case no annotations are added for that location.
*
* <p>This method removes from the ATypeElement all annotations supported by atf before
* inserting new ones. It is assumed that every time this method is called, the
* AnnotatedTypeMirror has a better type estimate for the ATypeElement. Therefore, it is not a
* problem to remove all annotations before inserting the new annotations.
*
* @param newATM the AnnotatedTypeMirror whose annotations will be added to the ATypeElement
* @param curATM used to check if the element which will be updated has explicit annotations in
* source code
* @param atf the annotated type factory of a given type system, whose type hierarchy will be
* used
* @param typeToUpdate the ATypeElement which will be updated
* @param idx used to write annotations on compound types of an ATypeElement
* @param defLoc the location where the annotation will be added
*/
private void updateTypeElementFromATM(AnnotatedTypeMirror newATM, AnnotatedTypeMirror curATM, AnnotatedTypeFactory atf, ATypeElement typeToUpdate, int idx, TypeUseLocation defLoc) {
// The others stay intact.
if (idx == 1) {
// This if avoids clearing the annotations multiple times in cases
// of type variables and compound types.
Set<Annotation> annosToRemove = getSupportedAnnosInSet(typeToUpdate.tlAnnotationsHere, atf);
// This method may be called consecutive times for the same ATypeElement.
// Each time it is called, the AnnotatedTypeMirror has a better type
// estimate for the ATypeElement. Therefore, it is not a problem to remove
// all annotations before inserting the new annotations.
typeToUpdate.tlAnnotationsHere.removeAll(annosToRemove);
}
// Only update the ATypeElement if there are no explicit annotations
if (curATM.getExplicitAnnotations().size() == 0) {
for (AnnotationMirror am : newATM.getAnnotations()) {
addAnnotationsToATypeElement(newATM, atf, typeToUpdate, defLoc, am, curATM.hasEffectiveAnnotation(am));
}
} else if (curATM.getKind() == TypeKind.TYPEVAR) {
// vars upper bound from being inserted.
for (AnnotationMirror am : newATM.getAnnotations()) {
if (curATM.getAnnotationInHierarchy(am) != null) {
// in the same hierarchy.
break;
}
addAnnotationsToATypeElement(newATM, atf, typeToUpdate, defLoc, am, curATM.hasEffectiveAnnotation(am));
}
}
// Recursively update compound type and type variable type if they exist.
if (newATM.getKind() == TypeKind.ARRAY && curATM.getKind() == TypeKind.ARRAY) {
AnnotatedArrayType newAAT = (AnnotatedArrayType) newATM;
AnnotatedArrayType oldAAT = (AnnotatedArrayType) curATM;
updateTypeElementFromATM(newAAT.getComponentType(), oldAAT.getComponentType(), atf, typeToUpdate.innerTypes.vivify(new InnerTypeLocation(TypeAnnotationPosition.getTypePathFromBinary(Collections.nCopies(2 * idx, 0)))), idx + 1, defLoc);
}
}
use of scenelib.annotations.Annotation in project checker-framework by typetools.
the class AddAnnotatedFor method addAnnotatedFor.
public static void addAnnotatedFor(AScene scene) {
for (AClass clazz : new HashSet<>(scene.classes.values())) {
Set<String> annotatedFor = new HashSet<>();
clazz.accept(annotatedForVisitor, annotatedFor);
if (!annotatedFor.isEmpty()) {
// Set eliminates duplicates, but it must be converted to List;
// for whatever reason, IndexFileWriter recognizes array
// arguments only in List form.
List<String> annotatedForList = new ArrayList<>(annotatedFor);
clazz.tlAnnotationsHere.add(new Annotation(adAnnotatedFor, Annotations.valueFieldOnly(annotatedForList)));
}
}
}
use of scenelib.annotations.Annotation in project checker-framework by typetools.
the class ToIndexFileConverter method visit.
@Override
public Void visit(VariableDeclarationExpr expr, AElement elem) {
List<AnnotationExpr> annos = expr.getAnnotations();
AMethod method = (AMethod) elem;
List<VariableDeclarator> varDecls = expr.getVariables();
for (int i = 0; i < varDecls.size(); i++) {
VariableDeclarator decl = varDecls.get(i);
LocalLocation loc = new LocalLocation(decl.getNameAsString(), i);
AField field = method.body.locals.vivify(loc);
visitType(expr.getCommonType(), field.type);
if (annos != null) {
for (AnnotationExpr annoExpr : annos) {
Annotation anno = extractAnnotation(annoExpr);
field.tlAnnotationsHere.add(anno);
}
}
}
return null;
}
use of scenelib.annotations.Annotation in project checker-framework by typetools.
the class ToIndexFileConverter method extractAnnotation.
/**
* Builds simplified annotation from its declaration. Only the name is included, because
* stubfiles do not generally have access to the full definitions of annotations.
*/
private static Annotation extractAnnotation(AnnotationExpr expr) {
// leave off leading '@'
String exprName = expr.toString().substring(1);
// the JDK adds, apparently for profiling.
if (exprName.contains("+")) {
return null;
}
AnnotationDef def = new AnnotationDef(exprName);
def.setFieldTypes(Collections.emptyMap());
return new Annotation(def, Collections.emptyMap());
}
Aggregations