|
| 1 | +package tech.picnic.errorprone.bugpatterns.util; |
| 2 | + |
| 3 | +import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; |
| 4 | + |
| 5 | +import com.google.errorprone.BugPattern; |
| 6 | +import com.google.errorprone.CompilationTestHelper; |
| 7 | +import com.google.errorprone.VisitorState; |
| 8 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 9 | +import com.google.errorprone.matchers.Description; |
| 10 | +import com.sun.source.tree.MethodTree; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +final class ConflictDetectionTest { |
| 14 | + @Test |
| 15 | + void matcher() { |
| 16 | + CompilationTestHelper.newInstance(RenameBlockerFlagger.class, getClass()) |
| 17 | + .addSourceLines( |
| 18 | + "/A.java", |
| 19 | + "import static staticimport.B.foo3t;", |
| 20 | + "", |
| 21 | + "class A {", |
| 22 | + " private void foo1() {", |
| 23 | + " foo3t();", |
| 24 | + " }", |
| 25 | + "", |
| 26 | + " // BUG: Diagnostic contains: [RenameBlockerFlagger] a method named `foo2t` already exists in this", |
| 27 | + " // class", |
| 28 | + " private void foo2() {}", |
| 29 | + "", |
| 30 | + " private void foo2t() {}", |
| 31 | + "", |
| 32 | + " // BUG: Diagnostic contains: [RenameBlockerFlagger] `foo3t` is already statically imported", |
| 33 | + " private void foo3() {}", |
| 34 | + "", |
| 35 | + " // BUG: Diagnostic contains: [RenameBlockerFlagger] `int` is a reserved keyword", |
| 36 | + " private void in() {}", |
| 37 | + "}") |
| 38 | + .addSourceLines( |
| 39 | + "/staticimport/B.java", |
| 40 | + "package staticimport;", |
| 41 | + "", |
| 42 | + "public class B {", |
| 43 | + " public static void foo3t() {}", |
| 44 | + "}") |
| 45 | + .doTest(); |
| 46 | + } |
| 47 | + |
| 48 | + @BugPattern(summary = "Flags blockers for renaming methods", severity = ERROR) |
| 49 | + public static final class RenameBlockerFlagger extends BugChecker |
| 50 | + implements BugChecker.MethodTreeMatcher { |
| 51 | + private static final long serialVersionUID = 1L; |
| 52 | + |
| 53 | + @Override |
| 54 | + public Description matchMethod(MethodTree tree, VisitorState state) { |
| 55 | + return ConflictDetection.findMethodRenameBlocker(tree.getName() + "t", state) |
| 56 | + .map(blocker -> buildDescription(tree).setMessage(blocker).build()) |
| 57 | + .orElse(Description.NO_MATCH); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments