-
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 FinalStaticMethod analyzer (#845)
- Loading branch information
1 parent
c10c310
commit c4ea0e8
Showing
6 changed files
with
98 additions
and
0 deletions.
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
.../io/github/martinwitt/spoon_analyzer/badsmells/final_static_method/FinalStaticMethod.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.final_static_method; | ||
|
||
import io.github.martinwitt.spoon_analyzer.BadSmell; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.declaration.CtType; | ||
|
||
public class FinalStaticMethod implements BadSmell { | ||
|
||
private static final String NAME = "FinalStaticMethod"; | ||
private static final String description = | ||
"A final method is a method that cannot be overridden in a subclass. As static methods are bound to the class the cant be overridden only hidden."; | ||
private final CtMethod<?> method; | ||
private final CtType<?> affectedType; | ||
|
||
public FinalStaticMethod(CtMethod<?> method, CtType<?> affectedType) { | ||
this.method = method; | ||
this.affectedType = affectedType; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public CtType<?> getAffectedType() { | ||
return affectedType; | ||
} | ||
|
||
/** | ||
* @return the final static method | ||
*/ | ||
public CtMethod<?> getMethod() { | ||
return method; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ub/martinwitt/spoon_analyzer/badsmells/final_static_method/FinalStaticMethodAnalyzer.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,34 @@ | ||
package io.github.martinwitt.spoon_analyzer.badsmells.final_static_method; | ||
|
||
import io.github.martinwitt.laughing_train.spoonutils.matcher.Matchers; | ||
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.declaration.CtMethod; | ||
import spoon.reflect.declaration.CtType; | ||
import spoon.reflect.visitor.Filter; | ||
|
||
public class FinalStaticMethodAnalyzer implements LocalAnalyzer { | ||
|
||
@Override | ||
public List<BadSmell> analyze(CtType<?> clazz) { | ||
List<BadSmell> badSmells = new ArrayList<>(); | ||
List<CtMethod<?>> elements = clazz.getElements(new FinalStaticMethodFilter()); | ||
for (CtMethod<?> method : elements) { | ||
badSmells.add(new FinalStaticMethod(method, clazz)); | ||
} | ||
return badSmells; | ||
} | ||
|
||
/** | ||
* A filter that matches final static methods. | ||
*/ | ||
private static class FinalStaticMethodFilter implements Filter<CtMethod<?>> { | ||
|
||
@Override | ||
public boolean matches(CtMethod<?> element) { | ||
return Matchers.allOf(Matchers.isFinal(), (Matchers.isStatic())).matches(element); | ||
} | ||
} | ||
} |