-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43262c5
commit 0709a49
Showing
5 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...in/java/tech/picnic/errorprone/refaster/matchers/IsMethodInvocationWithTwoOrMoreArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.matchers.Matchers.anyMethod; | ||
import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
|
||
/** A matcher for method invocations with two or more arguments. */ | ||
public final class IsMethodInvocationWithTwoOrMoreArgs implements Matcher<ExpressionTree> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public boolean matches(ExpressionTree expressionTree, VisitorState state) { | ||
if (expressionTree.getKind() == METHOD_INVOCATION) { | ||
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree; | ||
return anyMethod().matches(methodInvocationTree.getMethodSelect(), state) | ||
&& methodInvocationTree.getArguments().size() > 1; | ||
} | ||
return false; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...ava/tech/picnic/errorprone/refaster/matchers/IsMethodInvocationWithTwoOrMoreArgsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class IsMethodInvocationWithTwoOrMoreArgsTest { | ||
@Test | ||
void matches() { | ||
CompilationTestHelper.newInstance( | ||
IsMethodInvocationWithTwoOrMoreArgsTest.MatcherTestChecker.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"class A {", | ||
" String foo(String foo, String bar) {", | ||
" return foo + bar;", | ||
" }", | ||
"", | ||
" String foo(String foo) {", | ||
" return foo;", | ||
" }", | ||
"", | ||
" String foo() {", | ||
" return \"foo\";", | ||
" }", | ||
"", | ||
" Boolean negative1() {", | ||
" return Boolean.TRUE;", | ||
" }", | ||
"", | ||
" String negative2() {", | ||
" return \"foo\";", | ||
" }", | ||
"", | ||
" String negative3() {", | ||
" return foo(\"foo\");", | ||
" }", | ||
"", | ||
" String negative4() {", | ||
" return foo();", | ||
" }", | ||
"", | ||
" String positive1() {", | ||
" // BUG: Diagnostic contains:", | ||
" return foo(\"foo\", \"bar\");", | ||
" }", | ||
"", | ||
" String positive2() {", | ||
" // BUG: Diagnostic contains:", | ||
" return String.format(\"%s\", \"foo\");", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
/** A {@link BugChecker} that simply delegates to {@link IsCharacter}. */ | ||
@BugPattern( | ||
summary = "Flags expressions matched by `IsMethodInvocationWithTwoOrMoreArgs`", | ||
severity = ERROR) | ||
public static final class MatcherTestChecker extends AbstractMatcherTestChecker { | ||
private static final long serialVersionUID = 1L; | ||
|
||
// XXX: This is a false positive reported by Checkstyle. See | ||
// https://github.com/checkstyle/checkstyle/issues/10161#issuecomment-1242732120. | ||
@SuppressWarnings("RedundantModifier") | ||
public MatcherTestChecker() { | ||
super(new IsMethodInvocationWithTwoOrMoreArgs()); | ||
} | ||
} | ||
} |