Search in sources :

Example 11 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class ReportVisitor method visitMethodInvocation.

@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    ExecutableElement method = TreeUtils.elementFromUse(node);
    checkReportUse(node, method);
    boolean report = this.atypeFactory.getDeclAnnotation(method, ReportCall.class) != null;
    if (!report) {
        // Find all methods that are overridden by the called method
        Map<AnnotatedDeclaredType, ExecutableElement> overriddenMethods = AnnotatedTypes.overriddenMethods(elements, atypeFactory, method);
        for (Map.Entry<AnnotatedDeclaredType, ExecutableElement> pair : overriddenMethods.entrySet()) {
            // AnnotatedDeclaredType overriddenType = pair.getKey();
            ExecutableElement exe = pair.getValue();
            report = this.atypeFactory.getDeclAnnotation(exe, ReportCall.class) != null;
            if (report) {
                // Always report the element that has the annotation.
                // Alternative would be to always report the initial element.
                method = exe;
                break;
            }
        }
    }
    if (report) {
        checker.report(Result.failure("methodcall", node, ElementUtils.getVerboseName(method)), node);
    }
    return super.visitMethodInvocation(node, p);
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ReportCall(org.checkerframework.common.util.report.qual.ReportCall) Map(java.util.Map) ReportOverride(org.checkerframework.common.util.report.qual.ReportOverride)

Example 12 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class GenericAnnotatedTypeFactory method getResultingTypeOfConstructorMemberReference.

/**
 * Gets the type of the resulting constructor call of a MemberReferenceTree.
 *
 * @param memberReferenceTree MemberReferenceTree where the member is a constructor
 * @param constructorType AnnotatedExecutableType of the declaration of the constructor
 * @return AnnotatedTypeMirror of the resulting type of the constructor
 */
public AnnotatedTypeMirror getResultingTypeOfConstructorMemberReference(MemberReferenceTree memberReferenceTree, AnnotatedExecutableType constructorType) {
    assert memberReferenceTree.getMode() == MemberReferenceTree.ReferenceMode.NEW;
    // The return type for constructors should only have explicit annotations from the
    // constructor.  Recreate some of the logic from TypeFromTree.visitNewClass here.
    // The return type of the constructor will be the type of the expression of the member
    // reference tree.
    AnnotatedDeclaredType constructorReturnType = (AnnotatedDeclaredType) fromTypeTree(memberReferenceTree.getQualifierExpression());
    // Keep only explicit annotations and those from @Poly
    AnnotatedTypes.copyOnlyExplicitConstructorAnnotations(this, constructorReturnType, constructorType);
    // Now add back defaulting.
    addComputedTypeAnnotations(memberReferenceTree.getQualifierExpression(), constructorReturnType);
    return constructorReturnType;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 13 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class TypeFromExpressionVisitor method visitNewClass.

/**
 * Creates an AnnotatedDeclaredType for the NewClassTree and adds, for each hierarchy, one of:
 *
 * <ul>
 *   <li>an explicit annotation on the new class expression ({@code new @HERE MyClass()}), or
 *   <li>an explicit annotation on the declaration of the class ({@code @HERE class MyClass
 *       {}}), or
 *   <li>an explicit annotation on the declaration of the constructor ({@code @HERE public
 *       MyClass() {}}), or
 *   <li>no annotation for this hierarchy.
 * </ul>
 *
 * @param node NewClassTree
 * @param f the type factory
 * @return AnnotatedDeclaredType of {@code node}
 */
@Override
public AnnotatedTypeMirror visitNewClass(NewClassTree node, AnnotatedTypeFactory f) {
    // constructorFromUse return type has implicits
    // so use fromNewClass which does diamond inference and only
    // contains explicit annotations and those inherited from the class declaration
    AnnotatedDeclaredType type = f.fromNewClass(node);
    // TODO: is there more to check? Can one annotate them?
    if (isNewEnum(type)) {
        return type;
    }
    // Add annotations that are on the constructor declaration.
    // constructorFromUse gives us resolution of polymorphic qualifiers.
    // However, it also applies defaulting, so we might apply too many qualifiers.
    // Therefore, ensure to only add the qualifiers that are explicitly on
    // the constructor, but then take the possibly substituted qualifier.
    AnnotatedExecutableType ex = f.constructorFromUse(node).first;
    AnnotatedTypes.copyOnlyExplicitConstructorAnnotations(f, type, ex);
    return type;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 14 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class AnnotatedTypes method overriddenMethods.

/**
 * A utility method that takes a Method element and returns a set of all elements that this
 * method overrides (as {@link ExecutableElement}s)
 *
 * @param method the overriding method
 * @return an unmodifiable set of {@link ExecutableElement}s representing the elements that
 *     method overrides
 */
public static Map<AnnotatedDeclaredType, ExecutableElement> overriddenMethods(Elements elements, AnnotatedTypeFactory atypeFactory, ExecutableElement method) {
    final TypeElement elem = (TypeElement) method.getEnclosingElement();
    final AnnotatedDeclaredType type = atypeFactory.getAnnotatedType(elem);
    final Collection<AnnotatedDeclaredType> supertypes = getSuperTypes(type);
    return overriddenMethods(elements, method, supertypes);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 15 with AnnotatedDeclaredType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.

the class AnnotatedTypes method asOuterSuper.

/**
 * Return the base type of type or any of its outer types that starts with the given type. If
 * none exists, return null.
 *
 * @param type a type
 * @param superType a type
 */
private static AnnotatedTypeMirror asOuterSuper(Types types, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror type, AnnotatedTypeMirror superType) {
    if (type.getKind() == TypeKind.DECLARED) {
        AnnotatedDeclaredType dt = (AnnotatedDeclaredType) type;
        AnnotatedDeclaredType enclosingType = dt;
        TypeMirror superTypeMirror = types.erasure(superType.getUnderlyingType());
        while (enclosingType != null) {
            TypeMirror enclosingTypeMirror = types.erasure(enclosingType.getUnderlyingType());
            if (types.isSubtype(enclosingTypeMirror, superTypeMirror)) {
                dt = enclosingType;
                break;
            }
            enclosingType = enclosingType.getEnclosingType();
        }
        if (enclosingType == null) {
            // checker.message(Kind.WARNING, msg);
            return superType;
        }
        return asSuper(atypeFactory, dt, superType);
    }
    return asSuper(atypeFactory, type, superType);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Aggregations

AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)72 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)26 ArrayList (java.util.ArrayList)19 ExpressionTree (com.sun.source.tree.ExpressionTree)18 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)18 MethodTree (com.sun.source.tree.MethodTree)17 Tree (com.sun.source.tree.Tree)16 ClassTree (com.sun.source.tree.ClassTree)14 VariableTree (com.sun.source.tree.VariableTree)14 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)13 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)12 NewClassTree (com.sun.source.tree.NewClassTree)12 ExecutableElement (javax.lang.model.element.ExecutableElement)11 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)10 NewArrayTree (com.sun.source.tree.NewArrayTree)9 ReturnTree (com.sun.source.tree.ReturnTree)9 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)9 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)9 AssignmentTree (com.sun.source.tree.AssignmentTree)8