Skip to content

Commit

Permalink
Adapt Hibernate native support for HHH-17643
Browse files Browse the repository at this point in the history
This commit adapts Hibernate native support to handle
the changes performed as part of HHH-17643 which impacts
Hibernate versions 6.4.3+ and 6.2.21+.

It ignores the BytecodeProvider services loaded by the
service loader feature in order to default to the
"no-op" provider with native, and makes the substitutions
more lenient when a substituted field or method does not
exist.

gh-32314 is expected to remove the need for such
substitutions which are not great for maintainability
by design.

Closes gh-32311
  • Loading branch information
sdeleuze committed Feb 22, 2024
1 parent 41433d4 commit 9430b24
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.orm.jpa.vendor;

import java.util.Map;
import java.util.function.Predicate;

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
Expand All @@ -31,11 +32,27 @@
* @since 6.1
* @see <a href="https://hibernate.atlassian.net/browse/HHH-17568">HHH-17568</a>
*/
@TargetClass(className = "org.hibernate.bytecode.internal.none.BytecodeProviderImpl", onlyWith = SubstituteOnlyIfPresent.class)
@TargetClass(className = "org.hibernate.bytecode.internal.none.BytecodeProviderImpl", onlyWith = Target_BytecodeProvider.SubstituteOnlyIfPresent.class)
final class Target_BytecodeProvider {

@Substitute
public ReflectionOptimizer getReflectionOptimizer(Class<?> clazz, Map<String, PropertyAccess> propertyAccessMap) {
return null;
}

static class SubstituteOnlyIfPresent implements Predicate<String> {

@Override
public boolean test(String type) {
try {
Class<?> clazz = Class.forName(type, false, getClass().getClassLoader());
clazz.getDeclaredMethod("getReflectionOptimizer", Class.class, Map.class);
return true;
}
catch (ClassNotFoundException | NoClassDefFoundError | NoSuchMethodException ex) {
return false;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.orm.jpa.vendor;

import java.util.function.Predicate;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
Expand All @@ -27,12 +29,11 @@
/**
* Hibernate substitution designed to prevent ByteBuddy reachability on native, and to enforce the
* usage of {@code org.hibernate.bytecode.internal.none.BytecodeProviderImpl} with Hibernate 6.3+.
* TODO Collaborate with Hibernate team on a substitution-less alternative that does not require a custom list of StandardServiceInitiator
*
* @author Sebastien Deleuze
* @since 6.1
*/
@TargetClass(className = "org.hibernate.bytecode.internal.BytecodeProviderInitiator", onlyWith = SubstituteOnlyIfPresent.class)
@TargetClass(className = "org.hibernate.bytecode.internal.BytecodeProviderInitiator", onlyWith = Target_BytecodeProviderInitiator.SubstituteOnlyIfPresent.class)
final class Target_BytecodeProviderInitiator {

@Alias
Expand All @@ -46,4 +47,22 @@ final class Target_BytecodeProviderInitiator {
public static BytecodeProvider buildBytecodeProvider(String providerName) {
return new org.hibernate.bytecode.internal.none.BytecodeProviderImpl();
}

static class SubstituteOnlyIfPresent implements Predicate<String> {

@Override
public boolean test(String type) {
try {
Class<?> clazz = Class.forName(type, false, getClass().getClassLoader());
clazz.getDeclaredMethod("buildBytecodeProvider", String.class);
clazz.getField("BYTECODE_PROVIDER_NAME_NONE");
clazz.getField("BYTECODE_PROVIDER_NAME_DEFAULT");
return true;
}
catch (ClassNotFoundException | NoClassDefFoundError | NoSuchMethodException | NoSuchFieldException ex) {
return false;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Args = -H:ServiceLoaderFeatureExcludeServices=org.hibernate.bytecode.spi.BytecodeProvider

0 comments on commit 9430b24

Please sign in to comment.