Search in sources :

Example 11 with Violation

use of com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation 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

Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)11 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)6 ClassTree (com.sun.source.tree.ClassTree)5 Tree (com.sun.source.tree.Tree)4 Type (com.sun.tools.javac.code.Type)4 ClassType (com.sun.tools.javac.code.Type.ClassType)4 TypeVariableSymbol (com.sun.tools.javac.code.Symbol.TypeVariableSymbol)3 Immutable (com.google.errorprone.annotations.Immutable)2 AnnotationTree (com.sun.source.tree.AnnotationTree)2 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 NewClassTree (com.sun.source.tree.NewClassTree)2 TypeParameterTree (com.sun.source.tree.TypeParameterTree)2 Symbol (com.sun.tools.javac.code.Symbol)2 HashMap (java.util.HashMap)2 AutoValue (com.google.auto.value.AutoValue)1 Joiner (com.google.common.base.Joiner)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1