diff --git a/src/main/java/org/openrewrite/java/spring/NotRepeatSpringAnnotationsInSubclasses.java b/src/main/java/org/openrewrite/java/spring/NotRepeatSpringAnnotationsInSubclasses.java new file mode 100644 index 000000000..b0c8663a4 --- /dev/null +++ b/src/main/java/org/openrewrite/java/spring/NotRepeatSpringAnnotationsInSubclasses.java @@ -0,0 +1,83 @@ +/* + * Copyright 2025 the original author or authors. + *
+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://docs.moderne.io/licensing/moderne-source-available-license + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.spring;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.RemoveAnnotation;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.java.tree.TypeUtils;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class NotRepeatSpringAnnotationsInSubclasses extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Remove Spring annotations if they repeating in subclasses";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Remove Spring annotations in subclasses if they present in base classes.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ //return Preconditions.check(new UsesType<>("org.springframework.web.bind.annotation.PostMapping", false), new JavaIsoVisitor
+ * Licensed under the Moderne Source Available License (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://docs.moderne.io/licensing/moderne-source-available-license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.spring;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class NotRepeatSpringAnnotationsInSubclassesTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new NotRepeatSpringAnnotationsInSubclasses())
+ .parser(JavaParser.fromJavaVersion().classpath("spring-beans", "spring-boot",
+ "spring-context", "spring-core", "spring-web")
+ .dependsOn(
+ """
+ import org.springframework.web.bind.annotation.PathVariable;
+ import org.springframework.web.bind.annotation.PostMapping;
+ import org.springframework.web.bind.annotation.RequestBody;
+
+ public interface UserApi {
+ @PostMapping("/users/{id}")
+ String updateUser(
+ @PathVariable("id") Long id,
+ @RequestBody UserData request
+ );
+
+ class UserData {
+ private String firstName;
+ private String lastName;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+ }
+ }
+ """
+ ));
+ }
+
+ @Test
+ @DocumentExample
+ void removeMethodAnnotation() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.springframework.web.bind.annotation.PostMapping;
+ import org.springframework.web.bind.annotation.RestController;
+
+ @RestController
+ public class UserController implements UserApi {
+
+ @Override @PostMapping("/users/{id}")
+ public String updateUser(Long id, UserData request) {
+ return "User " + id + " updated: " + request.getFirstName() + " " + request.getLastName();
+ }
+ }
+ """,
+ """
+ import org.springframework.web.bind.annotation.RestController;
+
+ @RestController
+ public class UserController implements UserApi {
+
+ @Override
+ public String updateUser(Long id, UserData request) {
+ return "User " + id + " updated: " + request.getFirstName() + " " + request.getLastName();
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ @Disabled
+ void removeMethodArgumentsAnnotation() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.springframework.web.bind.annotation.PathVariable;
+ import org.springframework.web.bind.annotation.PostMapping;
+ import org.springframework.web.bind.annotation.RequestBody;
+ import org.springframework.web.bind.annotation.RestController;
+
+ @RestController
+ public class UserController implements UserApi {
+
+ @Override @PostMapping("/users/{id}")
+ public String updateUser(
+ @PathVariable("id") Long id,
+ @RequestBody UserData request
+ ) {
+ return "User " + id + " updated: " + request.getFirstName() + " " + request.getLastName();
+ }
+ }
+ """,
+ """
+ import org.springframework.web.bind.annotation.RestController;
+
+ @RestController
+ public class UserController implements UserApi {
+
+ @Override
+ public String updateUser(Long id, UserData request) {
+ return "User " + id + " updated: " + request.getFirstName() + " " + request.getLastName();
+ }
+ }
+ """
+ )
+ );
+ }
+}