From d258000f68c7738b930687fb0d53aa453e2ed167 Mon Sep 17 00:00:00 2001 From: timo <1398557+timo-a@users.noreply.github.com> Date: Tue, 24 Dec 2024 12:45:45 +0100 Subject: [PATCH 1/5] migrate recipe as-is --- .../lombok/NegligentlyConvertEquals.java | 101 +++++++++++ .../lombok/NegligentlyConvertEqualsTest.java | 166 ++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEquals.java create mode 100644 src/test/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEqualsTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEquals.java b/src/main/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEquals.java new file mode 100644 index 0000000000..9f0fa6428b --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEquals.java @@ -0,0 +1,101 @@ +/* + * 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.MethodMatcher;
+import org.openrewrite.java.tree.J;
+
+import static java.util.Comparator.comparing;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class NegligentlyConvertEquals extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ //language=markdown
+ return "Replace any custom `equals` or `hashCode` methods with the `EqualsAndHashCode` annotation";
+ }
+
+ @Override
+ public String getDescription() {
+ //language=markdown
+ return "This recipe substitutes a class level `@EqualsAndHashCode` annotation for a custom `equals` or `hashCode` methods. " +
+ "If both are defined, then both will be replaced. If only one is defined then it will be replaced. " +
+ "This recipe does not check if the custom `equals` or `hashCode` methods behave like ones generated by the lombok annotation. " +
+ "Doing so is considered infeasible at this time. " +
+ "As a compromise this recipe finds and replaces the custom methods and relies on the user to review the changes closely. " +
+ "As a consequence this recipe is VERY DANGEROUS to include into a composite recipe! " +
+ "Users are advised to run it only in isolation.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new Converter();
+ }
+
+ @Value
+ @EqualsAndHashCode(callSuper = false)
+ private static class Converter 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.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class NegligentlyConvertEqualsTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new NegligentlyConvertEquals())
+ .parser(JavaParser.fromJavaVersion().logCompilationWarningsAndErrors(true).classpath("lombok"));
+ }
+
+ @DocumentExample
+ @Test
+ void replaceEquals() {
+ rewriteRun(// language=java
+ java(
+ """
+ class A {
+
+ int foo;
+
+ @Override
+ public boolean equals(Object o) {
+ return false;
+ }
+ }
+ """,
+ """
+ import lombok.EqualsAndHashCode;
+
+ @EqualsAndHashCode
+ class A {
+
+ int foo;
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void replaceEqualsInPackage() {
+ rewriteRun(// language=java
+ java(
+ """
+ package com.example;
+
+ class A {
+
+ int foo;
+
+ @Override
+ public boolean equals(Object o) {
+ return false;
+ }
+ }
+ """,
+ """
+ package com.example;
+
+ import lombok.EqualsAndHashCode;
+
+ @EqualsAndHashCode
+ class A {
+
+ int foo;
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void replaceHashCode() {
+ rewriteRun(// language=java
+ java(
+ """
+ package com.example;
+
+ class A {
+
+ int foo;
+
+ @Override
+ public int hashCode() {
+ return 6;
+ }
+ }
+ """,
+ """
+ package com.example;
+
+ import lombok.EqualsAndHashCode;
+
+ @EqualsAndHashCode
+ class A {
+
+ int foo;
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void replaceEqualsAndHashCode() {
+ rewriteRun(// language=java
+ java(
+ """
+ package com.example;
+
+ class A {
+
+ int foo;
+
+ @Override
+ public boolean equals(Object o) {
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return 6;
+ }
+
+ }
+ """,
+ """
+ package com.example;
+
+ import lombok.EqualsAndHashCode;
+
+ @EqualsAndHashCode
+ class A {
+
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
+}
From 4db9a726cf1deb3bfdd8f8236f16e0fb6be3beb1 Mon Sep 17 00:00:00 2001
From: timo <1398557+timo-a@users.noreply.github.com>
Date: Tue, 24 Dec 2024 12:59:46 +0100
Subject: [PATCH 2/5] add test for empty class
---
.../lombok/NegligentlyConvertEqualsTest.java | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEqualsTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEqualsTest.java
index e9ebfd4c8d..79f74cc883 100644
--- a/src/test/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEqualsTest.java
+++ b/src/test/java/org/openrewrite/java/migrate/lombok/NegligentlyConvertEqualsTest.java
@@ -60,6 +60,21 @@ class A {
);
}
+ @Test
+ void noCostomMethodsNoAnnotation() {
+ rewriteRun(// language=java
+ java(
+ """
+ class A {
+
+ int foo;
+
+ }
+ """
+ )
+ );
+ }
+
@Test
void replaceEqualsInPackage() {
rewriteRun(// language=java
From b2206b6c7259e25c16e9d434f50bbececb6e542a Mon Sep 17 00:00:00 2001
From: Tim te Beek