Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate detectionTool metadata for Sonar codemods #346

Merged
merged 17 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ public AddMissingOverrideCodemod(
super(issues, SimpleName.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S1161",
"`@Override` should be used on overriding and implementing methods",
"https://rules.sonarsource.com/java/RSPEC-1161/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand All @@ -53,4 +45,12 @@ public ChangesResult onIssueFound(
}
return ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S1161",
"`@Override` should be used on overriding and implementing methods",
"https://rules.sonarsource.com/java/RSPEC-1161/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ public AvoidImplicitPublicConstructorCodemod(
super(issues, SimpleName.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S1118",
"Utility classes should not have public constructors",
"https://rules.sonarsource.com/java/RSPEC-1118/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand Down Expand Up @@ -68,4 +60,12 @@ public ChangesResult onIssueFound(

return ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S1118",
"Utility classes should not have public constructors",
"https://rules.sonarsource.com/java/RSPEC-1118/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public DeclareVariableOnSeparateLineCodemod(
super(issues, VariableDeclarator.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S1659",
"Multiple variables should not be declared on the same line",
"https://rules.sonarsource.com/java/RSPEC-1659/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand Down Expand Up @@ -68,4 +60,12 @@ public ChangesResult onIssueFound(
? ChangesResult.changesApplied
: ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S1659",
"Multiple variables should not be declared on the same line",
"https://rules.sonarsource.com/java/RSPEC-1659/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;

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

@Override
public DetectorRule getDetectorRule() {
public DetectorRule detectorRule() {
return new DetectorRule(
"java.lang.security.audit.sqli.jdbc-sqli.jdbc-sqli",
"java.lang.security.audit.sqli.jdbc-sqli.jdbc-sqli",
"https://semgrep.dev/r?q=java.lang.security.audit.sqli.jdbc-sqli.jdbc-sqli");
}

@Override
public Optional<FixedFinding> getFixedFinding(String id) {
return Optional.of(new FixedFinding(id, getDetectorRule()));
}

@Override
public CodemodFileScanningResult visit(
final CodemodInvocationContext context, final CompilationUnit cu) {
Expand All @@ -72,7 +66,7 @@ public CodemodFileScanningResult visit(
if (line == null) {
UnfixedFinding unfixableFinding =
new UnfixedFinding(
id, getDetectorRule(), context.path().toString(), null, "No line number provided");
id, detectorRule(), context.path().toString(), null, "No line number provided");
unfixedFindings.add(unfixableFinding);
continue;
}
Expand All @@ -87,7 +81,7 @@ public CodemodFileScanningResult visit(
UnfixedFinding unfixableFinding =
new UnfixedFinding(
id,
getDetectorRule(),
detectorRule(),
context.path().toString(),
line,
"No supported SQL methods found on the given line");
Expand All @@ -99,7 +93,7 @@ public CodemodFileScanningResult visit(
UnfixedFinding unfixableFinding =
new UnfixedFinding(
id,
getDetectorRule(),
detectorRule(),
context.path().toString(),
line,
"Multiple supported SQL methods found on the given line");
Expand All @@ -109,12 +103,12 @@ public CodemodFileScanningResult visit(

MethodCallExpr methodCallExpr = supportedSqlMethodCallsOnThatLine.get(0);
if (SQLParameterizerWithCleanup.checkAndFix(methodCallExpr)) {
changes.add(CodemodChange.from(line, getFixedFinding(id).get()));
changes.add(CodemodChange.from(line, new FixedFinding(id, detectorRule())));
} else {
UnfixedFinding unfixableFinding =
new UnfixedFinding(
id,
getDetectorRule(),
detectorRule(),
context.path().toString(),
line,
"State changing effects possible or unrecognized code shape");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public DefineConstantForLiteralCodemod(
super(issues, StringLiteralExpr.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S1192",
"String literals should not be duplicated",
"https://rules.sonarsource.com/java/RSPEC-1192/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand All @@ -55,4 +47,12 @@ public ChangesResult onIssueFound(
? ChangesResult.changesApplied
: ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S1192",
"String literals should not be duplicated",
"https://rules.sonarsource.com/java/RSPEC-1192/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public FixRedundantStaticOnEnumCodemod(
super(issues, EnumDeclaration.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S2786",
"Nested `enum`s should not be declared static",
"https://rules.sonarsource.com/java/RSPEC-2786/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand All @@ -46,4 +38,12 @@ public ChangesResult onIssueFound(
}
return ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S2786",
"Nested `enum`s should not be declared static",
"https://rules.sonarsource.com/java/RSPEC-2786/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ public HardenParseForConstructorChanger(
CodemodReporterStrategy.empty());
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S2130",
"Parsing should be used to convert `String`s to primitives",
"https://rules.sonarsource.com/java/RSPEC-2130/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand Down Expand Up @@ -105,6 +97,14 @@ private Optional<Expression> extractArgumentExpression(Expression argumentExpres
// Handle other cases or return null if unable to extract the argument expression
return Optional.empty();
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S2130",
"Parsing should be used to convert `String`s to primitives",
"https://rules.sonarsource.com/java/RSPEC-2130/");
}
}

/**
Expand All @@ -124,14 +124,6 @@ public HardenParseForValueOfChanger(
CodemodReporterStrategy.empty());
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S2130",
"Parsing should be used to convert `String`s to primitives",
"https://rules.sonarsource.com/java/RSPEC-2130/");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand Down Expand Up @@ -189,5 +181,13 @@ private boolean handleMethodCallChainsAfterValueOfIfNeeded(

return true;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S2130",
"Parsing should be used to convert `String`s to primitives",
"https://rules.sonarsource.com/java/RSPEC-2130/");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ public OverridesMatchParentSynchronizationCodemod(
super(issues, SimpleName.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S3551",
"Overrides should match their parent class methods in synchronization",
"https://rules.sonarsource.com/java/RSPEC-3551");
}

@Override
public ChangesResult onIssueFound(
CodemodInvocationContext context, CompilationUnit cu, SimpleName methodName, Issue issue) {
Expand All @@ -53,4 +45,12 @@ public ChangesResult onIssueFound(
}
return ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S3551",
"Overrides should match their parent class methods in synchronization",
"https://rules.sonarsource.com/java/RSPEC-3551");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public RemoveCommentedCodeCodemod(
super(issues, Comment.class, regionNodeMatcher, NodeCollector.ALL_COMMENTS);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S125",
"Sections of code should not be commented out",
"https://rules.sonarsource.com/java/RSPEC-125");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand All @@ -60,4 +52,12 @@ public ChangesResult onIssueFound(

return ChangesResult.changesApplied;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S125",
"Sections of code should not be commented out",
"https://rules.sonarsource.com/java/RSPEC-125");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ public RemoveRedundantVariableCreationCodemod(
super(issues, ObjectCreationExpr.class);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S1488",
"Local variables should not be declared and then immediately returned or thrown",
"https://rules.sonarsource.com/java/RSPEC-1488");
}

@Override
public ChangesResult onIssueFound(
final CodemodInvocationContext context,
Expand Down Expand Up @@ -74,4 +66,12 @@ public ChangesResult onIssueFound(

return ChangesResult.noChanges;
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S1488",
"Local variables should not be declared and then immediately returned or thrown",
"https://rules.sonarsource.com/java/RSPEC-1488");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public RemoveUnusedImportCodemod(
NodeCollector.ALL_FROM_TYPE);
}

@Override
public DetectorRule getDetectorRule() {
return new DetectorRule(
"java:S1128",
"Unnecessary imports should be removed",
"https://rules.sonarsource.com/java/RSPEC-1128");
}

@Override
public ChangesResult onIssueFound(
CodemodInvocationContext context, CompilationUnit cu, ImportDeclaration node, Issue issue) {
Expand All @@ -58,4 +50,12 @@ public ChangesResult onIssueFound(
return ChangesResult.noChanges;
}
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S1128",
"Unnecessary imports should be removed",
"https://rules.sonarsource.com/java/RSPEC-1128");
}
}
Loading
Loading