-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spoon): Add implicit array to string rule (#910)
- Loading branch information
1 parent
c2ca653
commit 56aa480
Showing
7 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...b/martinwitt/spoon_analyzer/badsmells/implicit_array_to_string/ImplicitArrayToString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.implicit_array_to_string; | ||
|
||
import io.github.martinwitt.spoon_analyzer.BadSmell; | ||
import io.github.martinwitt.spoon_analyzer.BadSmellVisitor; | ||
import spoon.reflect.code.CtInvocation; | ||
import spoon.reflect.declaration.CtType; | ||
|
||
public class ImplicitArrayToString implements BadSmell { | ||
|
||
private final CtType<?> clazz; | ||
private final CtInvocation<?> implicitToStringCaller; | ||
|
||
public ImplicitArrayToString(CtType<?> clazz, CtInvocation<?> implicitToStringCaller) { | ||
this.clazz = clazz; | ||
this.implicitToStringCaller = implicitToStringCaller; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "ImplicitArrayToString"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Calling toString() on an array returns not the content but the toString from the array object itself."; | ||
} | ||
|
||
@Override | ||
public CtType<?> getAffectedType() { | ||
return clazz; | ||
} | ||
|
||
public CtInvocation<?> getImplicitToStringCaller() { | ||
return implicitToStringCaller; | ||
} | ||
|
||
@Override | ||
public <T> T accept(BadSmellVisitor<T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...witt/spoon_analyzer/badsmells/implicit_array_to_string/ImplicitArrayToStringAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.implicit_array_to_string; | ||
|
||
import io.github.martinwitt.laughing_train.spoonutils.ImplicitToStringMatcher; | ||
import io.github.martinwitt.spoon_analyzer.BadSmell; | ||
import io.github.martinwitt.spoon_analyzer.LocalAnalyzer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import spoon.reflect.code.CtInvocation; | ||
import spoon.reflect.declaration.CtType; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
|
||
public class ImplicitArrayToStringAnalyzer implements LocalAnalyzer { | ||
|
||
@Override | ||
public List<BadSmell> analyze(CtType<?> clazz) { | ||
List<BadSmell> badSmells = new ArrayList<BadSmell>(); | ||
ImplicitToStringMatcher matcher = new ImplicitToStringMatcher(); | ||
List<CtInvocation<?>> invocations = clazz.getElements(new TypeFilter<>(CtInvocation.class)); | ||
for (CtInvocation<?> invocation : invocations) { | ||
if (matcher.matches(invocation)) { | ||
if (invocation.getArguments().stream() | ||
.anyMatch(v -> v.getType() != null && v.getType().isArray())) { | ||
badSmells.add(new ImplicitArrayToString(clazz, invocation)); | ||
} | ||
} | ||
} | ||
return badSmells; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../spoon_analyzer/badsmells/implicit_array_to_string/ImplicitArrayToStringAnalyzerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.implicit_array_to_string; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import spoon.Launcher; | ||
import spoon.reflect.declaration.CtType; | ||
import spoon.support.compiler.VirtualFile; | ||
|
||
public class ImplicitArrayToStringAnalyzerTest { | ||
|
||
@Test | ||
void UpperClassEqualsNoHashcode() { | ||
String code = | ||
""" | ||
class A { | ||
public void print(Object[] obj) { | ||
System.out.println(obj); | ||
} | ||
} | ||
"""; | ||
|
||
Launcher launcher = new Launcher(); | ||
launcher.addInputResource(new VirtualFile(code)); | ||
var model = launcher.buildModel(); | ||
ImplicitArrayToStringAnalyzer analyzer = new ImplicitArrayToStringAnalyzer(); | ||
CtType<?> simpleClass = model.getAllTypes().stream().findFirst().get(); | ||
var result = analyzer.analyze(simpleClass); | ||
|
||
assertEquals(1, result.size()); | ||
} | ||
} |