-
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: add UnnecessaryModifier rule (#219)
- Loading branch information
1 parent
c3fdd5d
commit 165bbde
Showing
2 changed files
with
69 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
66 changes: 66 additions & 0 deletions
66
...c/main/java/xyz/keksdose/spoon/code_solver/analyzer/qodana/rules/UnnecessaryModifier.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,66 @@ | ||
package xyz.keksdose.spoon.code_solver.analyzer.qodana.rules; | ||
|
||
import java.nio.file.Path; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.declaration.CtModifiable; | ||
import spoon.reflect.declaration.CtType; | ||
import xyz.keksdose.spoon.code_solver.analyzer.PositionScanner; | ||
import xyz.keksdose.spoon.code_solver.api.analyzer.AnalyzerResult; | ||
import xyz.keksdose.spoon.code_solver.history.Change; | ||
import xyz.keksdose.spoon.code_solver.history.ChangeListener; | ||
import xyz.keksdose.spoon.code_solver.history.MarkdownString; | ||
import xyz.keksdose.spoon.code_solver.transformations.BadSmell; | ||
|
||
public class UnnecessaryModifier extends AbstractRefactoring { | ||
|
||
public UnnecessaryModifier(AnalyzerResult result) { | ||
super(result); | ||
} | ||
|
||
private static final BadSmell UNNECESSARY_MODIFIER = new BadSmell() { | ||
@Override | ||
public MarkdownString getName() { | ||
return MarkdownString.fromRaw("Unnecessary-Modifier"); | ||
} | ||
|
||
@Override | ||
public MarkdownString getDescription() { | ||
return MarkdownString.fromRaw( | ||
"Some modifiers are not needed, because they are already the default and implicit. These modifiers can be removed."); | ||
} | ||
}; | ||
|
||
@Override | ||
public void refactor(ChangeListener listener, CtType<?> type) { | ||
if (type.isAnonymous() || !isSameType(type, Path.of(result.filePath()))) { | ||
return; | ||
} | ||
String modifier = result.messageMarkdown().split("`")[1]; | ||
if (modifier == null || modifier.isEmpty()) { | ||
return; | ||
} | ||
for (CtElement match : PositionScanner.findLineOnly(type, result.position())) { | ||
if (match instanceof CtModifiable ctModifierHandler) { | ||
var modifiers = new HashSet<>(ctModifierHandler.getModifiers()); | ||
modifiers.removeIf(v -> v.toString().equals(modifier.toLowerCase())); | ||
ctModifierHandler.setModifiers(modifiers); | ||
listener.setChanged( | ||
type.getTopLevelType(), | ||
new Change( | ||
UNNECESSARY_MODIFIER, | ||
MarkdownString.fromMarkdown( | ||
"Unnecessary modifier " + modifier + " removed", | ||
"Unnecessary modifier `" + modifier + "` removed"), | ||
type.getTopLevelType(), | ||
result)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<BadSmell> getHandledBadSmells() { | ||
return List.of(UNNECESSARY_MODIFIER); | ||
} | ||
} |