Skip to content

Commit 50cf515

Browse files
authored
Added check for existing overrides in add-missing-override (#447)
If you have multiple findings of the same rule for the same location, some codemods will duplicate their fixes. This PR adds a patch to stop this for the `add-missing-override` codemod.
1 parent 1997307 commit 50cf515

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.codemodder.codemods;
22

33
import com.github.javaparser.ast.CompilationUnit;
4-
import com.github.javaparser.ast.Node;
54
import com.github.javaparser.ast.body.MethodDeclaration;
65
import com.github.javaparser.ast.expr.SimpleName;
76
import io.codemodder.*;
@@ -11,7 +10,6 @@
1110
import io.codemodder.providers.sonar.RuleIssue;
1211
import io.codemodder.providers.sonar.SonarPluginJavaParserChanger;
1312
import io.codemodder.sonar.model.Issue;
14-
import java.util.Optional;
1513
import javax.inject.Inject;
1614

1715
/** A codemod for automatically fixing missing @Override annotations. */
@@ -36,15 +34,13 @@ public ChangesResult onFindingFound(
3634
final SimpleName methodName,
3735
final Issue issue) {
3836

39-
Optional<Node> parentNodeRef = methodName.getParentNode();
40-
if (parentNodeRef.isPresent()) {
41-
Node parentNode = parentNodeRef.get();
42-
if (parentNode instanceof MethodDeclaration method) {
43-
method.addAnnotation(Override.class);
44-
return ChangesResult.changesApplied;
45-
}
46-
}
47-
return ChangesResult.noChanges;
37+
var maybeMethodName =
38+
methodName
39+
.getParentNode()
40+
.map(p -> p instanceof MethodDeclaration ? (MethodDeclaration) p : null)
41+
.filter(mr -> !mr.getAnnotationByName("Override").isPresent());
42+
maybeMethodName.ifPresent(mr -> mr.addAnnotation(Override.class));
43+
return maybeMethodName.map(mr -> ChangesResult.changesApplied).orElse(ChangesResult.noChanges);
4844
}
4945

5046
@Override

core-codemods/src/test/resources/add-missing-override-s1161/Test.java.after

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public class SqlInjectionLesson10b extends AssignmentEndpoint {
144144
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
145145
return contents;
146146
}
147+
148+
@Override
149+
public CharSequence getCharContent2(boolean ignoreEncodingErrors) throws IOException {
150+
return contents;
151+
}
147152
}
148153

149154
private boolean check_text(String regex, String text) {

core-codemods/src/test/resources/add-missing-override-s1161/Test.java.before

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public class SqlInjectionLesson10b extends AssignmentEndpoint {
143143
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
144144
return contents;
145145
}
146+
147+
@Override
148+
public CharSequence getCharContent2(boolean ignoreEncodingErrors) throws IOException {
149+
return contents;
150+
}
146151
}
147152

148153
private boolean check_text(String regex, String text) {

core-codemods/src/test/resources/add-missing-override-s1161/sonar-issues_1.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,42 @@
4545
"severity": "MEDIUM"
4646
}
4747
]
48+
},
49+
{
50+
"key": "AYvtrjy0LCzGLicz7Ajy",
51+
"rule": "java:S1161",
52+
"severity": "MAJOR",
53+
"component": "nahsra_WebGoat_10_23:src/main/java/SqlInjectionLesson10b.java",
54+
"project": "nahsra_WebGoat_10_23",
55+
"line": 148,
56+
"hash": "e4d44f915becc09c0ce386b304b7621b",
57+
"textRange": {
58+
"startLine": 148,
59+
"endLine": 148,
60+
"startOffset": 24,
61+
"endOffset": 39
62+
},
63+
"flows": [],
64+
"status": "OPEN",
65+
"message": "Add the \"@Override\" annotation above this method signature",
66+
"effort": "5min",
67+
"debt": "5min",
68+
"author": "nanne.baars@owasp.org",
69+
"tags": [
70+
"bad-practice"
71+
],
72+
"creationDate": "2022-04-09T14:56:12+0200",
73+
"updateDate": "2023-11-20T17:58:54+0100",
74+
"type": "CODE_SMELL",
75+
"organization": "nahsra",
76+
"cleanCodeAttribute": "CLEAR",
77+
"cleanCodeAttributeCategory": "INTENTIONAL",
78+
"impacts": [
79+
{
80+
"softwareQuality": "MAINTAINABILITY",
81+
"severity": "MEDIUM"
82+
}
83+
]
4884
}
4985
],
5086
"components": [

0 commit comments

Comments
 (0)