Skip to content

Commit

Permalink
Added check for existing overrides in add-missing-override (#447)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrecsilva authored Sep 17, 2024
1 parent 1997307 commit 50cf515
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.codemodder.codemods;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.SimpleName;
import io.codemodder.*;
Expand All @@ -11,7 +10,6 @@
import io.codemodder.providers.sonar.RuleIssue;
import io.codemodder.providers.sonar.SonarPluginJavaParserChanger;
import io.codemodder.sonar.model.Issue;
import java.util.Optional;
import javax.inject.Inject;

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

Optional<Node> parentNodeRef = methodName.getParentNode();
if (parentNodeRef.isPresent()) {
Node parentNode = parentNodeRef.get();
if (parentNode instanceof MethodDeclaration method) {
method.addAnnotation(Override.class);
return ChangesResult.changesApplied;
}
}
return ChangesResult.noChanges;
var maybeMethodName =
methodName
.getParentNode()
.map(p -> p instanceof MethodDeclaration ? (MethodDeclaration) p : null)
.filter(mr -> !mr.getAnnotationByName("Override").isPresent());
maybeMethodName.ifPresent(mr -> mr.addAnnotation(Override.class));
return maybeMethodName.map(mr -> ChangesResult.changesApplied).orElse(ChangesResult.noChanges);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public class SqlInjectionLesson10b extends AssignmentEndpoint {
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return contents;
}

@Override
public CharSequence getCharContent2(boolean ignoreEncodingErrors) throws IOException {
return contents;
}
}

private boolean check_text(String regex, String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public class SqlInjectionLesson10b extends AssignmentEndpoint {
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return contents;
}

@Override
public CharSequence getCharContent2(boolean ignoreEncodingErrors) throws IOException {
return contents;
}
}

private boolean check_text(String regex, String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,42 @@
"severity": "MEDIUM"
}
]
},
{
"key": "AYvtrjy0LCzGLicz7Ajy",
"rule": "java:S1161",
"severity": "MAJOR",
"component": "nahsra_WebGoat_10_23:src/main/java/SqlInjectionLesson10b.java",
"project": "nahsra_WebGoat_10_23",
"line": 148,
"hash": "e4d44f915becc09c0ce386b304b7621b",
"textRange": {
"startLine": 148,
"endLine": 148,
"startOffset": 24,
"endOffset": 39
},
"flows": [],
"status": "OPEN",
"message": "Add the \"@Override\" annotation above this method signature",
"effort": "5min",
"debt": "5min",
"author": "[email protected]",
"tags": [
"bad-practice"
],
"creationDate": "2022-04-09T14:56:12+0200",
"updateDate": "2023-11-20T17:58:54+0100",
"type": "CODE_SMELL",
"organization": "nahsra",
"cleanCodeAttribute": "CLEAR",
"cleanCodeAttributeCategory": "INTENTIONAL",
"impacts": [
{
"softwareQuality": "MAINTAINABILITY",
"severity": "MEDIUM"
}
]
}
],
"components": [
Expand Down

0 comments on commit 50cf515

Please sign in to comment.