Skip to content

Commit

Permalink
Type Annotation type parameter replaced by value parameter (#8)
Browse files Browse the repository at this point in the history
* Type Annotation type parameter replaced by value parameter

* Format tests

* Remove unused autoformat & unnecessary else block; apply formatter

* Include TypeAnnotationParameter in MigrateToHibernate61

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
BoykoAlex and timtebeek authored Aug 17, 2023
1 parent 9e4338f commit 2a82e45
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}
100 changes: 100 additions & 0 deletions src/main/java/org/openrewrite/hibernate/TypeAnnotationParameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<ExecutionContext>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext executionContext) {
J.Annotation a = super.visitAnnotation(annotation, executionContext);
JavaType.FullyQualified type = TypeUtils.asFullyQualified(a.getType());
if (type != null && FQN_TYPE_ANNOTATION.equals(type.getFullyQualifiedName())) {
final boolean isOnlyParameter = a.getArguments().size() == 1;
a = a.withArguments(ListUtils.map(a.getArguments(), arg -> {
if (arg instanceof J.Assignment) {
J.Assignment assignment = (J.Assignment) arg;
if (assignment.getVariable() instanceof J.Identifier
&& "type".equals(((J.Identifier) assignment.getVariable()).getSimpleName())
&& assignment.getAssignment() instanceof J.Literal) {
J.Identifier paramName = (J.Identifier) assignment.getVariable();
String fqTypeName = (String) ((J.Literal) assignment.getAssignment()).getValue();
String simpleTypeName = getSimpleName(fqTypeName);
JavaType typeOfNewValue = JavaType.buildType(fqTypeName);
J.FieldAccess fa = new J.FieldAccess(
Tree.randomId(),
isOnlyParameter ? Space.EMPTY : assignment.getAssignment().getPrefix(),
assignment.getAssignment().getMarkers(),
new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, Collections.emptyList(), simpleTypeName, typeOfNewValue, null),
JLeftPadded.build(new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, Collections.emptyList(), "class", null, null)),
JavaType.buildType("java.lang.Class")
);
maybeAddImport(fqTypeName);
if (isOnlyParameter) {
return fa;
}
return assignment.withVariable(paramName.withSimpleName("value")).withAssignment(fa);
}
}
return arg;
}));
}
return a;
}
};
}

private static String getSimpleName(String fqName) {
int idx = fqName.lastIndexOf('.');
if (idx > 0 && idx < fqName.length() - 1) {
return fqName.substring(idx + 1);
}
return fqName;
}

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/hibernate-6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ description: >
recipeList:
- org.openrewrite.hibernate.MigrateToHibernateDependencies61
- org.openrewrite.hibernate.TypeAnnotationParameter
- org.openrewrite.hibernate.TypeDescriptorToType
- org.openrewrite.java.migrate.jakarta.JavaxPersistenceToJakartaPersistence
- org.openrewrite.java.migrate.jakarta.JavaxPersistenceXmlToJakartaPersistenceXml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}
"""
)
);
}
}

0 comments on commit 2a82e45

Please sign in to comment.