Skip to content

Commit

Permalink
UnusedMethod: Add support for excluding annotations from analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie authored and Stephan202 committed Aug 17, 2022
1 parent 973313f commit 70431bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
Expand Down Expand Up @@ -147,6 +148,16 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
/** The set of types exempting a type that is extending or implementing them. */
private static final ImmutableSet<String> EXEMPTING_SUPER_TYPES = ImmutableSet.of();

private final ImmutableSet<String> exemptMethodAnnotations;

public UnusedMethod(ErrorProneFlags errorProneFlags) {
ImmutableSet.Builder<String> exemptMethodAnnotations = ImmutableSet.builder();
errorProneFlags
.getList("UnusedMethod:ExemptMethodAnnotations")
.ifPresent(exemptMethodAnnotations::addAll);
this.exemptMethodAnnotations = exemptMethodAnnotations.build();
}

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
// Map of symbols to method declarations. Initially this is a map of all of the methods. As we
Expand Down Expand Up @@ -433,14 +444,16 @@ public Void visitMethod(MethodTree tree, Void unused) {
* Looks at the list of {@code annotations} and see if there is any annotation which exists {@code
* exemptingAnnotations}.
*/
private static boolean exemptedByAnnotation(List<? extends AnnotationTree> annotations) {
private boolean exemptedByAnnotation(List<? extends AnnotationTree> annotations) {
for (AnnotationTree annotation : annotations) {
Type annotationType = getType(annotation);
if (annotationType == null) {
continue;
}
TypeSymbol tsym = annotationType.tsym;
if (EXEMPTING_METHOD_ANNOTATIONS.contains(tsym.getQualifiedName().toString())) {
String annotationName = tsym.getQualifiedName().toString();
if (EXEMPTING_METHOD_ANNOTATIONS.contains(annotationName)
|| exemptMethodAnnotations.contains(annotationName)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.errorprone.bugpatterns;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
Expand Down Expand Up @@ -138,6 +139,21 @@ public void exemptedByName() {
.doTest();
}

@Test
public void exemptedByPassingViaArgs() {
helper
.addSourceLines("Foo.java", "package example;", "@interface Foo {}")
.addSourceLines(
"ExemptedViaArgs.java",
"package example;",
"class ExemptedViaArgs {",
" @Foo",
" private void bar() {}",
"}")
.setArgs(ImmutableList.of("-XepOpt:UnusedMethod:ExemptMethodAnnotations=example.Foo"))
.doTest();
}

@Test
public void suppressions() {
helper
Expand Down

0 comments on commit 70431bb

Please sign in to comment.