Skip to content

Commit 3867d19

Browse files
authored
fix: When searching CompilationUnit gets replaced and thus Markers are kept (#809)
- Ignore markers when printing - SearchResult and RecipeThatMadeChanges marker are removed
1 parent ca4be5e commit 3867d19

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/java/impl/OpenRewriteJavaSource.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Arrays;
3434
import java.util.List;
3535
import java.util.Set;
36+
import java.util.function.UnaryOperator;
3637
import java.util.stream.Collectors;
3738

3839
public class OpenRewriteJavaSource extends RewriteSourceFileHolder<J.CompilationUnit> implements JavaSource {
@@ -204,7 +205,24 @@ public <M extends Marker> M visitMarker(Marker marker, PrintOutputCapture<Execut
204205
}
205206
};
206207

207-
PrintOutputCapture<Integer> outputCapture = new PrintOutputCapture(executionContext);
208+
209+
// Don't print markers here, now that markers are kept in the underlying SourceFiles
210+
PrintOutputCapture<Integer> outputCapture = new PrintOutputCapture(executionContext, new PrintOutputCapture.MarkerPrinter() {
211+
@Override
212+
public String beforePrefix(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
213+
return "";
214+
}
215+
216+
@Override
217+
public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
218+
return "";
219+
}
220+
221+
@Override
222+
public String afterSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
223+
return "";
224+
}
225+
});
208226
((JavaPrinter) javaPrinter).visit(getSourceFile(), outputCapture);
209227

210228
return outputCapture.out.toString();

components/sbm-core/src/main/java/org/springframework/sbm/java/refactoring/JavaGlobalRefactoringImpl.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
*/
1616
package org.springframework.sbm.java.refactoring;
1717

18-
import org.springframework.sbm.project.resource.ProjectResourceSet;
19-
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
20-
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;
2118
import org.jetbrains.annotations.NotNull;
22-
import org.openrewrite.ExecutionContext;
23-
import org.openrewrite.Recipe;
24-
import org.openrewrite.Result;
25-
import org.openrewrite.SourceFile;
19+
import org.openrewrite.*;
20+
import org.openrewrite.java.JavaIsoVisitor;
2621
import org.openrewrite.java.JavaVisitor;
2722
import org.openrewrite.java.tree.J;
23+
import org.openrewrite.marker.Marker;
24+
import org.openrewrite.marker.Markers;
25+
import org.openrewrite.marker.RecipesThatMadeChanges;
26+
import org.openrewrite.marker.SearchResult;
27+
import org.springframework.sbm.project.resource.ProjectResourceSet;
28+
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
29+
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe;
2830

2931
import java.util.Arrays;
3032
import java.util.List;
@@ -81,7 +83,8 @@ public void refactor(Recipe... recipes) {
8183

8284
@Override
8385
public List<RewriteSourceFileHolder<J.CompilationUnit>> find(Recipe recipe) {
84-
return findInternal(getAllCompilationUnits(), recipe);
86+
List<RewriteSourceFileHolder<J.CompilationUnit>> matches = findInternal(getAllCompilationUnits(), recipe);
87+
return matches;
8588
}
8689

8790
@NotNull
@@ -94,11 +97,33 @@ protected List<RewriteSourceFileHolder<J.CompilationUnit>> findInternal(List<Rew
9497
.map(J.CompilationUnit.class::cast)
9598
.map(cu -> resourceWrappers.stream()
9699
.filter(fh -> fh.getId().equals(cu.getId()))
97-
.map(pr -> (RewriteSourceFileHolder<J.CompilationUnit>) pr)
100+
.map(pr -> {
101+
J.CompilationUnit cuRemovedMarkers = removeMarkers(cu, SearchResult.class, RecipesThatMadeChanges.class);
102+
pr.replaceWith(cuRemovedMarkers);
103+
return pr;
104+
})
98105
.findAny().orElseThrow())
99106
.collect(Collectors.toList());
100107
}
101108

109+
private J.CompilationUnit removeMarkers(J.CompilationUnit cu, Class<? extends Marker>... markerTypes) {
110+
RecipeRun recipeRun = new GenericOpenRewriteRecipe<>(() -> new JavaIsoVisitor<ExecutionContext>() {
111+
@Override
112+
public Markers visitMarkers(Markers m, ExecutionContext executionContext) {
113+
Markers markers = super.visitMarkers(m, executionContext);
114+
if (!markers.getMarkers().isEmpty()) {
115+
for (Class<? extends Marker> marker : markerTypes) {
116+
markers = markers.removeByType(marker);
117+
}
118+
}
119+
return markers;
120+
}
121+
}).run(List.of(cu), executionContext);
122+
J.CompilationUnit compilationUnit = (J.CompilationUnit) recipeRun.getResults().get(0).getAfter();
123+
compilationUnit = compilationUnit.withMarkers(compilationUnit.getMarkers().removeByType(RecipesThatMadeChanges.class));
124+
return compilationUnit;
125+
}
126+
102127
@Deprecated
103128
void processResults(List<J.CompilationUnit> compilationUnits, List<Result> changes) {
104129
if (!changes.isEmpty()) {

0 commit comments

Comments
 (0)