-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* AutoGeneratedValue recipe * cleanup * more cleanup * renamed recipe * removed public modifier from tests * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * recipe rename and whitespace cleanup * tweaked recipe logic * Update src/test/java/org/openrewrite/java/migrate/javaee/AddTableGeneratorTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/java/migrate/javaee/AddTableGeneratorTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * updated recipe description * Move recipe to openjpa-to-eclipselink.yml * Apply formatter --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bcf5804
commit 1ced6cf
Showing
5 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/main/java/org/openrewrite/java/migrate/javax/AddTableGenerator.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,90 @@ | ||
/* | ||
* Copyright 2024 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.java.migrate.javax; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.AnnotationMatcher; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.search.FindAnnotations; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class AddTableGenerator extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Attributes with automatically generated values require configuration"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Adds missing `@TableGenerator` annotation and updates the `@GeneratedValue` annotation values when it uses automatically generated values."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
new UsesType<>("javax.persistence.GeneratedValue", false), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
private final AnnotationMatcher GENERATED_VALUE = new AnnotationMatcher("@javax.persistence.GeneratedValue"); | ||
private final AnnotationMatcher GENERATED_VALUE_AUTO = new AnnotationMatcher("@javax.persistence.GeneratedValue(strategy=javax.persistence.GenerationType.AUTO)"); | ||
|
||
@Override | ||
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { | ||
Set<J.Annotation> generatedValueAnnotations = FindAnnotations.find(multiVariable, "@javax.persistence.GeneratedValue"); | ||
if (generatedValueAnnotations.isEmpty()) { | ||
return multiVariable; | ||
} | ||
|
||
J.Annotation generatedValueAnnotation = generatedValueAnnotations.iterator().next(); | ||
List<Expression> args = generatedValueAnnotation.getArguments(); | ||
if (!(args == null || args.isEmpty() || GENERATED_VALUE_AUTO.matches(generatedValueAnnotation))) { | ||
return multiVariable; | ||
} | ||
|
||
J.VariableDeclarations updatedVariable = JavaTemplate.apply( | ||
"@javax.persistence.TableGenerator(name = \"OPENJPA_SEQUENCE_TABLE\", table = \"OPENJPA_SEQUENCE_TABLE\", pkColumnName = \"ID\", valueColumnName = \"SEQUENCE_VALUE\", pkColumnValue = \"0\")", | ||
getCursor(), | ||
multiVariable.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); | ||
return super.visitVariableDeclarations(updatedVariable, ctx); | ||
} | ||
|
||
@Override | ||
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) { | ||
if (!GENERATED_VALUE.matches(annotation) && !GENERATED_VALUE_AUTO.matches(annotation)) { | ||
return annotation; | ||
} | ||
return JavaTemplate.builder("strategy = javax.persistence.GenerationType.TABLE, generator = \"OPENJPA_SEQUENCE_TABLE\"") | ||
.contextSensitive() | ||
.build() | ||
.apply(getCursor(), annotation.getCoordinates().replaceArguments()); | ||
} | ||
} | ||
); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/resources/META-INF/rewrite/openjpa-to-eclipselink.yml
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,25 @@ | ||
# | ||
# Copyright 2024 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. | ||
# | ||
--- | ||
type: specs.openrewrite.org/v1beta/recipe | ||
name: org.openrewrite.java.migrate.javax.openJPAToEclipseLink | ||
displayName: Migrate from OpenJPA to EclipseLink JPA | ||
description: These recipes help migrate Java Persistence applications using OpenJPA to EclipseLink JPA. | ||
tags: | ||
- javaee7 | ||
- deprecated | ||
recipeList: | ||
- org.openrewrite.java.migrate.javax.AddTableGenerator |
192 changes: 192 additions & 0 deletions
192
src/test/java/org/openrewrite/java/migrate/javaee/AddTableGeneratorTest.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,192 @@ | ||
/* | ||
* Copyright 2024 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.java.migrate.javaee; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.migrate.javax.AddTableGenerator; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class AddTableGeneratorTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.parser(JavaParser.fromJavaVersion() | ||
.classpathFromResources(new InMemoryExecutionContext(), "javax.persistence-api-2.2")) | ||
.recipe(new AddTableGenerator()); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void generatedValueExample() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.TableGenerator; | ||
@Entity | ||
public class GeneratedValueExample { | ||
// flag it | ||
@Id | ||
@GeneratedValue(strategy=GenerationType.AUTO) | ||
private int id; | ||
// flag it. Does not require @Id | ||
@GeneratedValue | ||
private int id2; | ||
// flag it even though it has a TableGenerator since GeneratedValue is default | ||
// A second TableGenerator will be created. | ||
@TableGenerator(name = "SOME_TABLE", table = "SOME_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@GeneratedValue | ||
private int id3; | ||
} | ||
""", """ | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.TableGenerator; | ||
@Entity | ||
public class GeneratedValueExample { | ||
// flag it | ||
@Id | ||
@javax.persistence.TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@GeneratedValue(strategy = javax.persistence.GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE") | ||
private int id; | ||
// flag it. Does not require @Id | ||
@GeneratedValue(strategy = javax.persistence.GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE") | ||
@javax.persistence.TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
private int id2; | ||
// flag it even though it has a TableGenerator since GeneratedValue is default | ||
// A second TableGenerator will be created. | ||
@TableGenerator(name = "SOME_TABLE", table = "SOME_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@javax.persistence.TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@GeneratedValue(strategy = javax.persistence.GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE") | ||
private int id3; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void generatedValueName() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
@Entity | ||
class GeneratedValueName { | ||
@Id | ||
@GeneratedValue | ||
private String name; | ||
} | ||
""", | ||
""" | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
@Entity | ||
class GeneratedValueName { | ||
@Id | ||
@javax.persistence.TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@GeneratedValue(strategy = javax.persistence.GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE") | ||
private String name; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void generatedValueQuickFixApplied() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.TableGenerator; | ||
@Entity | ||
class GeneratedValueQuickFixApplied { | ||
// not flagged since GeneratedValue is not default or AUTO | ||
@Id | ||
@TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@GeneratedValue(strategy = GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE") | ||
private int id; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void generatedValueStrategySpaces() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
@Entity | ||
class GeneratedValueStrategySpaces { | ||
@Id | ||
@GeneratedValue( strategy = GenerationType.AUTO ) | ||
private int id; | ||
} | ||
""", | ||
""" | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
@Entity | ||
class GeneratedValueStrategySpaces { | ||
@Id | ||
@javax.persistence.TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0") | ||
@GeneratedValue(strategy = javax.persistence.GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE") | ||
private int id; | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Binary file added
BIN
+161 KB
src/test/resources/META-INF/rewrite/classpath/javax.persistence-api-2.2.jar
Binary file not shown.