HHH-13014 Expose resolveClassLoadingStrategy as spi

This commit is contained in:
Fabio Massimo Ercoli 2018-10-05 17:46:53 +02:00 committed by Sanne Grinovero
parent cced0ad568
commit a189ac3220
3 changed files with 66 additions and 47 deletions

View File

@ -13,12 +13,11 @@ import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static org.hibernate.bytecode.spi.ClassLoadingStrategyHelper.resolveClassLoadingStrategy;
import static org.hibernate.internal.CoreLogging.messageLogger;
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
@ -37,8 +36,6 @@ import net.bytebuddy.asm.MemberSubstitution;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.DynamicType.Unloaded;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.dynamic.scaffold.TypeValidation;
import net.bytebuddy.implementation.FieldAccessor;
import net.bytebuddy.implementation.MethodDelegation;
@ -56,8 +53,6 @@ public final class ByteBuddyState {
private static final CoreMessageLogger LOG = messageLogger( ByteBuddyState.class );
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final boolean DEBUG = false;
private final ByteBuddy byteBuddy;
@ -222,45 +217,6 @@ public final class ByteBuddyState {
return unloadedClass;
}
// This method is kept public static as it is also required by a test.
public static ClassLoadingStrategy<ClassLoader> resolveClassLoadingStrategy(Class<?> originalClass) {
if ( ClassInjector.UsingLookup.isAvailable() ) {
// This is only enabled for JDK 9+
Method privateLookupIn;
try {
privateLookupIn = MethodHandles.class.getMethod( "privateLookupIn", Class.class, MethodHandles.Lookup.class );
}
catch (Exception e) {
throw new HibernateException( LOG.bytecodeEnhancementFailed( originalClass.getName() ), e );
}
try {
Object privateLookup;
try {
privateLookup = privateLookupIn.invoke( null, originalClass, LOOKUP );
}
catch (InvocationTargetException exception) {
if ( exception.getCause() instanceof IllegalAccessException ) {
return new ClassLoadingStrategy.ForUnsafeInjection( originalClass.getProtectionDomain() );
}
else {
throw new HibernateException( LOG.bytecodeEnhancementFailed( originalClass.getName() ), exception.getCause() );
}
}
return ClassLoadingStrategy.UsingLookup.of( privateLookup );
}
catch (Throwable e) {
throw new HibernateException( LOG.bytecodeEnhancementFailedUnableToGetPrivateLookupFor( originalClass.getName() ), e );
}
}
else {
return new ClassLoadingStrategy.ForUnsafeInjection( originalClass.getProtectionDomain() );
}
}
private static ForDeclaredMethods getDeclaredMethodMemberSubstitution() {
// this should only be called if the security manager is enabled, thus the privileged calls
return MemberSubstitution.relaxed()

View File

@ -0,0 +1,62 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.bytecode.spi;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.hibernate.HibernateException;
import org.hibernate.internal.CoreMessageLogger;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import static org.hibernate.internal.CoreLogging.messageLogger;
public class ClassLoadingStrategyHelper {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final CoreMessageLogger LOG = messageLogger( ClassLoadingStrategyHelper.class );
public static ClassLoadingStrategy<ClassLoader> resolveClassLoadingStrategy(Class<?> originalClass) {
// This is available only for JDK 9+
if ( !ClassInjector.UsingLookup.isAvailable() ) {
return new ClassLoadingStrategy.ForUnsafeInjection( originalClass.getProtectionDomain() );
}
Method privateLookupIn;
try {
privateLookupIn = MethodHandles.class.getMethod( "privateLookupIn", Class.class, MethodHandles.Lookup.class );
}
catch (Exception e) {
throw new HibernateException( LOG.bytecodeEnhancementFailed( originalClass.getName() ), e );
}
try {
Object privateLookup;
try {
privateLookup = privateLookupIn.invoke( null, originalClass, LOOKUP );
}
catch (InvocationTargetException exception) {
if ( exception.getCause() instanceof IllegalAccessException ) {
return new ClassLoadingStrategy.ForUnsafeInjection( originalClass.getProtectionDomain() );
}
else {
throw new HibernateException( LOG.bytecodeEnhancementFailed( originalClass.getName() ), exception.getCause() );
}
}
return ClassLoadingStrategy.UsingLookup.of( privateLookup );
}
catch (Throwable e) {
throw new HibernateException( LOG.bytecodeEnhancementFailedUnableToGetPrivateLookupFor( originalClass.getName() ), e );
}
}
}

View File

@ -8,7 +8,6 @@ package org.hibernate.test.annotations.tuplizer.bytebuddysubclass;
import java.io.Serializable;
import org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.tuple.Instantiator;
@ -16,6 +15,8 @@ import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
import static org.hibernate.bytecode.spi.ClassLoadingStrategyHelper.resolveClassLoadingStrategy;
/**
* @author Florian Bien
*/
@ -43,7 +44,7 @@ public class MyEntityInstantiator implements Instantiator {
.intercept( FixedValue.value( "transformed" ) )
.make()
// we use our internal helper to get a class loading strategy suitable for the JDK used
.load( entityClass.getClassLoader(), ByteBuddyState.resolveClassLoadingStrategy( entityClass ) )
.load( entityClass.getClassLoader(), resolveClassLoadingStrategy( entityClass ) )
.getLoaded();
try {