Search in sources :

Example 1 with Builder

use of com.google.errorprone.matchers.Description.Builder 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)

Example 2 with Builder

use of com.google.errorprone.matchers.Description.Builder in project error-prone by google.

the class HidingField method checkForHiddenFields.

private void checkForHiddenFields(List<VariableTree> originalClassMembers, Map<Name, VarSymbol> parentMembers, Name parentClassName, ClassTree classTree, VisitorState visitorState) {
    Iterator<VariableTree> origVariableIterator = originalClassMembers.iterator();
    VariableTree origVariable = null;
    while (origVariableIterator.hasNext()) {
        origVariable = origVariableIterator.next();
        if (parentMembers.containsKey(origVariable.getName())) {
            if (isPackagePrivateAndInDiffPackage(parentMembers.get(origVariable.getName()), classTree)) {
                continue;
            }
            Builder matchDesc = buildDescription(origVariable);
            matchDesc.setMessage("Hiding fields of superclasses may cause confusion and errors. " + "This field is hiding a field of the same name in superclass: " + parentClassName);
            visitorState.reportMatch(matchDesc.build());
            origVariableIterator.remove();
        }
    }
}
Also used : Builder(com.google.errorprone.matchers.Description.Builder) VariableTree(com.sun.source.tree.VariableTree)

Aggregations

Builder (com.google.errorprone.matchers.Description.Builder)2 ViolationReporter (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.ViolationReporter)1 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)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 VariableTree (com.sun.source.tree.VariableTree)1 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 Type (com.sun.tools.javac.code.Type)1