Skip to content

Commit 616bfdd

Browse files
SONARJAVA-4638 Improve Jakarta support (#4623)
Add support for the Jakarta namespace for the following checks: - RegexComplexityCheck - ReluctantQuantifierWithEmptyContinuationCheck - SecureCookieCheck - SerializableObjectInSessionCheck - ServletInstanceFieldCheck
1 parent 4f27457 commit 616bfdd

20 files changed

Lines changed: 321 additions & 34 deletions

File tree

its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void javaCheckTestSources() throws Exception {
181181
}
182182

183183
// store new unexpected diffs in JSON files - serializable
184-
Files.createDirectory(pathFor(TARGET_ACTUAL + "autoscan-diffs/"));
184+
Files.createDirectories(pathFor(TARGET_ACTUAL + "autoscan-diffs/"));
185185
for (var newDiff : newDiffs) {
186186
if (!newDiff.equals(knownDiffs.get(newDiff.ruleKey))) {
187187
Files.writeString(pathFor(TARGET_ACTUAL + "autoscan-diffs/diff_" + newDiff.ruleKey + ".json"), GSON.toJson(newDiff));
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1874",
33
"hasTruePositives": true,
4-
"falseNegatives": 93,
4+
"falseNegatives": 111,
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": "S1948",
33
"hasTruePositives": true,
4-
"falseNegatives": 0,
4+
"falseNegatives": 1,
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": "S2092",
33
"hasTruePositives": true,
4-
"falseNegatives": 42,
4+
"falseNegatives": 93,
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": "S2160",
33
"hasTruePositives": true,
4-
"falseNegatives": 1,
4+
"falseNegatives": 2,
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": "S2226",
33
"hasTruePositives": false,
4-
"falseNegatives": 5,
4+
"falseNegatives": 9,
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": "S2441",
33
"hasTruePositives": true,
4-
"falseNegatives": 0,
4+
"falseNegatives": 1,
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": "S3330",
33
"hasTruePositives": true,
4-
"falseNegatives": 51,
4+
"falseNegatives": 77,
55
"falsePositives": 0
66
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package checks;
2+
3+
import java.util.function.Function;
4+
import jakarta.annotation.Resource;
5+
import jakarta.servlet.http.HttpServlet;
6+
import org.apache.struts.action.Action;
7+
8+
class HttpServletAJakarta {
9+
private String userName;
10+
}
11+
12+
class HttpServletBJakarta extends HttpServlet {
13+
private String userName; // Noncompliant [[sc=18;ec=26]] {{Remove this misleading mutable servlet instance field or make it "static" and/or "final"}}
14+
private static String staticVar;
15+
private final String finalVar;
16+
private String storageType;
17+
private static final Function<Integer, Integer> LAMBDA = lambdaParam -> {
18+
Integer lambdaVar = null;
19+
return lambdaVar;
20+
};
21+
22+
public HttpServletBJakarta(String x) {
23+
String localVar;
24+
finalVar = x;
25+
}
26+
27+
public void init(jakarta.servlet.ServletConfig config) {
28+
storageType = StorageType.valueOf(config.getInitParameter("storageType"));
29+
}
30+
31+
private static class StorageType {
32+
public static String valueOf(String storageType) {
33+
return null;
34+
}
35+
}
36+
}
37+
38+
class HttpServletCJakarta extends Action {
39+
40+
private String userName; // Noncompliant
41+
private static String staticVar;
42+
private final String finalVar;
43+
44+
public HttpServletCJakarta(String x) {
45+
finalVar = x;
46+
}
47+
}
48+
49+
class HttpServletDJakarta extends HttpServlet {
50+
51+
@jakarta.inject.Inject private String userName; // compliant annotated with inject;
52+
@Inject private String userName1; // Noncompliant
53+
@Resource private String city; // compliant annotated with resource;
54+
private static String staticVar;
55+
}
56+
57+
public class ServletInstanceFieldCheckJakarta extends HttpServlet {
58+
@org.springframework.beans.factory.annotation.Autowired
59+
private javax.sql.DataSource myDB; // Noncompliant - filtered by the SpringFilter
60+
}
61+
62+
class HttpServletEJakarta extends HttpServlet {
63+
private String userName; // Noncompliant [[sc=18;ec=26]] {{Remove this misleading mutable servlet instance field or make it "static" and/or "final"}}
64+
private final String finalVar;
65+
private String storageType; // Compliant, initialized in init() method
66+
67+
public HttpServletEJakarta(String x) {
68+
String localVar;
69+
finalVar = x;
70+
}
71+
72+
public void init() {
73+
storageType = StorageType.valueOf(getServletConfig().getInitParameter("storageType"));
74+
}
75+
76+
private static class StorageType {
77+
public static String valueOf(String storageType) {
78+
return null;
79+
}
80+
}
81+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class RegexComplexityCheck {
1616
@Email(regexp = "((((a|b)|(c|d))+|((e|f)|(g|h))+)+|(((h|i)|(j|j))+|((k|l)|(m|n))+)+)")
1717
private String email;
1818

19+
// Noncompliant@+1
20+
@jakarta.validation.constraints.Email(regexp = "((((a|b)|(c|d))+|((e|f)|(g|h))+)+|(((h|i)|(j|j))+|((k|l)|(m|n))+)+)")
21+
private String emailJakarta;
22+
1923
void noncompliant(String str) {
2024
// Noncompliant@+2 [[sc=7;ec=8]] {{Simplify this regular expression to reduce its complexity from 106 to the 20 allowed.}}
2125
str.matches(

0 commit comments

Comments
 (0)