Search in sources :

Example 11 with TypeVariableSymbol

use of com.sun.tools.javac.code.Symbol.TypeVariableSymbol in project error-prone by google.

the class Inliner method inlineAsVar.

public TypeVar inlineAsVar(UTypeVar var) throws CouldNotResolveImportException {
    /*
     * In order to handle recursively bounded type variables without a stack overflow,
     * we first cache a type var with no bounds, then we inline the bounds.
     */
    TypeVar typeVar = typeVarCache.get(var.getName());
    if (typeVar != null) {
        return typeVar;
    }
    Name name = asName(var.getName());
    TypeSymbol sym = new TypeVariableSymbol(0, name, null, symtab().noSymbol);
    typeVar = new TypeVar(sym, null, null);
    sym.type = typeVar;
    typeVarCache.put(var.getName(), typeVar);
    // Any recursive uses of var will point to the same TypeVar object generated above.
    typeVar.bound = var.getUpperBound().inline(this);
    typeVar.lower = var.getLowerBound().inline(this);
    return typeVar;
}
Also used : TypeVar(com.sun.tools.javac.code.Type.TypeVar) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) Name(com.sun.tools.javac.util.Name)

Example 12 with TypeVariableSymbol

use of com.sun.tools.javac.code.Symbol.TypeVariableSymbol in project error-prone by google.

the class TypeParameterShadowing method findDuplicatesOf.

private Description findDuplicatesOf(Tree tree, List<? extends TypeParameterTree> typeParameters, VisitorState state) {
    Symbol symbol = ASTHelpers.getSymbol(tree);
    if (symbol == null) {
        return Description.NO_MATCH;
    }
    List<TypeVariableSymbol> enclosingTypeSymbols = typeVariablesEnclosing(symbol);
    if (enclosingTypeSymbols.isEmpty()) {
        return Description.NO_MATCH;
    }
    List<TypeVariableSymbol> conflictingTypeSymbols = new ArrayList<>();
    typeParameters.forEach(param -> enclosingTypeSymbols.stream().filter(tvs -> tvs.name.contentEquals(param.getName())).findFirst().ifPresent(conflictingTypeSymbols::add));
    if (conflictingTypeSymbols.isEmpty()) {
        return Description.NO_MATCH;
    }
    Description.Builder descriptionBuilder = buildDescription(tree);
    String message = "Found aliased type parameters: " + conflictingTypeSymbols.stream().map(tvs -> tvs.name + " declared in " + tvs.owner.getSimpleName()).collect(Collectors.joining("\n"));
    descriptionBuilder.setMessage(message);
    Set<String> typeVarsInScope = Streams.concat(enclosingTypeSymbols.stream(), symbol.getTypeParameters().stream()).map(v -> v.name.toString()).collect(toImmutableSet());
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    conflictingTypeSymbols.stream().map(v -> renameTypeVariable(typeParameterInList(typeParameters, v), tree, replacementTypeVarName(v.name, typeVarsInScope), state)).forEach(fixBuilder::merge);
    descriptionBuilder.addFix(fixBuilder.build());
    return descriptionBuilder.build();
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) TreeScanner(com.sun.tools.javac.tree.TreeScanner) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) Matcher(java.util.regex.Matcher) StandardTags(com.google.errorprone.BugPattern.StandardTags) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Objects(com.google.common.base.Objects) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Name(javax.lang.model.element.Name) MoreCollectors(com.google.common.collect.MoreCollectors) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) Streams(com.google.common.collect.Streams) JCTree(com.sun.tools.javac.tree.JCTree) Collectors(java.util.stream.Collectors) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) List(java.util.List) Description(com.google.errorprone.matchers.Description) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Pattern(java.util.regex.Pattern) ASTHelpers(com.google.errorprone.util.ASTHelpers) Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) ArrayList(java.util.ArrayList) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol)

Example 13 with TypeVariableSymbol

use of com.sun.tools.javac.code.Symbol.TypeVariableSymbol in project error-prone by google.

the class ImmutableChecker method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    ImmutableAnalysis analysis = new ImmutableAnalysis(this, state, wellKnownMutability);
    if (tree.getSimpleName().length() == 0) {
        // TODO(cushon): once Java 8 happens, require @Immutable on anonymous classes
        return handleAnonymousClass(tree, state, analysis);
    }
    AnnotationInfo annotation = analysis.getImmutableAnnotation(tree, state);
    if (annotation == null) {
        // report an error if it extends/implements any @Immutable-annotated types.
        return checkSubtype(tree, state);
    }
    // of the annotation are "trusted".
    if (wellKnownMutability.getKnownImmutableClasses().containsValue(annotation)) {
        return NO_MATCH;
    }
    // Check that the types in containerOf actually exist
    Map<String, TypeVariableSymbol> typarams = new HashMap<>();
    for (TypeParameterTree typaram : tree.getTypeParameters()) {
        typarams.put(typaram.getName().toString(), (TypeVariableSymbol) ASTHelpers.getSymbol(typaram));
    }
    SetView<String> difference = Sets.difference(annotation.containerOf(), typarams.keySet());
    if (!difference.isEmpty()) {
        return buildDescription(tree).setMessage(String.format("could not find type(s) referenced by containerOf: %s", Joiner.on("', '").join(difference))).build();
    }
    ImmutableSet<String> immutableAndContainer = typarams.entrySet().stream().filter(e -> annotation.containerOf().contains(e.getKey()) && analysis.isImmutableTypeParameter(e.getValue())).map(Entry::getKey).collect(toImmutableSet());
    if (!immutableAndContainer.isEmpty()) {
        return buildDescription(tree).setMessage(String.format("using both @ImmutableTypeParameter and containerOf is redundant: %s", Joiner.on("', '").join(immutableAndContainer))).build();
    }
    // Main path for @Immutable-annotated types:
    // 
    // Check that the fields (including inherited fields) are immutable, and
    // validate the type hierarchy superclass.
    ClassSymbol sym = ASTHelpers.getSymbol(tree);
    Violation info = analysis.checkForImmutability(Optional.of(tree), immutableTypeParametersInScope(ASTHelpers.getSymbol(tree), state, analysis), ASTHelpers.getType(tree), (Tree matched, Violation violation) -> describeClass(matched, sym, annotation, violation));
    if (!info.isPresent()) {
        return NO_MATCH;
    }
    return describeClass(tree, sym, annotation, info).build();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation) TypeParameterTree(com.sun.source.tree.TypeParameterTree) HashMap(java.util.HashMap) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeParameterTree(com.sun.source.tree.TypeParameterTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol)

Aggregations

TypeVariableSymbol (com.sun.tools.javac.code.Symbol.TypeVariableSymbol)13 Symbol (com.sun.tools.javac.code.Symbol)7 ClassTree (com.sun.source.tree.ClassTree)6 ArrayList (java.util.ArrayList)6 Streams (com.google.common.collect.Streams)5 VisitorState (com.google.errorprone.VisitorState)5 ASTHelpers (com.google.errorprone.util.ASTHelpers)5 MethodTree (com.sun.source.tree.MethodTree)5 Tree (com.sun.source.tree.Tree)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 Type (com.sun.tools.javac.code.Type)5 List (java.util.List)5 Set (java.util.Set)5 TypeParameterTree (com.sun.source.tree.TypeParameterTree)4 TypeVar (com.sun.tools.javac.code.Type.TypeVar)4 WildcardType (com.sun.tools.javac.code.Type.WildcardType)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 BugPattern (com.google.errorprone.BugPattern)3 JDK (com.google.errorprone.BugPattern.Category.JDK)3 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)3