Skip to content

Commit 5b4f8f3

Browse files
SONARJAVA-4543 S5778 does not report on final Enum methods (#5131)
1 parent ea22ed1 commit 5b4f8f3

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S5778",
33
"hasTruePositives": false,
4-
"falseNegatives": 31,
4+
"falseNegatives": 32,
55
"falsePositives": 0
6-
}
6+
}

java-checks-test-sources/default/src/test/java/checks/tests/OneExpectedRuntimeExceptionCheckSample.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@
1313

1414
public class OneExpectedRuntimeExceptionCheckSample {
1515

16+
private Object fun(Object x) {
17+
return x;
18+
}
19+
20+
enum ENUM {
21+
E1, E2
22+
}
23+
24+
@Test
25+
void testEnumStaticFinalMethods() {
26+
var o = fun(ENUM.E1);
27+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.name())); //Compliant, java.lang.Enum#name is final and cannot throw exceptions
28+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.ordinal()));
29+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.hashCode()));
30+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.compareTo(ENUM.E2)));
31+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.getDeclaringClass()));
32+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.describeConstable()));
33+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.equals(o)));
34+
35+
//Non-final methods
36+
assertThrows(IllegalStateException.class, () -> fun(ENUM.E1.toString())); // Noncompliant
37+
}
38+
1639
private final Class<IllegalStateException> myException = IllegalStateException.class;
1740
private final Executable exec = () -> foo(foo(1));
1841

java-checks/src/main/java/org/sonar/java/checks/tests/OneExpectedRuntimeExceptionCheck.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ public class OneExpectedRuntimeExceptionCheck extends AbstractOneExpectedExcepti
3333
.names("mock")
3434
.addParametersMatcher("java.lang.Class").addParametersMatcher("java.lang.Class", "java.lang.String")
3535
.build();
36-
private static final MethodMatchers AUTHORIZED_METHODS = MethodMatchers.or(FAIL_METHOD_MATCHER, MOCKITO_MOCK_METHOD_MATCHERS);
36+
private static final MethodMatchers ENUM_FINAL_METHODS = MethodMatchers.create()
37+
.ofSubTypes("java.lang.Enum")
38+
.names("name", "ordinal", "equals", "hashCode", "compareTo", "getDeclaringClass", "describeConstable")
39+
.withAnyParameters()
40+
.build();
41+
private static final MethodMatchers AUTHORIZED_METHODS = MethodMatchers.or(
42+
FAIL_METHOD_MATCHER,
43+
MOCKITO_MOCK_METHOD_MATCHERS,
44+
ENUM_FINAL_METHODS
45+
);
3746

3847
@Override
3948
void reportMultipleCallInTree(List<Type> expectedExceptions, Tree treeToVisit, Tree reportLocation, String placeToRefactor) {

0 commit comments

Comments
 (0)