Skip to content

Commit

Permalink
SONARJAVA-4509 Update S2438's issue message to be more actionable (#4451
Browse files Browse the repository at this point in the history
)

* Update reporting message for rule S2438 to be more actionable
* Move test file from rule S2438 to java-checks-test-sources
  • Loading branch information
erwan-serandour authored Aug 16, 2023
1 parent ecc7fed commit cf5a4e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class A {
package checks;
class ThreadAsRunnableArgumentCheck {

A(int i) {
ThreadAsRunnableArgumentCheck (int i) {
}

public void foo() {
Thread t = new Thread() {
};
new Thread(t).start(); // Noncompliant [[sc=16;ec=17]] {{"t" is a "Thread".}}
new Thread(t).start(); // Noncompliant [[sc=16;ec=17]] {{Replace "t" of type Thread with an instance of Runnable.}}

new Thread(bar()).start(); // Noncompliant [[sc=16;ec=21]]

Expand All @@ -20,14 +21,14 @@ public void run() {
};
new Thread(r).start(); // Compliant

new A(0);
new ThreadAsRunnableArgumentCheck (0);

MyClass m = new MyClass(myThread); // Noncompliant
m.foo(myThread); // Noncompliant
m = new MyClass(0, new MyThread()); // Noncompliant
// Noncompliant@+1
m = new MyClass(0, myThread, r, new MyThread()); // Noncompliant because of arg1 and arg3
m = new MyClass(0, new Thread[] {myThread, new MyThread()}); // Noncompliant {{"Argument 2" is a "Thread[]".}}
m = new MyClass(0, new Thread[] {myThread, new MyThread()}); // Noncompliant {{Replace "argument 2" of type Thread[] with an instance of Runnable[].}}
m = new MyClass(0); // Compliant
m = new MyClass(0, new Runnable[] {}); // Compliant
m = new MyClass(0, null, r, null); // Compliant
Expand All @@ -39,24 +40,29 @@ public Thread bar() {
return new Thread() {
};
}
}
class MyThread extends Thread {
}

class MyThread extends Thread {
}
class MyClass {
MyClass(Runnable r) {
}

class MyClass {
MyClass(Runnable r) {
}
MyClass(int i, String s) {
}

MyClass(int i, String s) {
}
MyClass(int i, Runnable... runners) {
}

MyClass(int i, Runnable... runners) {
}
void bar(Thread thread) {

void foo(Runnable r) {
}
}

void foo(Runnable r) {
}

void qix(Runnable... runners) {
void qix(Runnable... runners) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ private static Type getExpectedType(Type providedType, List<Type> parametersType

private static String getMessage(ExpressionTree argument, Type providedType, int index) {
String array = providedType.isArray() ? "[]" : "";
return MessageFormat.format("\"{0}\" is a \"Thread{1}\".", getArgName(argument, index), array);
return MessageFormat.format("Replace \"{0}\" of type Thread{1} with an instance of Runnable{1}.", getArgName(argument, index), array);
}

private static String getArgName(ExpressionTree tree, int index) {
if (tree.is(Tree.Kind.IDENTIFIER)) {
return ((IdentifierTree) tree).name();
}
return "Argument " + (index + 1);
return "argument " + (index + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

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

class ThreadAsRunnableArgumentCheckTest {

@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/checks/ThreadAsRunnableArgumentCheck.java")
.onFile(mainCodeSourcesPath("checks/ThreadAsRunnableArgumentCheck.java"))
.withCheck(new ThreadAsRunnableArgumentCheck())
.verifyIssues();
}
Expand Down

0 comments on commit cf5a4e2

Please sign in to comment.