Skip to content

Commit 380a8a9

Browse files
authored
Populate detectionTool metadata for Sonar codemods (#346)
add finding to sonar codemods and add assertion to check if `FixOnlyCodeChanger` has findings Issue #337
1 parent 1c274d7 commit 380a8a9

26 files changed

+219
-221
lines changed

core-codemods/src/main/java/io/codemodder/codemods/AddMissingOverrideCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ public AddMissingOverrideCodemod(
2828
super(issues, SimpleName.class);
2929
}
3030

31-
@Override
32-
public DetectorRule getDetectorRule() {
33-
return new DetectorRule(
34-
"java:S1161",
35-
"`@Override` should be used on overriding and implementing methods",
36-
"https://rules.sonarsource.com/java/RSPEC-1161/");
37-
}
38-
3931
@Override
4032
public ChangesResult onIssueFound(
4133
final CodemodInvocationContext context,
@@ -53,4 +45,12 @@ public ChangesResult onIssueFound(
5345
}
5446
return ChangesResult.noChanges;
5547
}
48+
49+
@Override
50+
public DetectorRule detectorRule() {
51+
return new DetectorRule(
52+
"java:S1161",
53+
"`@Override` should be used on overriding and implementing methods",
54+
"https://rules.sonarsource.com/java/RSPEC-1161/");
55+
}
5656
}

core-codemods/src/main/java/io/codemodder/codemods/AvoidImplicitPublicConstructorCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ public AvoidImplicitPublicConstructorCodemod(
3333
super(issues, SimpleName.class);
3434
}
3535

36-
@Override
37-
public DetectorRule getDetectorRule() {
38-
return new DetectorRule(
39-
"java:S1118",
40-
"Utility classes should not have public constructors",
41-
"https://rules.sonarsource.com/java/RSPEC-1118/");
42-
}
43-
4436
@Override
4537
public ChangesResult onIssueFound(
4638
final CodemodInvocationContext context,
@@ -68,4 +60,12 @@ public ChangesResult onIssueFound(
6860

6961
return ChangesResult.noChanges;
7062
}
63+
64+
@Override
65+
public DetectorRule detectorRule() {
66+
return new DetectorRule(
67+
"java:S1118",
68+
"Utility classes should not have public constructors",
69+
"https://rules.sonarsource.com/java/RSPEC-1118/");
70+
}
7171
}

core-codemods/src/main/java/io/codemodder/codemods/DeclareVariableOnSeparateLineCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ public DeclareVariableOnSeparateLineCodemod(
3030
super(issues, VariableDeclarator.class);
3131
}
3232

33-
@Override
34-
public DetectorRule getDetectorRule() {
35-
return new DetectorRule(
36-
"java:S1659",
37-
"Multiple variables should not be declared on the same line",
38-
"https://rules.sonarsource.com/java/RSPEC-1659/");
39-
}
40-
4133
@Override
4234
public ChangesResult onIssueFound(
4335
final CodemodInvocationContext context,
@@ -68,4 +60,12 @@ public ChangesResult onIssueFound(
6860
? ChangesResult.changesApplied
6961
: ChangesResult.noChanges;
7062
}
63+
64+
@Override
65+
public DetectorRule detectorRule() {
66+
return new DetectorRule(
67+
"java:S1659",
68+
"Multiple variables should not be declared on the same line",
69+
"https://rules.sonarsource.com/java/RSPEC-1659/");
70+
}
7171
}

core-codemods/src/main/java/io/codemodder/codemods/DefectDojoSqlInjectionCodemod.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.ArrayList;
1414
import java.util.List;
1515
import java.util.Objects;
16-
import java.util.Optional;
1716
import javax.inject.Inject;
1817

1918
/** This codemod knows how to translate */
@@ -40,18 +39,13 @@ public String vendorName() {
4039
}
4140

4241
@Override
43-
public DetectorRule getDetectorRule() {
42+
public DetectorRule detectorRule() {
4443
return new DetectorRule(
4544
"java.lang.security.audit.sqli.jdbc-sqli.jdbc-sqli",
4645
"java.lang.security.audit.sqli.jdbc-sqli.jdbc-sqli",
4746
"https://semgrep.dev/r?q=java.lang.security.audit.sqli.jdbc-sqli.jdbc-sqli");
4847
}
4948

50-
@Override
51-
public Optional<FixedFinding> getFixedFinding(String id) {
52-
return Optional.of(new FixedFinding(id, getDetectorRule()));
53-
}
54-
5549
@Override
5650
public CodemodFileScanningResult visit(
5751
final CodemodInvocationContext context, final CompilationUnit cu) {
@@ -72,7 +66,7 @@ public CodemodFileScanningResult visit(
7266
if (line == null) {
7367
UnfixedFinding unfixableFinding =
7468
new UnfixedFinding(
75-
id, getDetectorRule(), context.path().toString(), null, "No line number provided");
69+
id, detectorRule(), context.path().toString(), null, "No line number provided");
7670
unfixedFindings.add(unfixableFinding);
7771
continue;
7872
}
@@ -87,7 +81,7 @@ public CodemodFileScanningResult visit(
8781
UnfixedFinding unfixableFinding =
8882
new UnfixedFinding(
8983
id,
90-
getDetectorRule(),
84+
detectorRule(),
9185
context.path().toString(),
9286
line,
9387
"No supported SQL methods found on the given line");
@@ -99,7 +93,7 @@ public CodemodFileScanningResult visit(
9993
UnfixedFinding unfixableFinding =
10094
new UnfixedFinding(
10195
id,
102-
getDetectorRule(),
96+
detectorRule(),
10397
context.path().toString(),
10498
line,
10599
"Multiple supported SQL methods found on the given line");
@@ -109,12 +103,12 @@ public CodemodFileScanningResult visit(
109103

110104
MethodCallExpr methodCallExpr = supportedSqlMethodCallsOnThatLine.get(0);
111105
if (SQLParameterizerWithCleanup.checkAndFix(methodCallExpr)) {
112-
changes.add(CodemodChange.from(line, getFixedFinding(id).get()));
106+
changes.add(CodemodChange.from(line, new FixedFinding(id, detectorRule())));
113107
} else {
114108
UnfixedFinding unfixableFinding =
115109
new UnfixedFinding(
116110
id,
117-
getDetectorRule(),
111+
detectorRule(),
118112
context.path().toString(),
119113
line,
120114
"State changing effects possible or unrecognized code shape");

core-codemods/src/main/java/io/codemodder/codemods/DefineConstantForLiteralCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ public DefineConstantForLiteralCodemod(
2626
super(issues, StringLiteralExpr.class);
2727
}
2828

29-
@Override
30-
public DetectorRule getDetectorRule() {
31-
return new DetectorRule(
32-
"java:S1192",
33-
"String literals should not be duplicated",
34-
"https://rules.sonarsource.com/java/RSPEC-1192/");
35-
}
36-
3729
@Override
3830
public ChangesResult onIssueFound(
3931
final CodemodInvocationContext context,
@@ -55,4 +47,12 @@ public ChangesResult onIssueFound(
5547
? ChangesResult.changesApplied
5648
: ChangesResult.noChanges;
5749
}
50+
51+
@Override
52+
public DetectorRule detectorRule() {
53+
return new DetectorRule(
54+
"java:S1192",
55+
"String literals should not be duplicated",
56+
"https://rules.sonarsource.com/java/RSPEC-1192/");
57+
}
5858
}

core-codemods/src/main/java/io/codemodder/codemods/FixRedundantStaticOnEnumCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ public FixRedundantStaticOnEnumCodemod(
2626
super(issues, EnumDeclaration.class);
2727
}
2828

29-
@Override
30-
public DetectorRule getDetectorRule() {
31-
return new DetectorRule(
32-
"java:S2786",
33-
"Nested `enum`s should not be declared static",
34-
"https://rules.sonarsource.com/java/RSPEC-2786/");
35-
}
36-
3729
@Override
3830
public ChangesResult onIssueFound(
3931
final CodemodInvocationContext context,
@@ -46,4 +38,12 @@ public ChangesResult onIssueFound(
4638
}
4739
return ChangesResult.noChanges;
4840
}
41+
42+
@Override
43+
public DetectorRule detectorRule() {
44+
return new DetectorRule(
45+
"java:S2786",
46+
"Nested `enum`s should not be declared static",
47+
"https://rules.sonarsource.com/java/RSPEC-2786/");
48+
}
4949
}

core-codemods/src/main/java/io/codemodder/codemods/HardenStringParseToPrimitivesCodemod.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ public HardenParseForConstructorChanger(
6262
CodemodReporterStrategy.empty());
6363
}
6464

65-
@Override
66-
public DetectorRule getDetectorRule() {
67-
return new DetectorRule(
68-
"java:S2130",
69-
"Parsing should be used to convert `String`s to primitives",
70-
"https://rules.sonarsource.com/java/RSPEC-2130/");
71-
}
72-
7365
@Override
7466
public ChangesResult onIssueFound(
7567
final CodemodInvocationContext context,
@@ -105,6 +97,14 @@ private Optional<Expression> extractArgumentExpression(Expression argumentExpres
10597
// Handle other cases or return null if unable to extract the argument expression
10698
return Optional.empty();
10799
}
100+
101+
@Override
102+
public DetectorRule detectorRule() {
103+
return new DetectorRule(
104+
"java:S2130",
105+
"Parsing should be used to convert `String`s to primitives",
106+
"https://rules.sonarsource.com/java/RSPEC-2130/");
107+
}
108108
}
109109

110110
/**
@@ -124,14 +124,6 @@ public HardenParseForValueOfChanger(
124124
CodemodReporterStrategy.empty());
125125
}
126126

127-
@Override
128-
public DetectorRule getDetectorRule() {
129-
return new DetectorRule(
130-
"java:S2130",
131-
"Parsing should be used to convert `String`s to primitives",
132-
"https://rules.sonarsource.com/java/RSPEC-2130/");
133-
}
134-
135127
@Override
136128
public ChangesResult onIssueFound(
137129
final CodemodInvocationContext context,
@@ -189,5 +181,13 @@ private boolean handleMethodCallChainsAfterValueOfIfNeeded(
189181

190182
return true;
191183
}
184+
185+
@Override
186+
public DetectorRule detectorRule() {
187+
return new DetectorRule(
188+
"java:S2130",
189+
"Parsing should be used to convert `String`s to primitives",
190+
"https://rules.sonarsource.com/java/RSPEC-2130/");
191+
}
192192
}
193193
}

core-codemods/src/main/java/io/codemodder/codemods/OverridesMatchParentSynchronizationCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ public OverridesMatchParentSynchronizationCodemod(
3232
super(issues, SimpleName.class);
3333
}
3434

35-
@Override
36-
public DetectorRule getDetectorRule() {
37-
return new DetectorRule(
38-
"java:S3551",
39-
"Overrides should match their parent class methods in synchronization",
40-
"https://rules.sonarsource.com/java/RSPEC-3551");
41-
}
42-
4335
@Override
4436
public ChangesResult onIssueFound(
4537
CodemodInvocationContext context, CompilationUnit cu, SimpleName methodName, Issue issue) {
@@ -53,4 +45,12 @@ public ChangesResult onIssueFound(
5345
}
5446
return ChangesResult.noChanges;
5547
}
48+
49+
@Override
50+
public DetectorRule detectorRule() {
51+
return new DetectorRule(
52+
"java:S3551",
53+
"Overrides should match their parent class methods in synchronization",
54+
"https://rules.sonarsource.com/java/RSPEC-3551");
55+
}
5656
}

core-codemods/src/main/java/io/codemodder/codemods/RemoveCommentedCodeCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ public RemoveCommentedCodeCodemod(
4141
super(issues, Comment.class, regionNodeMatcher, NodeCollector.ALL_COMMENTS);
4242
}
4343

44-
@Override
45-
public DetectorRule getDetectorRule() {
46-
return new DetectorRule(
47-
"java:S125",
48-
"Sections of code should not be commented out",
49-
"https://rules.sonarsource.com/java/RSPEC-125");
50-
}
51-
5244
@Override
5345
public ChangesResult onIssueFound(
5446
final CodemodInvocationContext context,
@@ -60,4 +52,12 @@ public ChangesResult onIssueFound(
6052

6153
return ChangesResult.changesApplied;
6254
}
55+
56+
@Override
57+
public DetectorRule detectorRule() {
58+
return new DetectorRule(
59+
"java:S125",
60+
"Sections of code should not be commented out",
61+
"https://rules.sonarsource.com/java/RSPEC-125");
62+
}
6363
}

core-codemods/src/main/java/io/codemodder/codemods/RemoveRedundantVariableCreationCodemod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ public RemoveRedundantVariableCreationCodemod(
2828
super(issues, ObjectCreationExpr.class);
2929
}
3030

31-
@Override
32-
public DetectorRule getDetectorRule() {
33-
return new DetectorRule(
34-
"java:S1488",
35-
"Local variables should not be declared and then immediately returned or thrown",
36-
"https://rules.sonarsource.com/java/RSPEC-1488");
37-
}
38-
3931
@Override
4032
public ChangesResult onIssueFound(
4133
final CodemodInvocationContext context,
@@ -74,4 +66,12 @@ public ChangesResult onIssueFound(
7466

7567
return ChangesResult.noChanges;
7668
}
69+
70+
@Override
71+
public DetectorRule detectorRule() {
72+
return new DetectorRule(
73+
"java:S1488",
74+
"Local variables should not be declared and then immediately returned or thrown",
75+
"https://rules.sonarsource.com/java/RSPEC-1488");
76+
}
7777
}

0 commit comments

Comments
 (0)