diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/SummarizeGetter.java b/src/main/java/org/openrewrite/java/migrate/lombok/SummarizeGetter.java new file mode 100644 index 000000000..df9f9b20e --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/lombok/SummarizeGetter.java @@ -0,0 +1,128 @@ +/* + * Copyright 2024 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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.migrate.lombok;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.tree.J;
+
+import static java.util.Comparator.comparing;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class SummarizeGetter extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Summarize @Getter on fields to class level annotation";
+ }
+
+ @Override
+ public String getDescription() {
+ //language=markdown
+ return "Substitutes a class level `@Getter` annotation for annotations on every field.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new Summarizer();
+ }
+
+
+ @Value
+ @EqualsAndHashCode(callSuper = false)
+ private static class Summarizer extends JavaIsoVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.migrate.lombok;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.tree.J;
+
+import static java.util.Comparator.comparing;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class SummarizeSetter extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Summarize @Setter on fields to class level annotation";
+ }
+
+ @Override
+ public String getDescription() {
+ //language=markdown
+ return "Substitutes a class level `@Setter` annotation for annotations on every field.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new Summarizer();
+ }
+
+
+ @Value
+ @EqualsAndHashCode(callSuper = false)
+ private static class Summarizer extends JavaIsoVisitor
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.migrate.lombok;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class SummarizeGetterTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new SummarizeGetter());
+ }
+
+ @DocumentExample
+ @Test
+ void replaceOneFieldGetter() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+
+ class A {
+
+ @Getter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+
+ @Getter
+ class A {
+
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void replaceOneFieldGetterWhenInFront() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+
+ class A {
+
+ @Getter int foo = 9;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+
+ @Getter
+ class A {
+
+ int foo = 9;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void otherAnnotationAbove() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ @Getter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ @Getter
+ class A {
+
+ @Setter
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void otherAnnotationBelow() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ class A {
+
+ @Getter
+ @Setter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ @Getter
+ class A {
+
+ @Setter
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void otherAnnotationsAround() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+ import lombok.Singular;
+
+ class A {
+
+ @Singular
+ @Getter
+ @Setter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+ import lombok.Singular;
+
+ @Getter
+ class A {
+
+ @Singular
+ @Setter
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNothingWhenNotEveryFieldIsAnnotated() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+
+ class A {
+
+ @Getter
+ int foo;
+
+ int bar;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNothingWhenAFieldHasSpecialConfig() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Getter;
+
+ class A {
+
+ @Getter
+ int foo;
+
+ @Getter(AccessLevel.PACKAGE)
+ int bar;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void manyFields() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Getter;
+
+ class A {
+
+ @Getter
+ int foo;
+
+ @Getter
+ int bar;
+
+ @Getter
+ int foobar;
+
+ @Getter
+ int barfoo;
+
+ }
+ """,
+ """
+ import lombok.AccessLevel;
+ import lombok.Getter;
+
+ @Getter
+ class A {
+
+ int foo;
+
+ int bar;
+
+ int foobar;
+
+ int barfoo;
+
+ }
+ """
+ )
+ );
+ }
+
+
+
+}
diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/SummarizeSetterTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/SummarizeSetterTest.java
new file mode 100644
index 000000000..cdc18dbf0
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/migrate/lombok/SummarizeSetterTest.java
@@ -0,0 +1,420 @@
+/*
+ * Copyright 2024 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.migrate.lombok;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class SummarizeSetterTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new SummarizeSetter());
+ }
+
+ @DocumentExample
+ @Test
+ void replaceOneFieldGetter() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void replaceOneFieldGetterWhenInFront() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Setter;
+
+ class A {
+
+ @Setter int foo = 9;
+
+ }
+ """,
+ """
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ int foo = 9;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void otherAnnotationAbove() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ class A {
+
+ @Getter
+ @Setter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ @Getter
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void otherAnnotationBelow() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ @Getter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ @Getter
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void otherAnnotationsAround() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+ import lombok.Singular;
+
+ class A {
+
+ @Singular
+ @Setter
+ @Getter
+ int foo;
+
+ }
+ """,
+ """
+ import lombok.Getter;
+ import lombok.Setter;
+ import lombok.Singular;
+
+ @Setter
+ class A {
+
+ @Singular
+ @Getter
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNothingWhenNotEveryFieldIsAnnotated() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ int bar;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNothingWhenAFieldHasSpecialConfig() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ @Setter(AccessLevel.PACKAGE)
+ int bar;
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void manyFields() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ @Setter
+ int bar;
+
+ @Setter
+ int foobar;
+
+ @Setter
+ int barfoo;
+
+ }
+ """,
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ int foo;
+
+ int bar;
+
+ int foobar;
+
+ int barfoo;
+
+ }
+ """
+ )
+ );
+ }
+
+ /**
+ * The occurrence of final fields does not stand in the way of a class level @Setter.
+ * Lombok won't create Setters for them, but it does compile.
+ */
+ @Test
+ void finalField() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ final int bar;
+
+ @Setter
+ final int foobar;
+
+ @Setter
+ int barfoo;
+
+ }
+ """,
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ int foo;
+
+ final int bar;
+
+ final int foobar;
+
+ int barfoo;
+
+ }
+ """
+ )
+ );
+ }
+
+ /**
+ * The occurrence of static fields does not stand in the way of a class level @Setter.
+ * Lombok won't create Setters for them, but it does compile.
+ */
+ @Test
+ void staticField() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ static int bar;
+
+ @Setter
+ static int foobar;
+
+ @Setter
+ int barfoo;
+
+ }
+ """,
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ int foo;
+
+ static int bar;
+
+ static int foobar;
+
+ int barfoo;
+
+ }
+ """
+ )
+ );
+ }
+
+ /**
+ * The occurrence of final static fields does not stand in the way of a class level @Setter.
+ * Lombok won't create Setters for them, but it does compile.
+ */
+ @Test
+ void staticFinalField() {
+ rewriteRun(// language=java
+ java(
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ class A {
+
+ @Setter
+ int foo;
+
+ static final int bar;
+
+ @Setter
+ static final int foobar;
+
+ @Setter
+ int barfoo;
+
+ }
+ """,
+ """
+ import lombok.AccessLevel;
+ import lombok.Setter;
+
+ @Setter
+ class A {
+
+ int foo;
+
+ static final int bar;
+
+ static final int foobar;
+
+ int barfoo;
+
+ }
+ """
+ )
+ );
+ }
+
+
+}