Skip to content

Commit 4f27457

Browse files
author
Angelo Buono
authored
SONARJAVA-4640 - Support javax packages migrated to jakarta (#4621)
Update of SingleCharacterAlternationCheck, UnicodeCaseCheck, UnusedMethodParameterCheck, UnusedPrivateMethodCheck, ServletMethodsExceptionsThrownCheck and TooManyParametersCheck
1 parent ee671c9 commit 4f27457

16 files changed

Lines changed: 175 additions & 26 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1130",
33
"hasTruePositives": true,
4-
"falseNegatives": 30,
4+
"falseNegatives": 32,
55
"falsePositives": 0
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1161",
33
"hasTruePositives": true,
4-
"falseNegatives": 7,
4+
"falseNegatives": 10,
55
"falsePositives": 0
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1172",
33
"hasTruePositives": true,
4-
"falseNegatives": 14,
4+
"falseNegatives": 18,
55
"falsePositives": 0
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1989",
33
"hasTruePositives": false,
4-
"falseNegatives": 6,
4+
"falseNegatives": 12,
55
"falsePositives": 0
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S5866",
33
"hasTruePositives": true,
4-
"falseNegatives": 1,
4+
"falseNegatives": 2,
55
"falsePositives": 0
66
}

java-checks-test-sources/default/pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

77
<parent>
@@ -423,6 +423,12 @@
423423
<version>6.0.0</version>
424424
<scope>provided</scope>
425425
</dependency>
426+
<dependency>
427+
<groupId>jakarta.enterprise</groupId>
428+
<artifactId>jakarta.enterprise.cdi-api</artifactId>
429+
<version>4.0.1</version>
430+
<scope>provided</scope>
431+
</dependency>
426432
<dependency>
427433
<groupId>com.google.guava</groupId>
428434
<artifactId>guava</artifactId>

java-checks-test-sources/default/src/main/java/checks/ServletMethodsExceptionsThrownCheck.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ServletMethodsExceptionsThrownCheck extends HttpServlet {
1414

1515
private static boolean staticMethod() { return true; }
1616

17-
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
17+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
1818
String ip = request.getRemoteAddr();
1919
InetAddress addr = InetAddress.getByName(ip); // Noncompliant [[sc=36;ec=45]] {{Handle the following exception that could be thrown by "getByName": UnknownHostException.}}
2020
try {
@@ -32,7 +32,7 @@ public void foo(HttpServletRequest request, HttpServletResponse response) throws
3232
InetAddress addr = InetAddress.getByName(ip);
3333
}
3434

35-
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
35+
public void doPost(HttpServletRequest request, HttpServletResponse response) {
3636
throw new IllegalStateException("bla"); // Noncompliant {{Handle the "IllegalStateException" thrown here in a "try/catch" block.}}
3737
}
3838
public void bar(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
@@ -47,3 +47,44 @@ protected void doPut(HttpServletRequest request, HttpServletResponse response) t
4747
}
4848
}
4949
}
50+
51+
52+
class JakartaServletMethodsExceptionsThrownCheck extends jakarta.servlet.http.HttpServlet {
53+
54+
private static boolean var = staticMethod();
55+
56+
private static boolean staticMethod() { return true; }
57+
58+
public void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException {
59+
String ip = request.getRemoteAddr();
60+
InetAddress addr = InetAddress.getByName(ip); // Noncompliant [[sc=36;ec=45]] {{Handle the following exception that could be thrown by "getByName": UnknownHostException.}}
61+
try {
62+
addr = InetAddress.getByName(ip);
63+
} catch (IllegalArgumentException e) {
64+
throw e; // Noncompliant [[sc=7;ec=15]] {{Handle the "IllegalArgumentException" thrown here in a "try/catch" block.}}
65+
} catch (Exception e) {
66+
throw e; // Noncompliant {{Handle the "Exception" thrown here in a "try/catch" block.}}
67+
}
68+
staticMethod();
69+
}
70+
71+
public void foo(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException, NamingException {
72+
String ip = request.getRemoteAddr();
73+
InetAddress addr = InetAddress.getByName(ip);
74+
}
75+
76+
public void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) {
77+
throw new IllegalStateException("bla"); // Noncompliant {{Handle the "IllegalStateException" thrown here in a "try/catch" block.}}
78+
}
79+
public void bar(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException {
80+
throw new IllegalStateException("bla");
81+
}
82+
83+
protected void doPut(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException {
84+
try {
85+
foo(request, response); // Noncompliant [[sc=7;ec=10]] {{Handle the following exceptions that could be thrown by "foo": IOException, ServletException.}}
86+
} catch (NamingException ne) {
87+
throw new jakarta.servlet.ServletException(ne); // Noncompliant {{Handle the "ServletException" thrown here in a "try/catch" block.}}
88+
}
89+
}
90+
}

java-checks-test-sources/default/src/main/java/checks/TooManyParametersCheck.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import io.micronaut.http.annotation.Post;
1010
import io.micronaut.http.annotation.Put;
1111
import io.micronaut.http.annotation.Trace;
12+
import java.lang.annotation.ElementType;
13+
import java.lang.annotation.Target;
1214

1315
public class TooManyParametersCheck {
1416
TooManyParametersCheck(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) { // Noncompliant {{Constructor has 8 parameters, which is greater than 7 authorized.}}
@@ -20,6 +22,9 @@ public class TooManyParametersCheck {
2022
void otherMethod(int p1) {}
2123

2224
static void staticMethod(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) {} // Noncompliant
25+
26+
@CustomAnnotation
27+
void customAnnotatedMethod(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) {} // Noncompliant
2328
}
2429

2530
class TooManyParametersExtended extends TooManyParametersCheck {
@@ -81,4 +86,21 @@ public void trace(String p1, String p2, String p3, String p4, String p5, String
8186
*/
8287
record Record1(String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8) {} // Compliant
8388

84-
89+
class JakartaMethodsUsingAnnotations {
90+
@jakarta.ws.rs.GET
91+
public void foo(String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8) {} // Compliant
92+
93+
@jakarta.ws.rs.POST
94+
public void foo1(String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8) {} // Compliant
95+
96+
@jakarta.ws.rs.PUT
97+
public void foo2(String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8) {} // Compliant
98+
99+
@jakarta.ws.rs.PATCH
100+
public void foo3(String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8) {} // Compliant
101+
102+
@jakarta.inject.Inject
103+
public void foo5(String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8) {} // Compliant
104+
}
105+
@Target(ElementType.METHOD)
106+
@interface CustomAnnotation { }

java-checks-test-sources/default/src/main/java/checks/UnusedPrivateMethod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class UnusedPrivateMethodCheck {
1919

2020
private void init(@Observes Object object, String test) {} // Noncompliant
2121
private void init(@javax.enterprise.event.Observes Object object) {} //Compliant, javax.enterprise.event.Observes is an exception to the rule
22+
private void jakartaInit(@jakarta.enterprise.event.Observes Object object) {} //Compliant, jakarta.enterprise.event.Observes is an exception to the rule
2223
private void initNc(@AnotherAnnotation Object object) {} // Noncompliant
2324

2425
private UnusedPrivateMethodCheck() {}

java-checks-test-sources/default/src/main/java/checks/regex/SingleCharacterAlternationCheck.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class SingleCharacterAlternationCheck {
77
@Pattern(regexp = "x|y|z") // Noncompliant [[sc=22;ec=27]] {{Replace this alternation with a character class.}}
88
String pattern;
99

10+
@jakarta.validation.constraints.Pattern(regexp = "x|y|z") // Noncompliant [[sc=53;ec=58]] {{Replace this alternation with a character class.}}
11+
String jakartaPattern;
12+
1013
void nonCompliant() {
1114
String str = "abc123";
1215
str.matches("a|b|c"); // Noncompliant [[sc=18;ec=23]] {{Replace this alternation with a character class.}}

0 commit comments

Comments
 (0)