-
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): ✨ Provide a unified api for refactoring a repository wit…
…h code transformations api (#1011)
- Loading branch information
1 parent
445acdf
commit 5357c79
Showing
30 changed files
with
157 additions
and
68 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...zer/qodana/rules/AbstractRefactoring.java → ..._solver/analyzer/AbstractRefactoring.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
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
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
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
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
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
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
39 changes: 39 additions & 0 deletions
39
...sformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/SpoonRefactor.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,39 @@ | ||
package xyz.keksdose.spoon.code_solver.analyzer.spoon; | ||
|
||
import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult; | ||
import io.github.martinwitt.laughing_train.domain.value.RuleId; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import spoon.reflect.declaration.CtType; | ||
import xyz.keksdose.spoon.code_solver.analyzer.AbstractRefactoring; | ||
import xyz.keksdose.spoon.code_solver.history.ChangeListener; | ||
import xyz.keksdose.spoon.code_solver.transformations.TransformationProcessor; | ||
|
||
/** | ||
* Entry point for spoon based refactoring. This class is used to apply all refactors which are reported by spoon. | ||
*/ | ||
public class SpoonRefactor extends TransformationProcessor<CtType<?>> { | ||
|
||
private Map<RuleId, Function<AnalyzerResult, AbstractRefactoring>> ruleParser; | ||
private List<AbstractRefactoring> refactors; | ||
|
||
public SpoonRefactor(ChangeListener changeListener, List<? extends AnalyzerResult> badSmells) { | ||
super(changeListener); | ||
ruleParser = new HashMap<>(); | ||
Arrays.stream(SpoonRules.values()).forEach(rule -> ruleParser.put(rule.getRuleId(), rule.getRefactoring())); | ||
for (AnalyzerResult result : badSmells) { | ||
Optional.ofNullable(ruleParser.get(result.ruleID())).ifPresent(v -> refactors.add(v.apply(result))); | ||
} | ||
} | ||
|
||
@Override | ||
public void process(CtType<?> element) { | ||
for (AbstractRefactoring refactoring : refactors) { | ||
refactoring.refactor(listener, element); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ransformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/SpoonRules.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,38 @@ | ||
package xyz.keksdose.spoon.code_solver.analyzer.spoon; | ||
|
||
import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult; | ||
import io.github.martinwitt.laughing_train.domain.value.RuleId; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import xyz.keksdose.spoon.code_solver.analyzer.AbstractRefactoring; | ||
import xyz.keksdose.spoon.code_solver.analyzer.AnalyzerRule; | ||
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.MethodMayBeStatic; | ||
import xyz.keksdose.spoon.code_solver.transformations.BadSmell; | ||
|
||
/** | ||
* Enum for all spoon based rules. | ||
*/ | ||
public enum SpoonRules implements AnalyzerRule { | ||
METHOD_MAY_BE_STATIC("MethodMayBeStatic", MethodMayBeStatic::new); | ||
|
||
private final RuleId ruleId; | ||
private final Function<AnalyzerResult, AbstractRefactoring> refactoring; | ||
|
||
SpoonRules(String ruleId, Function<AnalyzerResult, AbstractRefactoring> refactoring) { | ||
this.ruleId = new RuleId(ruleId); | ||
this.refactoring = refactoring; | ||
} | ||
|
||
@Override | ||
public RuleId getRuleId() { | ||
return ruleId; | ||
} | ||
|
||
Function<AnalyzerResult, AbstractRefactoring> getRefactoring() { | ||
return refactoring; | ||
} | ||
|
||
List<BadSmell> getDescription() { | ||
return getRefactoring().apply(null).getHandledBadSmells(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/api/CodeRefactoring.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,50 @@ | ||
package xyz.keksdose.spoon.code_solver.api; | ||
|
||
import com.google.common.flogger.FluentLogger; | ||
import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import xyz.keksdose.spoon.code_solver.TransformationEngine; | ||
import xyz.keksdose.spoon.code_solver.analyzer.qodana.QodanaRefactor; | ||
import xyz.keksdose.spoon.code_solver.analyzer.qodana.QodanaRules; | ||
import xyz.keksdose.spoon.code_solver.analyzer.spoon.SpoonRefactor; | ||
import xyz.keksdose.spoon.code_solver.diffs.DiffCleaner; | ||
import xyz.keksdose.spoon.code_solver.history.ChangeListener; | ||
import xyz.keksdose.spoon.code_solver.history.Changelog; | ||
import xyz.keksdose.spoon.code_solver.transformations.TransformationProcessor; | ||
|
||
/** | ||
* This class is the entry point for the code transformation. It takes a repository and a list of badsmells. It then applies the transformations to the repository and returns a changelog. | ||
*/ | ||
public class CodeRefactoring { | ||
|
||
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); | ||
|
||
private final List<BiFunction<ChangeListener, List<? extends AnalyzerResult>, TransformationProcessor<?>>> | ||
refactor = new ArrayList<>(); | ||
|
||
public CodeRefactoring() { | ||
refactor.add((u, v) -> new QodanaRefactor(EnumSet.allOf(QodanaRules.class), u, v)); | ||
refactor.add((u, v) -> new SpoonRefactor(u, v)); | ||
} | ||
|
||
public Changelog refactorBadSmells(Path repository, List<? extends AnalyzerResult> results) { | ||
ChangeListener listener = new ChangeListener(); | ||
logger.atInfo().log("Refactoring %s", repository); | ||
DiffCleaner diffCleaner = new DiffCleaner(); | ||
List<Function<ChangeListener, TransformationProcessor<?>>> function = new ArrayList<>(); | ||
for (BiFunction<ChangeListener, List<? extends AnalyzerResult>, TransformationProcessor<?>> refactorSupplier : | ||
refactor) { | ||
function.add(v -> refactorSupplier.apply(v, results)); | ||
} | ||
TransformationEngine transformationEngine = new TransformationEngine(function); | ||
transformationEngine.setChangeListener(listener); | ||
Changelog log = transformationEngine.applyToGivenPath(repository.toString()); | ||
log.getChanges().forEach(change -> diffCleaner.clean(repository, change)); | ||
return log; | ||
} | ||
} |
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
Oops, something went wrong.