Search in sources :

Example 46 with AnnotatedDeclaredType

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

the class TypeFromExpressionVisitor method visitIdentifier.

@Override
public AnnotatedTypeMirror visitIdentifier(IdentifierTree node, AnnotatedTypeFactory f) {
    if (node.getName().contentEquals("this") || node.getName().contentEquals("super")) {
        AnnotatedDeclaredType res = f.getSelfType(node);
        return res;
    }
    Element elt = TreeUtils.elementFromUse(node);
    AnnotatedTypeMirror selfType = f.getImplicitReceiverType(node);
    if (selfType != null) {
        return AnnotatedTypes.asMemberOf(f.types, f, selfType, elt).asUse();
    }
    return f.getAnnotatedType(elt);
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element)

Example 47 with AnnotatedDeclaredType

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

the class TypeFromMemberVisitor method inferLambdaParamAnnotations.

/**
 * @return the type of the lambda parameter or null if paramElement is not a lambda parameter
 */
private static AnnotatedTypeMirror inferLambdaParamAnnotations(AnnotatedTypeFactory f, AnnotatedTypeMirror lambdaParam, Element paramElement) {
    if (paramElement.getKind() != ElementKind.PARAMETER || f.declarationFromElement(paramElement) == null || f.getPath(f.declarationFromElement(paramElement)) == null || f.getPath(f.declarationFromElement(paramElement)).getParentPath() == null) {
        return null;
    }
    Tree declaredInTree = f.getPath(f.declarationFromElement(paramElement)).getParentPath().getLeaf();
    if (declaredInTree.getKind() == Kind.LAMBDA_EXPRESSION) {
        LambdaExpressionTree lambdaDecl = (LambdaExpressionTree) declaredInTree;
        int index = lambdaDecl.getParameters().indexOf(f.declarationFromElement(paramElement));
        Pair<AnnotatedDeclaredType, AnnotatedExecutableType> res = f.getFnInterfaceFromTree(lambdaDecl);
        AnnotatedExecutableType functionType = res.second;
        AnnotatedTypeMirror funcTypeParam = functionType.getParameterTypes().get(index);
        if (TreeUtils.isImplicitlyTypedLambda(declaredInTree)) {
            if (f.types.isSubtype(funcTypeParam.actualType, lambdaParam.actualType)) {
                // (#979) isn't implement, check first.
                return AnnotatedTypes.asSuper(f, funcTypeParam, lambdaParam);
            }
            lambdaParam.addMissingAnnotations(funcTypeParam.getAnnotations());
            return lambdaParam;
        } else {
            // The lambda expression is explicitly typed, so the parameters have declared types:
            // (String s) -> ...
            // The declared type may or may not have explicit annotations.
            // If it does not have an annotation for a hierarchy, then copy the annotation from
            // the function type rather than use usual defaulting rules.
            // Note lambdaParam is a super type of funcTypeParam, so only primary annotations
            // can be copied.
            lambdaParam.addMissingAnnotations(funcTypeParam.getAnnotations());
            return lambdaParam;
        }
    }
    return null;
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Tree(com.sun.source.tree.Tree)

Example 48 with AnnotatedDeclaredType

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

the class TypeFromTree method fromClassTree.

/**
 * @return an AnnotatedDeclaredType representing the input ClassTree
 */
public static AnnotatedDeclaredType fromClassTree(final AnnotatedTypeFactory typeFactory, final ClassTree tree) {
    abortIfTreeIsNull(typeFactory, tree);
    final AnnotatedDeclaredType type = (AnnotatedDeclaredType) classVisitor.visit(tree, typeFactory);
    abortIfTypeIsExecutable(typeFactory, tree, type);
    return type;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 49 with AnnotatedDeclaredType

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

the class TypeFromTypeTreeVisitor method visitTypeParameter.

@Override
public AnnotatedTypeMirror visitTypeParameter(TypeParameterTree node, AnnotatedTypeFactory f) {
    List<AnnotatedTypeMirror> bounds = new ArrayList<>(node.getBounds().size());
    for (Tree t : node.getBounds()) {
        AnnotatedTypeMirror bound;
        if (visitedBounds.containsKey(t) && f == visitedBounds.get(t).atypeFactory) {
            bound = visitedBounds.get(t);
        } else {
            visitedBounds.put(t, f.type(t));
            bound = visit(t, f);
            visitedBounds.remove(t);
        }
        bounds.add(bound);
    }
    AnnotatedTypeVariable result = (AnnotatedTypeVariable) f.type(node);
    List<? extends AnnotationMirror> annotations = TreeUtils.annotationsFromTree(node);
    result.getLowerBound().addAnnotations(annotations);
    switch(bounds.size()) {
        case 0:
            break;
        case 1:
            result.setUpperBound(bounds.get(0));
            break;
        default:
            AnnotatedIntersectionType upperBound = (AnnotatedIntersectionType) result.getUpperBound();
            List<AnnotatedDeclaredType> superBounds = new ArrayList<>(bounds.size());
            for (AnnotatedTypeMirror b : bounds) {
                superBounds.add((AnnotatedDeclaredType) b);
            }
            upperBound.setDirectSuperTypes(superBounds);
    }
    return result;
}
Also used : AnnotatedIntersectionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) MethodTree(com.sun.source.tree.MethodTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 50 with AnnotatedDeclaredType

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

the class TypeFromTypeTreeVisitor method visitParameterizedType.

@Override
public AnnotatedTypeMirror visitParameterizedType(ParameterizedTypeTree node, AnnotatedTypeFactory f) {
    List<AnnotatedTypeMirror> args = new ArrayList<>(node.getTypeArguments().size());
    for (Tree t : node.getTypeArguments()) {
        args.add(visit(t, f));
    }
    // use creator?
    AnnotatedTypeMirror result = f.type(node);
    AnnotatedTypeMirror atype = visit(node.getType(), f);
    result.addAnnotations(atype.getAnnotations());
    if (result instanceof AnnotatedDeclaredType) {
        assert result instanceof AnnotatedDeclaredType : node + " --> " + result;
        ((AnnotatedDeclaredType) result).setTypeArguments(args);
    }
    return result;
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayList(java.util.ArrayList) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) MethodTree(com.sun.source.tree.MethodTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree)

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