Skip to content

Commit

Permalink
SONARJAVA-4462 Add quickfix support for S6485 (#4461)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorian-burihabwa-sonarsource authored Sep 30, 2024
1 parent 19be817 commit 8fef1ca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class KnownCapacityHashBasedCollectionCheckSample {
private static final int cap = 1;

void nonCompliant() {
HashMap<String, String> quickFixable = new HashMap<>(100); // Noncompliant[[sc=44;ec=62;quickfixes=map]]
//fix@map {{Replace with "HashMap.newHashMap".}}
//edit@map [[sc=44;ec=57]] {{HashMap.newHashMap}}

HashMap<String, String> hashMap = new HashMap<>(100); // Noncompliant[[sc=39;ec=57;]]
HashMap<String, String> hashMap2 = new HashMap<>(100); // Noncompliant {{Replace this call to the constructor with the better suited static method HashMap.newHashMap(int numMappings)}}
HashSet<String> hashSet2 = new HashSet<>(100); // Noncompliant {{Replace this call to the constructor with the better suited static method HashSet.newHashSet(int numMappings)}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import java.util.List;
import java.util.Map;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
import org.sonar.java.checks.helpers.QuickFixHelper;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.reporting.JavaTextEdit;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.tree.NewClassTree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
Expand All @@ -47,7 +50,12 @@ public List<Kind> nodesToVisit() {

@Override
protected void onConstructorFound(NewClassTree newClassTree) {
reportIssue(newClassTree, getIssueMessage(newClassTree));
QuickFixHelper.newIssue(context)
.forRule(this)
.onTree(newClassTree)
.withMessage(getIssueMessage(newClassTree))
.withQuickFix(() -> computeQuickFix(newClassTree))
.report();
}

@Override
Expand All @@ -69,4 +77,12 @@ private static String getIssueMessage(NewClassTree newClassTree) {
return String.format("Replace this call to the constructor with the better suited static method %s", replacementMethod);
}

private static JavaQuickFix computeQuickFix(NewClassTree newClassTree) {
String replacementMethod = TYPES_TO_METHODS.get(newClassTree.symbolType().name()).replace("(int numMappings)", "");
JavaTextEdit edit = JavaTextEdit.replaceBetweenTree(newClassTree.firstToken(), newClassTree.identifier().lastToken(), replacementMethod);
return JavaQuickFix.newQuickFix("Replace with \"" + replacementMethod + "\".")
.addTextEdit(edit)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ void test() {
}

@Test
void testNoIssuesOnEarlierJavaVersions() {

var check = new KnownCapacityHashBasedCollectionCheck();

void test_no_issues_on_java_versions_less_than_19() {
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/KnownCapacityHashBasedCollectionCheckSample.java"))
.withCheck(check)
.withCheck(new KnownCapacityHashBasedCollectionCheck())
.withJavaVersion(18)
.verifyNoIssues();

CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/KnownCapacityHashBasedCollectionCheckSample.java"))
.withCheck(check)
.verifyNoIssues();
}

}

0 comments on commit 8fef1ca

Please sign in to comment.