Skip to content

Commit

Permalink
SONARJAVA-4186 Fix S2134 to handle correctly unknown types
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops authored and quentin-jaquier-sonarsource committed Mar 4, 2022
1 parent 258bd1d commit 01352b0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package checks;

class ThreadOverridesRunCheck {
void foo(Runnable r) {
Thread t1 = new Thread(r) { // Compliant
void doSomething() { /* do nothing */ }
};

Thread t2 = new Thread(unknown) {
void doSomething() { /* do nothing */ }
};

Thread t3 = new Thread() { }; // Noncompliant
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ private static boolean hasConstructorCallingSuperWithRunnable(ClassTree classTre
}

private static boolean hasRunnableArgument(Arguments args) {
return args.stream().anyMatch(ThreadOverridesRunCheck::isRunnable);
return args.stream().map(ExpressionTree::symbolType).anyMatch(ThreadOverridesRunCheck::isRunnable);
}

private static boolean isRunnable(ExpressionTree arg) {
return arg.symbolType().isSubtypeOf("java.lang.Runnable");
private static boolean isRunnable(Type argType) {
return argType.isUnknown() || argType.isSubtypeOf("java.lang.Runnable");
}

private static boolean hasCallToSuperWithRunnable(MethodTree constructor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sonar.java.checks.verifier.CheckVerifier;

import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath;

class ThreadOverridesRunCheckTest {

Expand All @@ -33,4 +34,12 @@ void detected() {
.withCheck(new ThreadOverridesRunCheck())
.verifyIssues();
}

@Test
void non_compiling() {
CheckVerifier.newVerifier()
.onFile(nonCompilingTestSourcesPath("checks/ThreadOverridesRunCheck.java"))
.withCheck(new ThreadOverridesRunCheck())
.verifyIssues();
}
}

0 comments on commit 01352b0

Please sign in to comment.