File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
main/java/org/openrewrite/spring
test/kotlin/org/openrewrite/spring Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,9 @@ public J visitAnnotation(J.Annotation annotation) {
5151 J .Annotation a = refactor (annotation , super ::visitAnnotation );
5252
5353 if (PARAM_ANNOTATIONS .stream ().anyMatch (annClass -> isOfClassType (annotation .getType (), annClass )) &&
54- annotation .getArgs () != null && nameArgumentValue (annotation ).isPresent ()) {
54+ annotation .getArgs () != null && nameArgumentValue (annotation ).isPresent () &&
55+ // ModelAttribute can be on methods as well as parameters
56+ getCursor ().getParentOrThrow ().getTree () instanceof J .VariableDecls ) {
5557 J .Annotation .Arguments args = a .getArgs ();
5658
5759 if (args == null ) {
@@ -74,6 +76,7 @@ public J visitAnnotation(J.Annotation annotation) {
7476 args = null ;
7577 }
7678
79+
7780 nameArgumentValue (a ).ifPresent (value -> andThen (new RenameVariable (
7881 getCursor ().getParentOrThrow ().<J .VariableDecls >getTree ().getVars ().get (0 ),
7982 (String ) value .getValue ())));
Original file line number Diff line number Diff line change @@ -99,4 +99,33 @@ class ImplicitWebAnnotationNamesTest {
9999 }
100100 """ )
101101 }
102+
103+ @Test
104+ fun dontRemoveModelAttributeOnMethods () {
105+ val controller = jp.parse("""
106+ import org.springframework.web.bind.annotation.*;
107+ import java.util.*;
108+
109+ public class UsersController {
110+ @ModelAttribute("types")
111+ public Collection<String> populateUserTypes() {
112+ return Arrays.asList("free", "premium");
113+ }
114+ }
115+ """ .trimIndent())
116+
117+ val fixed = controller.refactor().visit(ImplicitWebAnnotationNames ()).fix().fixed
118+
119+ assertRefactored(fixed, """
120+ import org.springframework.web.bind.annotation.*;
121+ import java.util.*;
122+
123+ public class UsersController {
124+ @ModelAttribute("types")
125+ public Collection<String> populateUserTypes() {
126+ return Arrays.asList("free", "premium");
127+ }
128+ }
129+ """ )
130+ }
102131}
You can’t perform that action at this time.
0 commit comments