Search in sources :

Example 1 with Annotation

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;
}
Also used : HashMap(java.util.HashMap) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Annotation(scenelib.annotations.Annotation) AnnotationDef(scenelib.annotations.el.AnnotationDef) AnnotationFieldType(scenelib.annotations.field.AnnotationFieldType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Annotation

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);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) InnerTypeLocation(scenelib.annotations.el.InnerTypeLocation) Annotation(scenelib.annotations.Annotation)

Example 3 with Annotation

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)));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AClass(scenelib.annotations.el.AClass) Annotation(scenelib.annotations.Annotation) HashSet(java.util.HashSet)

Example 4 with Annotation

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;
}
Also used : AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) LocalLocation(scenelib.annotations.el.LocalLocation) AMethod(scenelib.annotations.el.AMethod) Annotation(scenelib.annotations.Annotation) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AField(scenelib.annotations.el.AField)

Example 5 with Annotation

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());
}
Also used : Annotation(scenelib.annotations.Annotation) AnnotationDef(scenelib.annotations.el.AnnotationDef)

Aggregations

Annotation (scenelib.annotations.Annotation)13 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)5 AClass (scenelib.annotations.el.AClass)4 HashSet (java.util.HashSet)3 AField (scenelib.annotations.el.AField)3 AMethod (scenelib.annotations.el.AMethod)3 Parameter (com.github.javaparser.ast.body.Parameter)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ArrayType (com.github.javaparser.ast.type.ArrayType)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 Type (com.github.javaparser.ast.type.Type)2 TypeParameter (com.github.javaparser.ast.type.TypeParameter)2 VoidType (com.github.javaparser.ast.type.VoidType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 ArrayList (java.util.ArrayList)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)2