-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat: Add CollectionsEmptyConstantsProcessor (SonarSource rule 1596) #574
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||||||||||||||||||||||||
package sorald.processor; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import java.util.Collections; | ||||||||||||||||||||||||||||||
import sorald.annotations.ProcessorAnnotation; | ||||||||||||||||||||||||||||||
import spoon.reflect.code.*; | ||||||||||||||||||||||||||||||
import spoon.reflect.declaration.CtMethod; | ||||||||||||||||||||||||||||||
import spoon.reflect.declaration.CtType; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@ProcessorAnnotation( | ||||||||||||||||||||||||||||||
key = 1596, | ||||||||||||||||||||||||||||||
description = | ||||||||||||||||||||||||||||||
"\"Collections.EMPTY_LIST\", \"EMPTY_MAP\", and \"EMPTY_SET\" should not be used") | ||||||||||||||||||||||||||||||
public class CollectionsEmptyConstantsProcessor extends SoraldAbstractProcessor<CtFieldAccess<?>> { | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||
protected void repairInternal(CtFieldAccess<?> element) { | ||||||||||||||||||||||||||||||
CtType<?> collectionsType = getFactory().Class().get(Collections.class); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
CtMethod<?> methodToBeCalled; | ||||||||||||||||||||||||||||||
if (element.getVariable().getSimpleName().equals("EMPTY_LIST")) { | ||||||||||||||||||||||||||||||
methodToBeCalled = collectionsType.getMethodsByName("emptyList").get(0); | ||||||||||||||||||||||||||||||
} else if (element.getVariable().getSimpleName().equals("EMPTY_MAP")) { | ||||||||||||||||||||||||||||||
methodToBeCalled = collectionsType.getMethodsByName("emptyMap").get(0); | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
methodToBeCalled = collectionsType.getMethodsByName("emptySet").get(0); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about simplifying this a bit? The pattern is "camel case the name and make method invocation".
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a great suggestion! Applied! |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
CtInvocation<?> newInvocation = | ||||||||||||||||||||||||||||||
getFactory().createInvocation(element.getTarget(), methodToBeCalled.getReference()); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
element.replace(newInvocation); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class CollectionsEmptyConstants { | ||
public static void main(String[] args) { | ||
List<String> collection1 = Collections.EMPTY_LIST; // Noncompliant | ||
Map<String, String> collection2 = Collections.EMPTY_MAP; // Noncompliant | ||
Set<String> collection3 = Collections.EMPTY_SET; // Noncompliant | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class CollectionsEmptyConstants { | ||
public static void main(String[] args) { | ||
List<String> collection1 = Collections.emptyList(); | ||
Map<String, String> collection2 = Collections.emptyMap(); | ||
Set<String> collection3 = Collections.emptySet(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a side note, this violates the rule you implemented a processor for in #562 :)
(sorald-buildbreaker does not catch it because it uses Sorald 0.3.0, which doesn't have that processor)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:X