Search in sources :

Example 1 with TypeParameterTree

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

the class TypeParameterShadowing method renameTypeVariable.

private static SuggestedFix renameTypeVariable(Tree sourceTree, List<? extends TypeParameterTree> typeParameters, Name typeVariable, String typeVarReplacement, VisitorState state) {
    TypeParameterTree matchingTypeParam = typeParameters.stream().filter(t -> t.getName().contentEquals(typeVariable)).collect(MoreCollectors.onlyElement());
    Symbol typeVariableSymbol = ASTHelpers.getSymbol(matchingTypeParam);
    // replace only the type parameter name (and not any upper bounds)
    String name = matchingTypeParam.getName().toString();
    int pos = ((JCTree) matchingTypeParam).getStartPosition();
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
    ((JCTree) sourceTree).accept(new TreeScanner() {

        @Override
        public void visitIdent(JCTree.JCIdent tree) {
            Symbol identSym = ASTHelpers.getSymbol(tree);
            if (Objects.equal(identSym, typeVariableSymbol)) {
                // }
                if (Objects.equal(state.getSourceForNode(tree), name)) {
                    fixBuilder.replace(tree, typeVarReplacement);
                }
            }
        }
    });
    return fixBuilder.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) 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) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Pattern(java.util.regex.Pattern) ASTHelpers(com.google.errorprone.util.ASTHelpers) TypeParameterTree(com.sun.source.tree.TypeParameterTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreeScanner(com.sun.tools.javac.tree.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 2 with TypeParameterTree

use of com.sun.source.tree.TypeParameterTree 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, "@Immutable classes cannot have non-final fields", "@Immutable class has mutable field");
    if (tree.getSimpleName().length() == 0) {
        // TODO(cushon): once Java 8 happens, require @Immutable on anonymous classes
        return handleAnonymousClass(tree, state, analysis);
    }
    ImmutableAnnotationInfo annotation = getImmutableAnnotation(tree);
    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.KNOWN_IMMUTABLE.containsValue(annotation)) {
        return Description.NO_MATCH;
    }
    // Check that the types in containerOf actually exist
    Set<String> typarams = new HashSet<>();
    for (TypeParameterTree typaram : tree.getTypeParameters()) {
        typarams.add(typaram.getName().toString());
    }
    SetView<String> difference = Sets.difference(annotation.containerOf(), typarams);
    if (!difference.isEmpty()) {
        String message = String.format("could not find type(s) referenced by containerOf: %s", Joiner.on("', '").join(difference));
        return buildDescription(tree).setMessage(message).build();
    }
    // Main path for @Immutable-annotated types:
    //
    // Check that the fields (including inherited fields) are immutable, and
    // validate the type hierarchy superclass.
    Violation info = analysis.checkForImmutability(Optional.of(tree), immutableTypeParametersInScope(ASTHelpers.getSymbol(tree)), ASTHelpers.getType(tree));
    if (!info.isPresent()) {
        return Description.NO_MATCH;
    }
    String message = "type annotated with @Immutable could not be proven immutable: " + info.message();
    return buildDescription(tree).setMessage(message).build();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation) TypeParameterTree(com.sun.source.tree.TypeParameterTree) HashSet(java.util.HashSet)

Example 3 with TypeParameterTree

use of com.sun.source.tree.TypeParameterTree 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(tree, typeParameters, v.name, 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) 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) 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)

Aggregations

TypeParameterTree (com.sun.source.tree.TypeParameterTree)3 Objects (com.google.common.base.Objects)2 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 MoreCollectors (com.google.common.collect.MoreCollectors)2 Streams (com.google.common.collect.Streams)2 BugPattern (com.google.errorprone.BugPattern)2 JDK (com.google.errorprone.BugPattern.Category.JDK)2 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)2 VisitorState (com.google.errorprone.VisitorState)2 ClassTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher)2 MethodTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher)2 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)2 Description (com.google.errorprone.matchers.Description)2 ASTHelpers (com.google.errorprone.util.ASTHelpers)2 ClassTree (com.sun.source.tree.ClassTree)2 MethodTree (com.sun.source.tree.MethodTree)2 Tree (com.sun.source.tree.Tree)2 Symbol (com.sun.tools.javac.code.Symbol)2 TypeVariableSymbol (com.sun.tools.javac.code.Symbol.TypeVariableSymbol)2 JCTree (com.sun.tools.javac.tree.JCTree)2