Search in sources :

Example 6 with ParameterizedTypeTree

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

the class TypeFromTypeTreeVisitor method visitParameterizedType.

@Override
public AnnotatedTypeMirror visitParameterizedType(ParameterizedTypeTree node, AnnotatedTypeFactory f) {
    ClassSymbol baseType = (ClassSymbol) TreeUtils.elementFromTree(node.getType());
    updateWildcardBounds(node.getTypeArguments(), baseType.getTypeParameters());
    List<AnnotatedTypeMirror> args = CollectionsPlume.mapList((Tree t) -> visit(t, f), node.getTypeArguments());
    // use creator?
    AnnotatedTypeMirror result = f.type(node);
    AnnotatedTypeMirror atype = visit(node.getType(), f);
    result.addAnnotations(atype.getAnnotations());
    // diamond which should be inferred.
    if (result instanceof AnnotatedDeclaredType && !args.isEmpty()) {
        assert result instanceof AnnotatedDeclaredType : node + " --> " + result;
        ((AnnotatedDeclaredType) result).setTypeArguments(args);
    }
    return result;
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) 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)

Example 7 with ParameterizedTypeTree

use of com.sun.source.tree.ParameterizedTypeTree in project error-prone by google.

the class JdkObsolete method linkedListFix.

// rewrite e.g. `List<Object> xs = new LinkedList<>()` -> `... = new ArrayList<>()`
private static Optional<Fix> linkedListFix(Tree tree, VisitorState state) {
    Type type = getTargetType(state);
    if (type == null) {
        return Optional.empty();
    }
    Types types = state.getTypes();
    for (String replacement : ImmutableList.of("java.util.ArrayList", "java.util.ArrayDeque")) {
        Symbol sym = state.getSymbolFromString(replacement);
        if (sym == null) {
            continue;
        }
        if (types.isAssignable(types.erasure(sym.asType()), types.erasure(type))) {
            SuggestedFix.Builder fix = SuggestedFix.builder();
            while (tree instanceof ParameterizedTypeTree) {
                tree = ((ParameterizedTypeTree) tree).getType();
            }
            fix.replace(tree, SuggestedFixes.qualifyType(state, fix, sym));
            return Optional.of(fix.build());
        }
    }
    return Optional.empty();
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Aggregations

ParameterizedTypeTree (com.sun.source.tree.ParameterizedTypeTree)7 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)4 ClassTree (com.sun.source.tree.ClassTree)4 ExpressionTree (com.sun.source.tree.ExpressionTree)4 IdentifierTree (com.sun.source.tree.IdentifierTree)4 MethodTree (com.sun.source.tree.MethodTree)4 Tree (com.sun.source.tree.Tree)4 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)3 MemberSelectTree (com.sun.source.tree.MemberSelectTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 VariableTree (com.sun.source.tree.VariableTree)3 AnnotationTree (com.sun.source.tree.AnnotationTree)2 CatchTree (com.sun.source.tree.CatchTree)2 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)2 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)2 InstanceOfTree (com.sun.source.tree.InstanceOfTree)2 IntersectionTypeTree (com.sun.source.tree.IntersectionTypeTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 NewArrayTree (com.sun.source.tree.NewArrayTree)2