18
18
import lombok .Getter ;
19
19
import lombok .extern .slf4j .Slf4j ;
20
20
import org .openrewrite .ExecutionContext ;
21
- import org .openrewrite .Parser ;
22
- import org .openrewrite .SourceFile ;
23
21
import org .openrewrite .internal .lang .Nullable ;
24
22
import org .openrewrite .java .JavaIsoVisitor ;
25
23
import org .openrewrite .java .JavaParser ;
28
26
import org .openrewrite .java .marker .JavaSourceSet ;
29
27
import org .openrewrite .java .tree .J ;
30
28
import org .openrewrite .java .tree .J .VariableDeclarations .NamedVariable ;
31
- import org .openrewrite .java .tree .JavaSourceFile ;
32
29
import org .openrewrite .java .tree .JavaType ;
33
30
import org .openrewrite .java .tree .TypeTree ;
34
31
import org .openrewrite .marker .Markers ;
35
- import org .springframework .rewrite .parsers .RewriteExecutionContext ;
32
+ import org .springframework .rewrite .parsers .JavaParserBuilder ;
36
33
import org .springframework .rewrite .parsers .maven .ClasspathDependencies ;
37
34
import org .springframework .rewrite .project .resource .RewriteSourceFileHolder ;
38
35
import org .springframework .rewrite .support .openrewrite .GenericOpenRewriteRecipe ;
39
36
import org .springframework .sbm .java .api .Annotation ;
40
- import org .springframework .sbm .java .api .JavaSource ;
41
37
import org .springframework .sbm .java .api .Member ;
42
- import org .springframework .sbm .java .api .Type ;
43
38
import org .springframework .sbm .java .refactoring .JavaRefactoring ;
44
- import org .springframework .rewrite .parsers .JavaParserBuilder ;
45
- import org .springframework .sbm .support .openrewrite .java .AddAnnotationVisitor ;
46
39
import org .springframework .sbm .support .openrewrite .java .RemoveAnnotationVisitor ;
47
40
48
41
import java .io .ByteArrayInputStream ;
49
- import java .util .ArrayList ;
50
- import java .util .Comparator ;
51
- import java .util .List ;
52
- import java .util .UUID ;
42
+ import java .nio .file .Path ;
43
+ import java .util .*;
53
44
import java .util .stream .Collectors ;
45
+ import java .util .stream .Stream ;
54
46
55
47
@ Getter
56
48
@ Slf4j
@@ -67,6 +59,7 @@ public class OpenRewriteMember implements Member {
67
59
private final JavaRefactoring refactoring ;
68
60
private final JavaParserBuilder javaParserBuilder ;
69
61
62
+
70
63
public OpenRewriteMember (
71
64
J .VariableDeclarations variableDecls , NamedVariable namedVar ,
72
65
RewriteSourceFileHolder <J .CompilationUnit > rewriteSourceFileHolder , JavaRefactoring refactoring , JavaParserBuilder javaParser ) {
@@ -151,8 +144,86 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
151
144
152
145
@ Override
153
146
public void addAnnotation (String snippet , String annotationImport , String ... otherImports ) {
154
- AddAnnotationVisitor visitor = new AddAnnotationVisitor (() -> javaParserBuilder , getVariableDeclarations (), snippet , annotationImport , otherImports );
155
- refactoring .refactor (rewriteSourceFileHolder , visitor );
147
+ GenericOpenRewriteRecipe <JavaIsoVisitor <ExecutionContext >> recipe = new GenericOpenRewriteRecipe <>(() -> new JavaIsoVisitor <>() {
148
+
149
+ // private JavaSourceSet main;
150
+ // private List<Path> classpath;
151
+ // private Set<String> imports;
152
+ // private List<JavaType.FullyQualified> newCLasspathFqn;
153
+ // private JavaTypeCache typeCache;
154
+ // private boolean visitCu = false;
155
+
156
+ // @Override
157
+ // public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit, ExecutionContext executionContext) {
158
+ // J.CompilationUnit cu = super.visitCompilationUnit(compilationUnit, executionContext);
159
+ // if(visitCu) {
160
+ // // TODO: De ganze Zirkus hier nochmal
161
+ // JavaSourceSet javaSourceSet = cu.getMarkers().findFirst(JavaSourceSet.class).get();
162
+ // Field field = ReflectionUtils.findField(JavaSourceSet.class, "classpath");
163
+ // ReflectionUtils.makeAccessible(field);
164
+ // ReflectionUtils.setField(field, javaSourceSet, newCLasspathFqn);
165
+ // imports.forEach(i -> maybeAddImport(i));
166
+ // visitCu = false;
167
+ // final J.CompilationUnit tmpCu = cu;
168
+ // J.CompilationUnit cu1 = (J.CompilationUnit) JavaParser.fromJavaVersion().typeCache(typeCache).classpath(classpath).clone().build().parseInputs(List.of(new JavaParser.Input(cu.getSourcePath(), () -> new ByteArrayInputStream(tmpCu.printAll().getBytes()))), null, executionContext).toList().get(0);
169
+ // cu = cu1.withMarkers(Markers.build(List.of(main)));
170
+ // }
171
+ // return cu;
172
+ // }
173
+
174
+ @ Override
175
+ public J .VariableDeclarations visitVariableDeclarations (J .VariableDeclarations multiVariable , ExecutionContext executionContext ) {
176
+ J .VariableDeclarations vd = super .visitVariableDeclarations (multiVariable , executionContext );
177
+ if (vd == getVariableDeclarations ()) {
178
+ J .CompilationUnit cu = getCursor ().dropParentUntil (J .CompilationUnit .class ::isInstance ).getValue ();
179
+ List <Path > classpathJars = cu .getMarkers ().findFirst (ClasspathDependencies .class ).get ().getDependencies ();
180
+ // JavaSourceSet javaSourceSet = cu.getMarkers().findFirst(JavaSourceSet.class).get();
181
+ // JavaTypeCache typeCache = new JavaTypeCache();
182
+
183
+ Set <String > imports = Stream .concat (Stream .of (annotationImport ), Stream .of (annotationImport )).collect (Collectors .toSet ());
184
+ JavaTypeCache typeCache = new JavaTypeCache ();
185
+
186
+
187
+
188
+ JavaParser .Builder <? extends JavaParser , ?> javaParser = JavaParser .fromJavaVersion ().typeCache (typeCache .clone ()).classpath (classpathJars );
189
+
190
+ vd = JavaTemplate .builder (snippet )
191
+ .imports (new ArrayList <>(imports ).toArray (String []::new ))
192
+ .javaParser (javaParser )
193
+ .build ()
194
+ .apply (getCursor (), vd .getCoordinates ().addAnnotation (Comparator .comparing (J .Annotation ::getSimpleName )));
195
+ imports .forEach (i -> maybeAddImport (i , false ));
196
+
197
+ // final J.CompilationUnit tmpCu = cu;
198
+ // J.CompilationUnit cu1 = (J.CompilationUnit) JavaParser.fromJavaVersion().typeCache(typeCache).classpath(classpath).build().parseInputs(List.of(new JavaParser.Input(cu.getSourcePath(), () -> new ByteArrayInputStream(tmpCu.printAll().getBytes()))), null, executionContext).toList().get(0);
199
+
200
+ /*
201
+ visitCu = false;
202
+ final J.CompilationUnit tmpCu = cu;
203
+ J.CompilationUnit cu1 = (J.CompilationUnit) JavaParser.fromJavaVersion().typeCache(typeCache).classpath(classpath).build().parseInputs(List.of(new JavaParser.Input(cu.getSourcePath(), () -> new ByteArrayInputStream(tmpCu.printAll().getBytes()))), null, executionContext).toList().get(0);
204
+ JavaSourceSet newJavaSourceSet = JavaSourceSet.build(javaSourceSet.getName(), cu.getMarkers().findFirst(ClasspathDependencies.class).get().getDependencies(), typeCache, true);
205
+ cu = cu1.withMarkers(cu1.getMarkers().removeByType(JavaSourceSet.class).addIfAbsent(newJavaSourceSet));
206
+ // cu = cu1.withMarkers(Markers.build(List.of(main)));
207
+
208
+
209
+ // J.CompilationUnit cu = getRewriteSourceFileHolder().getSourceFile();
210
+ // classpath = cu.getMarkers().findFirst(ClasspathDependencies.class).get().getDependencies();
211
+ imports = Stream.concat(Stream.of(annotationImport), Stream.of(annotationImport)).collect(Collectors.toSet());
212
+ // // FIXME: main is not always correct
213
+ // typeCache = new JavaTypeCache();
214
+ // main = JavaSourceSet.build("main", classpath, typeCache, true);
215
+
216
+ newCLasspathFqn = main.getClasspath();
217
+
218
+ visitCu = true;
219
+ imports.forEach(i -> maybeAddImport(i, false));
220
+
221
+ */
222
+ }
223
+ return vd ;
224
+ }
225
+ });
226
+ refactoring .refactor (rewriteSourceFileHolder , recipe );
156
227
}
157
228
158
229
@ Override
0 commit comments