Skip to content

Commit

Permalink
Migrate from extending EmptyInterceptor to implementing `Intercepto…
Browse files Browse the repository at this point in the history
…r`/`StatementInspector` (#39)

* Add hibernate-6.0.yml Including new recipe for going from extend EmptyInterceptor to implements Interceptor/StatementInspector

* Formating, name and desc

* License

* Empty newline

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Apply formatter

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Add test that fails; we can't assume `@Override` is present

* Drop duplicate `newArtifactId`

* Use MethodMatcher
Instead of autoformat, apply correct padding

* Apply suggestions from code review

Co-authored-by: Tim te Beek <tim@moderne.io>

* Polish to use JavaTemplate.apply & drop annotation handling

* Retain any other annotations, or lack of annotations

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent a089c43 commit 43662be
Show file tree
Hide file tree
Showing 4 changed files with 573 additions and 280 deletions.
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.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.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.FindImplementations;
import org.openrewrite.java.tree.*;

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 MethodMatcher ON_PREPARE_STATEMENT = new MethodMatcher("org.hibernate.Interceptor onPrepareStatement(java.lang.String)", true);

@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)));
maybeAddImport(INTERCEPTOR);
if (getCursor().pollMessage("prepareStatementFound") != null) {
cd = cd.withImplements(ListUtils.concat(cd.getImplements(), (TypeTree) TypeTree.build("StatementInspector").withType(JavaType.buildType(STATEMENT_INSPECTOR)).withPrefix(Space.SINGLE_SPACE)));
maybeAddImport(STATEMENT_INSPECTOR);
}
maybeRemoveImport(EMPTY_INTERCEPTOR);
if (cd.getPadding().getImplements() != null) {
cd = cd.getPadding().withImplements(cd.getPadding().getImplements().withBefore(Space.SINGLE_SPACE));
}
}
return cd;
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
J.ClassDeclaration cd = getCursor().firstEnclosing(J.ClassDeclaration.class);
if (cd != null && ON_PREPARE_STATEMENT.matches(md, cd)) {
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "prepareStatementFound", true);
J.MethodDeclaration inspectMD = JavaTemplate.apply(
(md.getLeadingAnnotations().isEmpty() ? "" : "@Override ") +
"public String inspect(String overriddenBelow) { return overriddenBelow; }",
getCursor(), md.getCoordinates().replace());
return inspectMD
.withPrefix(md.getPrefix())
.withLeadingAnnotations(md.getLeadingAnnotations())
.withParameters(md.getParameters())
.withBody(md.getBody());
}
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

0 comments on commit 43662be

Please sign in to comment.