Search in sources :

Example 26 with AnnotatedDeclaredType

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

the class StubParser method annotate.

/**
 * Add to {@code atype}:
 *
 * <ol>
 *   <li>the annotations from {@code typeDef}, and
 *   <li>any type annotations that parsed as declaration annotations (ie those in {@code
 *       declAnnos}).
 * </ol>
 *
 * @param atype annotated type to which to add annotations
 * @param typeDef parsed type
 * @param declAnnos annotations stored on the declaration of the variable with this type, or
 *     null
 */
private void annotate(AnnotatedTypeMirror atype, Type typeDef, NodeList<AnnotationExpr> declAnnos) {
    if (atype.getKind() == TypeKind.ARRAY) {
        annotateAsArray((AnnotatedArrayType) atype, (ReferenceType) typeDef, declAnnos);
        return;
    }
    clearAnnotations(atype, typeDef);
    // Primary annotations for the type of a variable declaration are not stored in typeDef, but
    // rather as declaration annotations (passed as declAnnos to this method).  But, if typeDef
    // is not the type of a variable, then the primary annotations are stored in typeDef.
    NodeList<AnnotationExpr> primaryAnnotations;
    if (typeDef.getAnnotations().isEmpty() && declAnnos != null) {
        primaryAnnotations = declAnnos;
    } else {
        primaryAnnotations = typeDef.getAnnotations();
    }
    if (atype.getKind() != TypeKind.WILDCARD) {
        // The primary annotation on a wildcard applies to the super or extends bound and
        // are added below.
        annotate(atype, primaryAnnotations);
    }
    switch(atype.getKind()) {
        case DECLARED:
            ClassOrInterfaceType declType = unwrapDeclaredType(typeDef);
            if (declType == null) {
                break;
            }
            AnnotatedDeclaredType adeclType = (AnnotatedDeclaredType) atype;
            if (declType.getTypeArguments().isPresent() && !declType.getTypeArguments().get().isEmpty() && !adeclType.getTypeArguments().isEmpty()) {
                assert declType.getTypeArguments().get().size() == adeclType.getTypeArguments().size() : String.format("Mismatch in type argument size between %s (%d) and %s (%d)", declType, declType.getTypeArguments().get().size(), adeclType, adeclType.getTypeArguments().size());
                for (int i = 0; i < declType.getTypeArguments().get().size(); ++i) {
                    annotate(adeclType.getTypeArguments().get(i), declType.getTypeArguments().get().get(i), null);
                }
            }
            break;
        case WILDCARD:
            AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) atype;
            WildcardType wildcardDef = (WildcardType) typeDef;
            if (wildcardDef.getExtendedType().isPresent()) {
                annotate(wildcardType.getExtendsBound(), wildcardDef.getExtendedType().get(), null);
                annotate(wildcardType.getSuperBound(), primaryAnnotations);
            } else if (wildcardDef.getSuperType().isPresent()) {
                annotate(wildcardType.getSuperBound(), wildcardDef.getSuperType().get(), null);
                annotate(wildcardType.getExtendsBound(), primaryAnnotations);
            } else {
                annotate(atype, primaryAnnotations);
            }
            break;
        case TYPEVAR:
            // Add annotations from the declaration of the TypeVariable
            AnnotatedTypeVariable typeVarUse = (AnnotatedTypeVariable) atype;
            for (AnnotatedTypeVariable typePar : typeParameters) {
                if (typePar.getUnderlyingType() == atype.getUnderlyingType()) {
                    AnnotatedTypeMerger.merge(typePar.getUpperBound(), typeVarUse.getUpperBound());
                    AnnotatedTypeMerger.merge(typePar.getLowerBound(), typeVarUse.getLowerBound());
                }
            }
            break;
        default:
    }
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.github.javaparser.ast.type.WildcardType) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 27 with AnnotatedDeclaredType

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

the class StubParser method annotateSupertypes.

private void annotateSupertypes(ClassOrInterfaceDeclaration typeDecl, AnnotatedDeclaredType type) {
    if (typeDecl.getExtendedTypes() != null) {
        for (ClassOrInterfaceType superType : typeDecl.getExtendedTypes()) {
            AnnotatedDeclaredType foundType = findType(superType, type.directSuperTypes());
            if (foundType == null) {
                throw new Error("StubParser: could not find superclass " + superType + " from type " + type + LINE_SEPARATOR + "Stub file does not match bytecode");
            }
            annotate(foundType, superType, null);
        }
    }
    if (typeDecl.getImplementedTypes() != null) {
        for (ClassOrInterfaceType superType : typeDecl.getImplementedTypes()) {
            AnnotatedDeclaredType foundType = findType(superType, type.directSuperTypes());
            if (foundType == null) {
                throw new Error("StubParser: could not find superinterface " + superType + " from type " + type + LINE_SEPARATOR + "Stub file does not match bytecode");
            }
            annotate(foundType, superType, null);
        }
    }
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 28 with AnnotatedDeclaredType

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

the class StubParser method findType.

/**
 * Return the element of {@code types} whose name matches {@code type}.
 */
private AnnotatedDeclaredType findType(ClassOrInterfaceType type, List<AnnotatedDeclaredType> types) {
    String typeString = type.getNameAsString();
    for (AnnotatedDeclaredType superType : types) {
        if (superType.getUnderlyingType().asElement().getSimpleName().contentEquals(typeString)) {
            return superType;
        }
    }
    stubWarnNotFound("Supertype " + typeString + " not found");
    if (debugStubParser) {
        stubDebug("Supertypes that were searched:");
        for (AnnotatedDeclaredType superType : types) {
            stubDebug(String.format("  %s", superType));
        }
    }
    return null;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 29 with AnnotatedDeclaredType

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

the class AnnotatedTypeCopier method visitDeclared.

@Override
public AnnotatedTypeMirror visitDeclared(AnnotatedDeclaredType original, IdentityHashMap<AnnotatedTypeMirror, AnnotatedTypeMirror> originalToCopy) {
    if (originalToCopy.containsKey(original)) {
        return originalToCopy.get(original);
    }
    final AnnotatedDeclaredType copy = (AnnotatedDeclaredType) AnnotatedTypeMirror.createType(original.getUnderlyingType(), original.atypeFactory, original.isDeclaration());
    maybeCopyPrimaryAnnotations(original, copy);
    originalToCopy.put(original, copy);
    if (original.wasRaw()) {
        copy.setWasRaw();
    }
    if (original.enclosingType != null) {
        copy.enclosingType = (AnnotatedDeclaredType) visit(original.enclosingType, originalToCopy);
    }
    if (original.typeArgs != null) {
        final List<AnnotatedTypeMirror> copyTypeArgs = new ArrayList<>();
        for (final AnnotatedTypeMirror typeArg : original.getTypeArguments()) {
            copyTypeArgs.add(visit(typeArg, originalToCopy));
        }
        copy.setTypeArguments(copyTypeArgs);
    }
    return copy;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList)

Example 30 with AnnotatedDeclaredType

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

the class AnnotatedTypeCopier method visitIntersection.

@Override
public AnnotatedTypeMirror visitIntersection(AnnotatedIntersectionType original, IdentityHashMap<AnnotatedTypeMirror, AnnotatedTypeMirror> originalToCopy) {
    if (originalToCopy.containsKey(original)) {
        return originalToCopy.get(original);
    }
    final AnnotatedIntersectionType copy = (AnnotatedIntersectionType) AnnotatedTypeMirror.createType(original.getUnderlyingType(), original.atypeFactory, original.isDeclaration());
    maybeCopyPrimaryAnnotations(original, copy);
    originalToCopy.put(original, copy);
    if (original.supertypes != null) {
        final List<AnnotatedDeclaredType> copySupertypes = new ArrayList<>();
        for (final AnnotatedDeclaredType supertype : original.supertypes) {
            copySupertypes.add((AnnotatedDeclaredType) visit(supertype, originalToCopy));
        }
        copy.supertypes = Collections.unmodifiableList(copySupertypes);
    }
    return copy;
}
Also used : AnnotatedIntersectionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList)

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