Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from extending EmptyInterceptor to implementing Interceptor/StatementInspector #39

Merged
merged 13 commits into from
Oct 8, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.hibernate;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.FindImplementations;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeTree;
import org.openrewrite.java.tree.TypeUtils;
public class EmptyInterceptorToInterface extends Recipe {

private final String EMPTY_INTERCEPTOR = "org.hibernate.EmptyInterceptor";
private final String INTERCEPTOR = "org.hibernate.Interceptor";
private final String STATEMENT_INSPECTOR = "org.hibernate.resource.jdbc.spi.StatementInspector";
private static final AnnotationMatcher OVERRIDE_ANNOTATION_MATCHER = new AnnotationMatcher("java.lang.Override");

@Override
public String getDisplayName() {
return "Replace `extends EmptyInterceptor` with `implements Interceptor` and potentially `StatementInspector`";
}

@Override
public String getDescription() {
return "In Hibernate 6.0 the `Interceptor` interface received default implementations therefore the NOOP implementation that could be extended was no longer needed. " +
"This recipe migrates 5.x `Interceptor#onPrepareStatement(String)` to 6.0 `StatementInspector#inspect()`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new FindImplementations(EMPTY_INTERCEPTOR),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (cd.getExtends() != null && TypeUtils.isOfClassType(cd.getExtends().getType(), EMPTY_INTERCEPTOR)) {
cd = cd.withExtends(null).withImplements(ListUtils.concat(cd.getImplements(), (TypeTree) TypeTree.build("Interceptor").withType(JavaType.buildType(INTERCEPTOR)).withPrefix(Space.SINGLE_SPACE)));
Boolean prepareStatement = getCursor().pollMessage("prepareStatementFound");
if (Boolean.TRUE.equals(prepareStatement)) {
cd = cd.withImplements(ListUtils.concat(cd.getImplements(), (TypeTree) TypeTree.build("StatementInspector").withType(JavaType.buildType(STATEMENT_INSPECTOR)).withPrefix(Space.SINGLE_SPACE)));
}
maybeAddImport(INTERCEPTOR);
maybeAddImport(STATEMENT_INSPECTOR);
maybeRemoveImport(EMPTY_INTERCEPTOR);
}
return autoFormat(cd, ctx);
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
if (md.getLeadingAnnotations().stream().anyMatch(OVERRIDE_ANNOTATION_MATCHER::matches) && md.getSimpleName().equals("onPrepareStatement")) {
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "prepareStatementFound", true);
String template = "@Override\n" +
"public String inspect() {\n" +
"}\n";
J.MethodDeclaration inspect = JavaTemplate.builder(template)
.javaParser(JavaParser.fromJavaVersion())
.build()
.apply(getCursor(), md.getCoordinates().replace());
md = inspect.withBody(md.getBody()).withModifiers(md.getModifiers()).withLeadingAnnotations(md.getLeadingAnnotations()).withParameters(md.getParameters());
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}
return md;
}
}
);
}
}
283 changes: 283 additions & 0 deletions src/main/resources/META-INF/rewrite/hibernate-6.0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
#
# 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.hibernate.MigrateToHibernate60
displayName: Migrate to Hibernate 6.0.x
description: >-
This recipe will apply changes commonly needed when migrating to Hibernate 6.0.x. The hibernate dependencies will
be updated to use the new org.hibernate.orm group ID and the recipe will make changes necessary to use Hibernate
with Jakarta EE 9.0.

recipeList:
- org.openrewrite.hibernate.MigrateToHibernateDependencies60
- org.openrewrite.hibernate.EmptyInterceptorToInterface
- org.openrewrite.hibernate.MigrateBooleanMappings
- org.openrewrite.hibernate.TypeAnnotationParameter
- org.openrewrite.hibernate.TypeDescriptorToType
- org.openrewrite.java.migrate.jakarta.JavaxPersistenceToJakartaPersistence
- org.openrewrite.java.migrate.jakarta.JavaxPersistenceXmlToJakartaPersistenceXml
- org.openrewrite.hibernate.MigrateToHypersistenceUtilsHibernate6.0

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.hibernate.MigrateToHibernateDependencies60
displayName: Migrate Hibernate dependencies to 6.0.x
description: >-
This recipe will migrate any existing dependencies on Hibernate 5.x to the latest 6.0.x release. This migration will
include the adjustment to the new `org.hibernate.orm` group ID. It accounts for artifacts names that both do and do
not include the `jakarta` suffix and it will change both dependencies and managed dependencies.

recipeList:
# hibernate-agroal
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-agroal
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-agroal-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-agroal
newVersion: 6.0.x
# hibernate-c3p0
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-c3p0
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-c3p0-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-c3p0
newVersion: 6.0.x
# hibernate-community-dialects
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-community-dialects
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-community-dialects-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-community-dialects
newVersion: 6.0.x
# hibernate-core
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-core
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-core-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-core
newVersion: 6.0.x
# hibernate-envers
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-envers
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-envers-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-envers
newVersion: 6.0.x
# hibernate-graalvm
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-graalvm
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-graalvm-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-graalvm
newVersion: 6.0.x
# hibernate-hikaricp
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-hikaricp
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-hikaricp-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-hikaricp
newVersion: 6.0.x
# hibernate-jcache
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-jcache
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-jcache-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-jcache
newVersion: 6.0.x
# hibernate-jpamodelgen
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-jpamodelgen
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-jpamodelgen-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-jpamodelgen
newVersion: 6.0.x
# hibernate-micrometer
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-micrometer
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-micrometer-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-micrometer
newVersion: 6.0.x
# hibernate-proxool
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-proxool
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-proxool-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-proxool
newVersion: 6.0.x
# hibernate-spatial
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-spatial
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-spatial-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-spatial
newVersion: 6.0.x
# hibernate-testing
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-testing
newGroupId: org.hibernate.orm
newVersion: 6.0.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.hibernate
oldArtifactId: hibernate-testing-jakarta
newGroupId: org.hibernate.orm
newArtifactId: hibernate-testing
newVersion: 6.0.x
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: org.hibernate
artifactId: hibernate-entitymanager
# Account for jaxb-runtime becoming `runtime` vs `compile` dependency
# Add the jakarta JAXB artifact if it is missing but a project uses types in java.xml.bind
- org.openrewrite.java.dependencies.AddDependency:
groupId: jakarta.xml.bind
artifactId: jakarta.xml.bind-api
version: 3.0.x
onlyIfUsing: javax.xml.bind..*
acceptTransitive: true
# If a project already had the jakarta api, make sure it is at the latest version.
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: jakarta.xml.bind
artifactId: jakarta.xml.bind-api
newVersion: 3.0.x

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.hibernate.TypeDescriptorToType
displayName: Rename `JavaTypeDescriptor` and `SqlTypeDescriptor` to `JavaType` and `SqlType`
description: >-
Rename `JavaTypeDescriptor` and `SqlTypeDescriptor` to `JavaType` and `SqlType` respectively.
See https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#type-system

recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.hibernate.type.descriptor.java.JavaTypeDescriptor
newFullyQualifiedTypeName: org.hibernate.type.descriptor.java.JavaType
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.hibernate.type.descriptor.sql.SqlTypeDescriptor
newFullyQualifiedTypeName: org.hibernate.type.descriptor.sql.SqlType

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.hibernate.MigrateToHypersistenceUtilsHibernate6.0
displayName: Migrate Hibernate Types to Hypersistence Utils 6.0
description: >-
This recipe will migrate any existing dependencies on `com.vladmihalcea:hibernate-types` to `io.hypersistence:hypersistence-utils-hibernate-60`.
This migration will include the adjustment from `com.vladmihalcea` to `io.hypersistence.utils` package name.

recipeList:
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.vladmihalcea
oldArtifactId: hibernate-types-4
newGroupId: io.hypersistence
newArtifactId: hypersistence-utils-hibernate-60
newVersion: 3.5.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.vladmihalcea
oldArtifactId: hibernate-types-5
newGroupId: io.hypersistence
newArtifactId: hypersistence-utils-hibernate-60
newVersion: 3.5.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.vladmihalcea
oldArtifactId: hibernate-types-43
newGroupId: io.hypersistence
newArtifactId: hypersistence-utils-hibernate-60
newVersion: 3.5.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.vladmihalcea
oldArtifactId: hibernate-types-52
newGroupId: io.hypersistence
newArtifactId: hypersistence-utils-hibernate-60
newVersion: 3.5.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.vladmihalcea
oldArtifactId: hibernate-types-55
newGroupId: io.hypersistence
newArtifactId: hypersistence-utils-hibernate-60
newVersion: 3.5.x
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.vladmihalcea
oldArtifactId: hibernate-types-60
newGroupId: io.hypersistence
newArtifactId: hypersistence-utils-hibernate-60
newVersion: 3.5.x
- org.openrewrite.java.ChangePackage:
oldPackageName: com.vladmihalcea
newPackageName: io.hypersistence.utils
recursive: true
Loading
Loading