Search in sources :

Example 6 with Annotation

use of scenelib.annotations.Annotation in project checker-framework by typetools.

the class ToIndexFileConverter method visit.

@Override
public Void visit(MethodDeclaration decl, AElement elem) {
    Type type = decl.getType();
    List<Parameter> params = decl.getParameters();
    List<TypeParameter> typeParams = decl.getTypeParameters();
    Optional<ReceiverParameter> rcvrParam = decl.getReceiverParameter();
    BlockStmt body = decl.getBody().orElse(null);
    StringBuilder sb = new StringBuilder(decl.getNameAsString()).append('(');
    AClass clazz = (AClass) elem;
    AMethod method;
    if (params != null) {
        for (Parameter param : params) {
            Type ptype = param.getType();
            sb.append(getJVML(ptype));
        }
    }
    sb.append(')').append(getJVML(type));
    method = clazz.methods.vivify(sb.toString());
    visitDecl(decl, method);
    visitType(type, method.returnType);
    if (params != null) {
        for (int i = 0; i < params.size(); i++) {
            Parameter param = params.get(i);
            AField field = method.parameters.vivify(i);
            visitType(param.getType(), field.type);
        }
    }
    if (rcvrParam.isPresent()) {
        for (AnnotationExpr expr : rcvrParam.get().getAnnotations()) {
            Annotation anno = extractAnnotation(expr);
            method.receiver.type.tlAnnotationsHere.add(anno);
        }
    }
    if (typeParams != null) {
        for (int i = 0; i < typeParams.size(); i++) {
            TypeParameter typeParam = typeParams.get(i);
            List<ClassOrInterfaceType> bounds = typeParam.getTypeBound();
            if (bounds != null) {
                for (int j = 0; j < bounds.size(); j++) {
                    ClassOrInterfaceType bound = bounds.get(j);
                    BoundLocation loc = new BoundLocation(i, j);
                    bound.accept(this, method.bounds.vivify(loc));
                }
            }
        }
    }
    return body == null ? null : body.accept(this, method);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Annotation(scenelib.annotations.Annotation) AField(scenelib.annotations.el.AField) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ReferenceType(com.github.javaparser.ast.type.ReferenceType) VoidType(com.github.javaparser.ast.type.VoidType) ArrayType(com.github.javaparser.ast.type.ArrayType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) WildcardType(com.github.javaparser.ast.type.WildcardType) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) AClass(scenelib.annotations.el.AClass) BoundLocation(scenelib.annotations.el.BoundLocation) AMethod(scenelib.annotations.el.AMethod)

Example 7 with Annotation

use of scenelib.annotations.Annotation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method removeIgnoredAnnosFromATypeElement.

/**
 * Removes all annotations that should be ignored from an ATypeElement. (See {@link
 * #shouldIgnore}).
 */
private void removeIgnoredAnnosFromATypeElement(ATypeElement typeEl, TypeUseLocation loc) {
    String firstKey = typeEl.description.toString() + typeEl.tlAnnotationsHere.toString();
    Set<String> annosToIgnoreForLocation = annosToIgnore.get(Pair.of(firstKey, loc));
    if (annosToIgnoreForLocation != null) {
        Set<Annotation> annosToRemove = new HashSet<>();
        for (Annotation anno : typeEl.tlAnnotationsHere) {
            if (annosToIgnoreForLocation.contains(anno.def().toString())) {
                annosToRemove.add(anno);
            }
        }
        typeEl.tlAnnotationsHere.removeAll(annosToRemove);
    }
    // Recursively remove ignored annotations from inner types
    if (typeEl.innerTypes.size() != 0) {
        for (ATypeElement innerType : typeEl.innerTypes.values()) {
            removeIgnoredAnnosFromATypeElement(innerType, loc);
        }
    }
}
Also used : Annotation(scenelib.annotations.Annotation) ATypeElement(scenelib.annotations.el.ATypeElement) HashSet(java.util.HashSet)

Example 8 with Annotation

use of scenelib.annotations.Annotation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method typeElementToATM.

/**
 * Updates an {@link org.checkerframework.framework.type.AnnotatedTypeMirror} to contain the
 * {@link scenelib.annotations.Annotation}s of an {@link scenelib.annotations.el.ATypeElement}.
 *
 * @param atm the AnnotatedTypeMirror to be modified
 * @param type the {@link scenelib.annotations.el.ATypeElement}
 * @param atf the annotated type factory of a given type system, whose type hierarchy will be
 *     used
 */
private void typeElementToATM(AnnotatedTypeMirror atm, ATypeElement type, AnnotatedTypeFactory atf) {
    Set<Annotation> annos = getSupportedAnnosInSet(type.tlAnnotationsHere, atf);
    for (Annotation anno : annos) {
        AnnotationMirror am = AnnotationConverter.annotationToAnnotationMirror(anno, atf.getProcessingEnv());
        atm.addAnnotation(am);
    }
    if (atm.getKind() == TypeKind.ARRAY) {
        AnnotatedArrayType aat = (AnnotatedArrayType) atm;
        for (ATypeElement innerType : type.innerTypes.values()) {
            typeElementToATM(aat.getComponentType(), innerType, atf);
        }
    }
    if (atm.getKind() == TypeKind.TYPEVAR) {
        AnnotatedTypeVariable atv = (AnnotatedTypeVariable) atm;
        for (ATypeElement innerType : type.innerTypes.values()) {
            typeElementToATM(atv.getUpperBound(), innerType, atf);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) Annotation(scenelib.annotations.Annotation) ATypeElement(scenelib.annotations.el.ATypeElement)

Example 9 with Annotation

use of scenelib.annotations.Annotation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method addAnnotationsToATypeElement.

private void addAnnotationsToATypeElement(AnnotatedTypeMirror newATM, AnnotatedTypeFactory atf, ATypeElement typeToUpdate, TypeUseLocation defLoc, AnnotationMirror am, boolean isEffectiveAnnotation) {
    Annotation anno = AnnotationConverter.annotationMirrorToAnnotation(am);
    if (anno != null) {
        typeToUpdate.tlAnnotationsHere.add(anno);
        if (isEffectiveAnnotation || shouldIgnore(am, defLoc, atf, newATM)) {
            // firstKey works as a unique identifier for each annotation
            // that should not be inserted in source code
            String firstKey = typeToUpdate.description.toString() + typeToUpdate.tlAnnotationsHere.toString();
            Pair<String, TypeUseLocation> key = Pair.of(firstKey, defLoc);
            Set<String> annosIgnored = annosToIgnore.get(key);
            if (annosIgnored == null) {
                annosIgnored = new HashSet<>();
                annosToIgnore.put(key, annosIgnored);
            }
            annosIgnored.add(anno.def().toString());
        }
    }
}
Also used : TypeUseLocation(org.checkerframework.framework.qual.TypeUseLocation) Annotation(scenelib.annotations.Annotation)

Example 10 with Annotation

use of scenelib.annotations.Annotation in project checker-framework by typetools.

the class WholeProgramInferenceScenesHelper method getSupportedAnnosInSet.

/**
 * Returns a subset of annosSet, consisting of the annotations supported by atf.
 */
private Set<Annotation> getSupportedAnnosInSet(Set<Annotation> annosSet, AnnotatedTypeFactory atf) {
    Set<Annotation> output = new HashSet<>();
    Set<Class<? extends java.lang.annotation.Annotation>> supportedAnnos = atf.getSupportedTypeQualifiers();
    for (Annotation anno : annosSet) {
        for (Class<? extends java.lang.annotation.Annotation> clazz : supportedAnnos) {
            // TODO: Remove comparison by name, and make this routine more efficient.
            if (clazz.getName().equals(anno.def.name)) {
                output.add(anno);
            }
        }
    }
    return output;
}
Also used : AClass(scenelib.annotations.el.AClass) Annotation(scenelib.annotations.Annotation) HashSet(java.util.HashSet)

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