-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type Annotation type parameter replaced by value parameter (#8)
* 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
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/org/openrewrite/hibernate/TypeAnnotationParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/org/openrewrite/hibernate/TypeAnnotationParameterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |