diff --git a/build.gradle.kts b/build.gradle.kts index c759e6f..a2614ec 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,4 +19,7 @@ dependencies { testImplementation("org.openrewrite:rewrite-maven") testImplementation("org.junit.jupiter:junit-jupiter-engine:latest.release") + + testRuntimeOnly("org.hibernate:hibernate-core:5.6.15.Final") + } diff --git a/src/main/java/org/openrewrite/hibernate/TypeAnnotationParameter.java b/src/main/java/org/openrewrite/hibernate/TypeAnnotationParameter.java new file mode 100644 index 0000000..ce1e546 --- /dev/null +++ b/src/main/java/org/openrewrite/hibernate/TypeAnnotationParameter.java @@ -0,0 +1,100 @@ +/* + * Copyright 2021 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.hibernate;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.Tree;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.tree.*;
+import org.openrewrite.marker.Markers;
+
+import java.time.Duration;
+import java.util.Collections;
+
+public class TypeAnnotationParameter extends Recipe {
+
+ private static final String FQN_TYPE_ANNOTATION = "org.hibernate.annotations.Type";
+
+ @Override
+ public String getDisplayName() {
+ return "@Type annotation type parameter migration";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Hibernate 6.x has 'type' parameter of type String replaced with 'value' of type class.";
+ }
+
+ @Override
+ public @Nullable Duration getEstimatedEffortPerOccurrence() {
+ return Duration.ofMinutes(1);
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new 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.hibernate;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+public class TypeAnnotationParameterTest implements RewriteTest {
+
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new TypeAnnotationParameter()).parser(JavaParser.fromJavaVersion().classpath("hibernate-core"));
+ }
+
+ @Test
+ void onlyOneParameter() {
+ rewriteRun(
+ //language=java
+ java("""
+ import org.hibernate.annotations.Type;
+
+ public class TestApplication {
+ @Type(type = "java.util.concurrent.atomic.AtomicBoolean")
+ Object a;
+ }
+ """,
+ """
+ import org.hibernate.annotations.Type;
+
+ import java.util.concurrent.atomic.AtomicBoolean;
+
+ public class TestApplication {
+ @Type(AtomicBoolean.class)
+ Object a;
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void multipleParameters() {
+ rewriteRun(
+ //language=java
+ java("""
+ import org.hibernate.annotations.Type;
+
+ class TestApplication {
+ @Type(type = "java.util.concurrent.atomic.AtomicBoolean", parameters = {})
+ Object a;
+ }
+ """,
+ """
+ import org.hibernate.annotations.Type;
+
+ import java.util.concurrent.atomic.AtomicBoolean;
+
+ class TestApplication {
+ @Type(value = AtomicBoolean.class, parameters = {})
+ Object a;
+ }
+ """
+ )
+ );
+ }
+}