From 50cf515d8f58429a450a61c9cfdf759f58b8a0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20C=2E=20Silva?= <12188364+andrecsilva@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:59:31 -0300 Subject: [PATCH] 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. --- .../codemods/AddMissingOverrideCodemod.java | 18 ++++------ .../Test.java.after | 5 +++ .../Test.java.before | 5 +++ .../sonar-issues_1.json | 36 +++++++++++++++++++ 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/core-codemods/src/main/java/io/codemodder/codemods/AddMissingOverrideCodemod.java b/core-codemods/src/main/java/io/codemodder/codemods/AddMissingOverrideCodemod.java index 5f724c700..d24e2033e 100644 --- a/core-codemods/src/main/java/io/codemodder/codemods/AddMissingOverrideCodemod.java +++ b/core-codemods/src/main/java/io/codemodder/codemods/AddMissingOverrideCodemod.java @@ -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.*; @@ -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. */ @@ -36,15 +34,13 @@ public ChangesResult onFindingFound( final SimpleName methodName, final Issue issue) { - Optional 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 diff --git a/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.after b/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.after index 0c4938d36..39c29fa33 100644 --- a/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.after +++ b/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.after @@ -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) { diff --git a/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.before b/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.before index 325d376bb..bed67f034 100644 --- a/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.before +++ b/core-codemods/src/test/resources/add-missing-override-s1161/Test.java.before @@ -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) { diff --git a/core-codemods/src/test/resources/add-missing-override-s1161/sonar-issues_1.json b/core-codemods/src/test/resources/add-missing-override-s1161/sonar-issues_1.json index 3cbf420bc..f4a6d261c 100644 --- a/core-codemods/src/test/resources/add-missing-override-s1161/sonar-issues_1.json +++ b/core-codemods/src/test/resources/add-missing-override-s1161/sonar-issues_1.json @@ -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": "nanne.baars@owasp.org", + "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": [