Search in sources :

Example 1 with FIELD

use of javax.lang.model.element.ElementKind.FIELD in project error-prone by google.

the class UnusedMethod method fixConstructors.

private void fixConstructors(ImmutableListMultimap<Symbol, TreePath> unusedConstructors, VisitorState state) {
    for (Map.Entry<Symbol, List<TreePath>> entry : asMap(unusedConstructors).entrySet()) {
        Symbol symbol = entry.getKey();
        List<TreePath> trees = entry.getValue();
        SuggestedFix.Builder fix = SuggestedFix.builder();
        int constructorCount = size(scope(symbol.members()).getSymbols(Symbol::isConstructor));
        int finalFields = size(scope(symbol.members()).getSymbols(s -> s.getKind().equals(FIELD) && s.getModifiers().contains(FINAL)));
        boolean fixable;
        if (constructorCount == trees.size()) {
            fix.postfixWith(getLast(trees).getLeaf(), format("private %s() {}", symbol.getSimpleName()));
            fixable = finalFields == 0;
        } else {
            fixable = true;
        }
        String message = String.format("Constructor '%s' is never used.", symbol.getSimpleName());
        trees.forEach(t -> fix.merge(replaceIncludingComments(t, "", state)));
        state.reportMatch(buildDescription(trees.get(0).getLeaf()).addFix(fixable ? fix.build() : emptyFix()).setMessage(message).build());
    }
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Modifier(javax.lang.model.element.Modifier) SuggestedFixes.replaceIncludingComments(com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments) MoreAnnotations.asStrings(com.google.errorprone.util.MoreAnnotations.asStrings) ASTHelpers.isGeneratedConstructor(com.google.errorprone.util.ASTHelpers.isGeneratedConstructor) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) ASTHelpers.shouldKeep(com.google.errorprone.util.ASTHelpers.shouldKeep) AssignmentTree(com.sun.source.tree.AssignmentTree) FINAL(javax.lang.model.element.Modifier.FINAL) IdentifierTree(com.sun.source.tree.IdentifierTree) Map(java.util.Map) Supplier(com.google.errorprone.suppliers.Supplier) Suppliers.typeFromString(com.google.errorprone.suppliers.Suppliers.typeFromString) MoreAnnotations.getAnnotationValue(com.google.errorprone.util.MoreAnnotations.getAnnotationValue) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) TreePath(com.sun.source.util.TreePath) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) Iterables.getLast(com.google.common.collect.Iterables.getLast) MemberSelectTree(com.sun.source.tree.MemberSelectTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) String.format(java.lang.String.format) TreeScanner(com.sun.source.util.TreeScanner) List(java.util.List) SuggestedFix.emptyFix(com.google.errorprone.fixes.SuggestedFix.emptyFix) CompilationUnitTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher) Description(com.google.errorprone.matchers.Description) Multimaps.asMap(com.google.common.collect.Multimaps.asMap) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ASTHelpers.scope(com.google.errorprone.util.ASTHelpers.scope) Type(com.sun.tools.javac.code.Type) SERIALIZATION_METHODS(com.google.errorprone.matchers.Matchers.SERIALIZATION_METHODS) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Iterables.size(com.google.common.collect.Iterables.size) VisitorState(com.google.errorprone.VisitorState) Kind(com.sun.source.tree.Tree.Kind) NewClassTree(com.sun.source.tree.NewClassTree) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) TreePathScanner(com.sun.source.util.TreePathScanner) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Name(javax.lang.model.element.Name) Ascii(com.google.common.base.Ascii) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) FIELD(javax.lang.model.element.ElementKind.FIELD) CONSTRUCTOR(javax.lang.model.element.ElementKind.CONSTRUCTOR) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ASTHelpers.canBeRemoved(com.google.errorprone.util.ASTHelpers.canBeRemoved) ASTHelpers.isSubtype(com.google.errorprone.util.ASTHelpers.isSubtype) TreePath(com.sun.source.util.TreePath) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) Suppliers.typeFromString(com.google.errorprone.suppliers.Suppliers.typeFromString) Map(java.util.Map) Multimaps.asMap(com.google.common.collect.Multimaps.asMap) HashMap(java.util.HashMap)

Aggregations

Ascii (com.google.common.base.Ascii)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableListMultimap.toImmutableListMultimap (com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Iterables.getLast (com.google.common.collect.Iterables.getLast)1 Iterables.size (com.google.common.collect.Iterables.size)1 Multimaps.asMap (com.google.common.collect.Multimaps.asMap)1 BugPattern (com.google.errorprone.BugPattern)1 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)1 VisitorState (com.google.errorprone.VisitorState)1 CompilationUnitTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher)1 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 SuggestedFix.emptyFix (com.google.errorprone.fixes.SuggestedFix.emptyFix)1 SuggestedFixes.replaceIncludingComments (com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments)1 Description (com.google.errorprone.matchers.Description)1 SERIALIZATION_METHODS (com.google.errorprone.matchers.Matchers.SERIALIZATION_METHODS)1 Supplier (com.google.errorprone.suppliers.Supplier)1 Suppliers.typeFromString (com.google.errorprone.suppliers.Suppliers.typeFromString)1