Search in sources :

Example 1 with ViolationReporter

use of com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.ViolationReporter in project error-prone by google.

the class ImmutableChecker method handleAnonymousClass.

// Anonymous classes
/**
 * Check anonymous implementations of {@code @Immutable} types.
 */
private Description handleAnonymousClass(ClassTree tree, VisitorState state, ImmutableAnalysis analysis) {
    ClassSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null) {
        return NO_MATCH;
    }
    Type superType = immutableSupertype(sym, state);
    if (superType == null) {
        return NO_MATCH;
    }
    // We don't need to check that the superclass has an immutable instantiation.
    // The anonymous instance can only be referred to using a superclass type, so
    // the type arguments will be validated at any type use site where we care about
    // the instance's immutability.
    // 
    // Also, we have no way to express something like:
    // 
    // public static <@Immutable T> ImmutableBox<T> create(T t) {
    // return new ImmutableBox<>(t);
    // }
    ImmutableSet<String> typarams = immutableTypeParametersInScope(sym, state, analysis);
    Violation info = analysis.areFieldsImmutable(Optional.of(tree), typarams, ASTHelpers.getType(tree), new ViolationReporter() {

        @Override
        public Builder describe(Tree tree, Violation info) {
            return describeAnonymous(tree, superType, info);
        }
    });
    if (!info.isPresent()) {
        return NO_MATCH;
    }
    return describeAnonymous(tree, superType, info).build();
}
Also used : Violation(com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ViolationReporter(com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.ViolationReporter) Builder(com.google.errorprone.matchers.Description.Builder) 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)

Aggregations

ViolationReporter (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.ViolationReporter)1 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)1 Builder (com.google.errorprone.matchers.Description.Builder)1 ClassTree (com.sun.source.tree.ClassTree)1 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 NewClassTree (com.sun.source.tree.NewClassTree)1 Tree (com.sun.source.tree.Tree)1 TypeParameterTree (com.sun.source.tree.TypeParameterTree)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 Type (com.sun.tools.javac.code.Type)1