|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.javax; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import org.openrewrite.ExecutionContext; |
| 20 | +import org.openrewrite.Preconditions; |
| 21 | +import org.openrewrite.Recipe; |
| 22 | +import org.openrewrite.TreeVisitor; |
| 23 | +import org.openrewrite.java.ChangeType; |
| 24 | +import org.openrewrite.java.JavaIsoVisitor; |
| 25 | +import org.openrewrite.java.RemoveAnnotationAttribute; |
| 26 | +import org.openrewrite.java.search.FindAnnotations; |
| 27 | +import org.openrewrite.java.search.UsesType; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | + |
| 30 | +@EqualsAndHashCode(callSuper = false) |
| 31 | +public class UseJoinColumnForMapping extends Recipe { |
| 32 | + private final String JOIN_COLUMN = "javax.persistence.JoinColumn"; |
| 33 | + private final String COLUMN = "javax.persistence.Column"; |
| 34 | + |
| 35 | + @Override |
| 36 | + public String getDisplayName() { |
| 37 | + return "`@JoinColumn` annotations must be used with relationship mappings"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getDescription() { |
| 42 | + return "In OpenJPA, when a relationship attribute has either a `@OneToOne` or a `@ManyToOne` annotation with a " + |
| 43 | + "`@Column` annotation, the `@Column` annotation is treated as a `@JoinColumn` annotation. EclipseLink " + |
| 44 | + "throws an exception that indicates that the entity class must use `@JoinColumn` instead of `@Column` " + |
| 45 | + "to map a relationship attribute."; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 50 | + return Preconditions.check( |
| 51 | + Preconditions.and( |
| 52 | + new UsesType<>("javax.persistence.Entity", true), |
| 53 | + new UsesType<>(COLUMN, true), |
| 54 | + Preconditions.or( |
| 55 | + new UsesType<>("javax.persistence.OneToOne", true), |
| 56 | + new UsesType<>("javax.persistence.ManyToOne", true) |
| 57 | + ) |
| 58 | + ), |
| 59 | + new JavaIsoVisitor<ExecutionContext>() { |
| 60 | + @Override |
| 61 | + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { |
| 62 | + // Exit if not annotated with @Column and a relationship mapping annotation |
| 63 | + if (FindAnnotations.find(multiVariable, COLUMN).isEmpty() |
| 64 | + || (FindAnnotations.find(multiVariable, "javax.persistence.OneToOne").isEmpty() |
| 65 | + && FindAnnotations.find(multiVariable, "javax.persistence.ManyToOne").isEmpty())) { |
| 66 | + return multiVariable; |
| 67 | + } |
| 68 | + |
| 69 | + // Change @Column to @JoinColumn |
| 70 | + // The javax.persistence.Column attributes length, precision, and scale are not kept. |
| 71 | + maybeRemoveImport(COLUMN); |
| 72 | + maybeAddImport(JOIN_COLUMN); |
| 73 | + J.VariableDeclarations joinColumn = (J.VariableDeclarations) new ChangeType(COLUMN, JOIN_COLUMN, false).getVisitor().visit(multiVariable, ctx); |
| 74 | + joinColumn = (J.VariableDeclarations) new RemoveAnnotationAttribute(JOIN_COLUMN, "length").getVisitor().visit(joinColumn, ctx); |
| 75 | + joinColumn = (J.VariableDeclarations) new RemoveAnnotationAttribute(JOIN_COLUMN, "precision").getVisitor().visit(joinColumn, ctx); |
| 76 | + joinColumn = (J.VariableDeclarations) new RemoveAnnotationAttribute(JOIN_COLUMN, "scale").getVisitor().visit(joinColumn, ctx); |
| 77 | + |
| 78 | + return joinColumn; |
| 79 | + } |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments