Search in sources :

Example 31 with AnnotatedDeclaredType

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

the class AnnotatedTypeCopier method visitUnion.

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

Example 32 with AnnotatedDeclaredType

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

the class AnnotatedTypeFactory method fromNewClassContextHelper.

// This method extracts the ugly hacky parts.
// This method should be rewritten and in particular diamonds should be
// implemented cleanly.
// See Issue 289.
private void fromNewClassContextHelper(AnnotatedDeclaredType type, AnnotatedTypeMirror ctxtype) {
    switch(ctxtype.getKind()) {
        case DECLARED:
            AnnotatedDeclaredType adctx = (AnnotatedDeclaredType) ctxtype;
            if (type.getTypeArguments().size() == adctx.getTypeArguments().size()) {
                // Try to simply take the type arguments from LHS.
                List<AnnotatedTypeMirror> oldArgs = type.getTypeArguments();
                List<AnnotatedTypeMirror> newArgs = adctx.getTypeArguments();
                for (int i = 0; i < type.getTypeArguments().size(); ++i) {
                    if (!types.isSameType(oldArgs.get(i).actualType, newArgs.get(i).actualType)) {
                        // One of the underlying types doesn't match. Give up.
                        return;
                    }
                }
                type.setTypeArguments(newArgs);
            /* It would be nice to call isSubtype for a basic sanity check.
                     * However, the type might not have been completely initialized yet,
                     * so isSubtype might fail.
                     *
                    if (!typeHierarchy.isSubtype(type, ctxtype)) {
                        // Simply taking the newArgs didn't result in a valid subtype.
                        // Give up and simply use the inferred types.
                        type.setTypeArguments(oldArgs);
                    }
                    */
            } else {
            // TODO: Find a way to determine annotated type arguments.
            // Look at what Attr and Resolve are doing and rework this whole method.
            }
            break;
        case ARRAY:
            // so nothing to be done.
            break;
        case TYPEVAR:
            // Uses an ExecutableElement, which did not substitute type variables.
            break;
        case WILDCARD:
            // TODO: look at bounds of wildcard and see whether we can improve.
            break;
        default:
            if (ctxtype.getKind().isPrimitive()) {
            // See Issue 438. Ignore primitive types for diamond inference - a primitive
            // type
            // is never a suitable context anyways.
            } else {
                ErrorReporter.errorAbort("AnnotatedTypeFactory.fromNewClassContextHelper: unexpected context: " + ctxtype + " (" + ctxtype.getKind() + ")");
            }
    }
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 33 with AnnotatedDeclaredType

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

the class AnnotatedTypeFactory method getSelfType.

/**
 * Returns the type of {@code this} in the current location, which can be used if {@code this}
 * has a special semantics (e.g. {@code this} is non-null).
 *
 * <p>The parameter is an arbitrary tree and does not have to mention "this", neither explicitly
 * nor implicitly. This method should be overridden for type-system specific behavior.
 *
 * <p>TODO: in 1.8.2, handle all receiver type annotations. TODO: handle enclosing classes
 * correctly.
 */
public AnnotatedDeclaredType getSelfType(Tree tree) {
    TreePath path = getPath(tree);
    ClassTree enclosingClass = TreeUtils.enclosingClass(path);
    if (enclosingClass == null) {
        // I hope this only happens when tree is a fake tree that
        // we created, e.g. when desugaring enhanced-for-loops.
        enclosingClass = getCurrentClassTree(tree);
    }
    AnnotatedDeclaredType type = getAnnotatedType(enclosingClass);
    MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
    if (enclosingClass.getSimpleName().length() != 0 && enclosingMethod != null) {
        AnnotatedDeclaredType methodReceiver;
        if (TreeUtils.isConstructor(enclosingMethod)) {
            methodReceiver = (AnnotatedDeclaredType) getAnnotatedType(enclosingMethod).getReturnType();
        } else {
            methodReceiver = getAnnotatedType(enclosingMethod).getReceiverType();
        }
        if (shouldTakeFromReceiver(methodReceiver)) {
            // TODO  what about all annotations on the receiver?
            // Code is also duplicated above.
            type.clearAnnotations();
            type.addAnnotations(methodReceiver.getAnnotations());
        }
    }
    return type;
}
Also used : TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree)

Example 34 with AnnotatedDeclaredType

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

the class AnnotatedTypeFactory method getCurrentMethodReceiver.

/**
 * Returns the receiver type of the current method being visited, and returns null if the
 * visited tree is not within a method or if that method has no receiver (e.g. a static method).
 *
 * <p>The method uses the parameter only if the most enclosing method cannot be found directly.
 *
 * @return receiver type of the most enclosing method being visited
 */
@Nullable
protected final AnnotatedDeclaredType getCurrentMethodReceiver(Tree tree) {
    AnnotatedDeclaredType res = visitorState.getMethodReceiver();
    if (res == null) {
        TreePath path = getPath(tree);
        if (path != null) {
            MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
            ClassTree enclosingClass = TreeUtils.enclosingClass(path);
            boolean found = false;
            for (Tree member : enclosingClass.getMembers()) {
                if (member.getKind() == Tree.Kind.METHOD) {
                    if (member == enclosingMethod) {
                        found = true;
                    }
                }
            }
            if (found && enclosingMethod != null) {
                AnnotatedExecutableType method = getAnnotatedType(enclosingMethod);
                res = method.getReceiverType();
            // TODO: three tests fail if one adds the following, which would make
            // sense, or not?
            // visitorState.setMethodReceiver(res);
            } else {
                // We are within an anonymous class or field initializer
                res = this.getAnnotatedType(enclosingClass);
            }
        }
    }
    return res;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ReturnTree(com.sun.source.tree.ReturnTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 35 with AnnotatedDeclaredType

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

the class AnnotatedTypeFactory method getFnInterfaceFromTree.

/**
 * Returns the functional interface and the function type that this lambda or member references
 * targets.
 *
 * <p>The function type is the type of the single method declared in the functional interface
 * adapted as if it were invoked using the functional interface as the receiver expression.
 *
 * <p>The target type of a lambda or a method reference is the type to which it is assigned or
 * casted.
 *
 * @param tree lambda expression tree or member reference tree
 * @return the functional interface and the function type that this method reference or lambda
 *     targets.
 */
private Pair<AnnotatedDeclaredType, AnnotatedExecutableType> getFnInterfaceFromTree(Tree tree) {
    // Functional interface
    AnnotatedDeclaredType functionalInterfaceType = getFunctionalInterfaceType(tree);
    makeGroundTargetType(functionalInterfaceType, (DeclaredType) TreeUtils.typeOf(tree));
    // Functional method
    Element fnElement = TreeUtils.findFunction(tree, processingEnv);
    // Function type
    AnnotatedExecutableType functionType = (AnnotatedExecutableType) AnnotatedTypes.asMemberOf(types, this, functionalInterfaceType, fnElement);
    return Pair.of(functionalInterfaceType, functionType);
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

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