15
15
*/
16
16
package org .springframework .sbm .java .refactoring ;
17
17
18
- import org .springframework .sbm .project .resource .ProjectResourceSet ;
19
- import org .springframework .sbm .project .resource .RewriteSourceFileHolder ;
20
- import org .springframework .sbm .support .openrewrite .GenericOpenRewriteRecipe ;
21
18
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 ;
26
21
import org .openrewrite .java .JavaVisitor ;
27
22
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 ;
28
30
29
31
import java .util .Arrays ;
30
32
import java .util .List ;
@@ -81,7 +83,8 @@ public void refactor(Recipe... recipes) {
81
83
82
84
@ Override
83
85
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 ;
85
88
}
86
89
87
90
@ NotNull
@@ -94,11 +97,33 @@ protected List<RewriteSourceFileHolder<J.CompilationUnit>> findInternal(List<Rew
94
97
.map (J .CompilationUnit .class ::cast )
95
98
.map (cu -> resourceWrappers .stream ()
96
99
.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
+ })
98
105
.findAny ().orElseThrow ())
99
106
.collect (Collectors .toList ());
100
107
}
101
108
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
+
102
127
@ Deprecated
103
128
void processResults (List <J .CompilationUnit > compilationUnits , List <Result > changes ) {
104
129
if (!changes .isEmpty ()) {
0 commit comments