@@ -116,10 +116,10 @@ public J.Annotation visitAnnotation(J.Annotation original, ExecutionContext ctx)
116116 }
117117 if ("value" .equals (attributeName ())) {
118118 String attrVal = newAttributeValue .contains ("," ) && attributeIsArray (a ) ? getAttributeValuesAsArray (a ) : newAttributeValue ;
119- return JavaTemplate . apply ("#{}" , getCursor (), a .getCoordinates ().replaceArguments (), attrVal );
119+ return getJavaTemplate ("#{}" ). apply ( getCursor (), a .getCoordinates ().replaceArguments (), attrVal );
120120 }
121121 String attrVal = newAttributeValue .contains ("," ) && attributeIsArray (a ) ? getAttributeValuesAsString (a ) : newAttributeValue ;
122- return JavaTemplate . apply ("#{} = #{}" , getCursor (), a .getCoordinates ().replaceArguments (), attributeName , attrVal );
122+ return getJavaTemplate ("#{} = #{}" ). apply ( getCursor (), a .getCoordinates ().replaceArguments (), attributeName , attrVal );
123123 }
124124
125125 // UPDATE the value when the annotation has arguments, e.g. @Foo(name="old") to `@Foo(name="new")
@@ -199,7 +199,7 @@ public J.Annotation visitAnnotation(J.Annotation original, ExecutionContext ctx)
199199 return as .withAssignment (createNewArrayWithExistingAndNew (annotation , (J .FieldAccess ) exp , getAttributeValues (annotation )));
200200 }
201201 //noinspection ConstantConditions
202- return JavaTemplate .< J . Annotation > apply ("#{} = #{}" , getCursor (), as .getCoordinates ().replace (), var_ .getSimpleName (), newAttributeValue )
202+ return getJavaTemplate ("#{} = #{}" ).< J . Annotation > apply ( getCursor (), as .getCoordinates ().replace (), var_ .getSimpleName (), newAttributeValue )
203203 .getArguments ().get (annotation .getArguments ().indexOf (as ));
204204 }
205205 return as ;
@@ -248,13 +248,24 @@ public J.Annotation visitAnnotation(J.Annotation original, ExecutionContext ctx)
248248 getAttributeValues (annotation ).stream ().map (String ::valueOf ).collect (joining ("," , "{" , "}" )) :
249249 newAttributeValue ;
250250 //noinspection ConstantConditions
251- return JavaTemplate .<J .Annotation >apply ("#{}" , getCursor (), annotation .getCoordinates ().replaceArguments (), attrVal )
251+ return getJavaTemplate ( "#{}" ) .<J .Annotation >apply (getCursor (), annotation .getCoordinates ().replaceArguments (), attrVal )
252252 .getArguments ().get (0 );
253253 }
254254 // Make the attribute name explicit, before we add the new value below
255255 return createAnnotationAssignment (annotation , "value" , fieldAccess );
256256 }
257257
258+ private JavaTemplate getJavaTemplate (String template ) {
259+ JavaTemplate .Builder builder = JavaTemplate .builder (template );
260+ if (isFullyQualifiedClass ()) {
261+ JavaType .ShallowClass fqn = JavaType .ShallowClass .build (attributeValue .substring (0 , attributeValue .length () - 6 ));
262+ builder
263+ .javaParser (JavaParser .fromJavaVersion ().dependsOn (String .format ("package %s;\n public interface %s {}\n " , fqn .getPackageName (), fqn .getClassName ())))
264+ .imports (fqn .getFullyQualifiedName ());
265+ }
266+ return builder .build ();
267+ }
268+
258269 private @ Nullable Expression update (J .NewArray arrayValue , J .Annotation annotation , @ Nullable String newAttributeValue ) {
259270 if (newAttributeValue == null ) {
260271 return null ;
@@ -268,13 +279,13 @@ public J.Annotation visitAnnotation(J.Annotation original, ExecutionContext ctx)
268279 private Expression createAnnotationLiteral (J .Annotation annotation , String newAttributeValue ) {
269280 String attrVal = newAttributeValue .contains ("," ) && attributeIsArray (annotation ) ? getAttributeValuesAsString (annotation ) : newAttributeValue ;
270281 //noinspection ConstantConditions
271- return JavaTemplate .<J .Annotation >apply ("#{}" , getCursor (), annotation .getCoordinates ().replaceArguments (), attrVal )
282+ return getJavaTemplate ( "#{}" ) .<J .Annotation >apply (getCursor (), annotation .getCoordinates ().replaceArguments (), attrVal )
272283 .getArguments ().get (0 );
273284 }
274285
275286 private J .Assignment createAnnotationAssignment (J .Annotation annotation , String name , @ Nullable Object parameter ) {
276287 //noinspection ConstantConditions
277- return (J .Assignment ) JavaTemplate .< J . Annotation > apply (name + " = " + (parameter instanceof J ? "#{any()}" : "#{}" ), getCursor (), annotation .getCoordinates ().replaceArguments (), parameter )
288+ return (J .Assignment ) getJavaTemplate (name + " = " + (parameter instanceof J ? "#{any()}" : "#{}" )).< J . Annotation > apply ( getCursor (), annotation .getCoordinates ().replaceArguments (), parameter )
278289 .getArguments ().get (0 );
279290 }
280291
@@ -290,7 +301,7 @@ private J.NewArray createNewArrayWithExistingAndNew(J.Annotation annotation, J.L
290301 }
291302 // Use a template to create the array structure, then replace the initializer
292303 //noinspection ConstantConditions
293- J .NewArray template = (J .NewArray ) JavaTemplate .< J . Annotation > apply ("{#{any()}}" , getCursor (), annotation .getCoordinates ().replaceArguments (), existingLiteral )
304+ J .NewArray template = (J .NewArray ) getJavaTemplate ("{#{any()}}" ).< J . Annotation > apply ( getCursor (), annotation .getCoordinates ().replaceArguments (), existingLiteral )
294305 .getArguments ().get (0 );
295306 return template .withInitializer (initializer );
296307 }
@@ -302,19 +313,13 @@ private J.NewArray createNewArrayWithExistingAndNew(J.Annotation annotation, J.F
302313 // Add new values, skipping duplicates - use template for non-string values
303314 for (String attribute : newValues ) {
304315 if (!attributeNameOrValIsAlreadyPresent (initializer , singleton (attribute ))) {
305- JavaTemplate .Builder templateBuilder = JavaTemplate .builder ("#{}" );
306316 String templateValue = attribute ;
307317 // For FQ classes, add stub so type info is resolved and use FQ name in template
308318 if (isFullyQualifiedClass ()) {
309- String fqClassName = attributeValue .substring (0 , attributeValue .length () - 6 );
310- String packageName = fqClassName .substring (0 , fqClassName .lastIndexOf ('.' ));
311- String simpleName = fqClassName .substring (fqClassName .lastIndexOf ('.' ) + 1 );
312- templateBuilder = templateBuilder .javaParser (JavaParser .fromJavaVersion ()
313- .dependsOn ("package " + packageName + "; public class " + simpleName + " {}" ));
314- templateValue = fqClassName + ".class" ;
319+ templateValue = getFullyQualifiedClass (attributeValue );
315320 }
316321 //noinspection ConstantConditions
317- Expression newExpr = templateBuilder . build ( ).<J .Annotation >apply (getCursor (), annotation .getCoordinates ().replaceArguments (), templateValue )
322+ Expression newExpr = getJavaTemplate ( "#{}" ).<J .Annotation >apply (getCursor (), annotation .getCoordinates ().replaceArguments (), templateValue )
318323 .getArguments ().get (0 ).withPrefix (SINGLE_SPACE );
319324 initializer .add (newExpr );
320325 }
0 commit comments