Search in sources :

Example 1 with WildcardTree

use of com.sun.source.tree.WildcardTree in project checker-framework by typetools.

the class TypeFromTypeTreeVisitor method updateWildcardBounds.

/**
 * Work around a bug in javac 9 where sometimes the bound field is set to the transitive
 * supertype's type parameter instead of the type parameter which the wildcard directly
 * instantiates. See https://github.com/eisop/checker-framework/issues/18
 *
 * <p>Sets each wildcard type argument's bound from typeArgs to the corresponding type parameter
 * from typeParams.
 *
 * <p>If typeArgs.size() == 0 the method does nothing and returns. Otherwise, typeArgs.size() has
 * to be equal to typeParams.size().
 *
 * <p>For each wildcard type argument and corresponding type parameter, sets the
 * WildcardType.bound field to the corresponding type parameter, if and only if the owners of the
 * existing bound and the type parameter are different.
 *
 * <p>In scenarios where the bound's owner is the same, we don't want to replace a
 * capture-converted bound in the wildcard type with a non-capture-converted bound given by the
 * type parameter declaration.
 *
 * @param typeArgs the type of the arguments at (e.g., at the call side)
 * @param typeParams the type of the formal parameters (e.g., at the method declaration)
 */
// workaround for javac bug
@SuppressWarnings("interning:not.interned")
private void updateWildcardBounds(List<? extends Tree> typeArgs, List<TypeVariableSymbol> typeParams) {
    if (typeArgs.isEmpty()) {
        // Nothing to do for empty type arguments.
        return;
    }
    assert typeArgs.size() == typeParams.size();
    Iterator<? extends Tree> typeArgsItr = typeArgs.iterator();
    Iterator<TypeVariableSymbol> typeParamsItr = typeParams.iterator();
    while (typeArgsItr.hasNext()) {
        Tree typeArg = typeArgsItr.next();
        TypeVariableSymbol typeParam = typeParamsItr.next();
        if (typeArg instanceof WildcardTree) {
            TypeVar typeVar = (TypeVar) typeParam.asType();
            WildcardType wcType = (WildcardType) ((JCWildcard) typeArg).type;
            if (wcType.bound != null && wcType.bound.tsym != null && typeVar.tsym != null && wcType.bound.tsym.owner != typeVar.tsym.owner) {
                wcType.withTypeVar(typeVar);
            }
        }
    }
}
Also used : TypeVar(com.sun.tools.javac.code.Type.TypeVar) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) 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) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) WildcardTree(com.sun.source.tree.WildcardTree)

Aggregations

AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)1 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)1 ClassTree (com.sun.source.tree.ClassTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1 IntersectionTypeTree (com.sun.source.tree.IntersectionTypeTree)1 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 MethodTree (com.sun.source.tree.MethodTree)1 ParameterizedTypeTree (com.sun.source.tree.ParameterizedTypeTree)1 PrimitiveTypeTree (com.sun.source.tree.PrimitiveTypeTree)1 Tree (com.sun.source.tree.Tree)1 TypeParameterTree (com.sun.source.tree.TypeParameterTree)1 UnionTypeTree (com.sun.source.tree.UnionTypeTree)1 WildcardTree (com.sun.source.tree.WildcardTree)1 TypeVariableSymbol (com.sun.tools.javac.code.Symbol.TypeVariableSymbol)1 TypeVar (com.sun.tools.javac.code.Type.TypeVar)1 WildcardType (com.sun.tools.javac.code.Type.WildcardType)1 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)1