HHH-14385 Allow specifying a target JVM version for compatibility of generated proxies via ByteBuddy

This commit is contained in:
Sanne Grinovero 2020-12-22 16:23:10 +00:00 committed by Sanne Grinovero
parent 89f0000b62
commit 21ec1a19d1
2 changed files with 21 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import org.hibernate.proxy.ProxyConfiguration;
import org.hibernate.proxy.ProxyFactory;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.TypeCache;
import net.bytebuddy.asm.AsmVisitorWrapper.ForDeclaredMethods;
import net.bytebuddy.asm.MemberSubstitution;
@ -71,7 +72,11 @@ public final class ByteBuddyState {
private final TypeCache<TypeCache.SimpleKey> basicProxyCache;
ByteBuddyState() {
this.byteBuddy = new ByteBuddy().with( TypeValidation.DISABLED );
this( ClassFileVersion.ofThisVm( ClassFileVersion.JAVA_V8 ) );
}
ByteBuddyState(ClassFileVersion classFileVersion) {
this.byteBuddy = new ByteBuddy( classFileVersion ).with( TypeValidation.DISABLED );
this.proxyCache = new TypeCache.WithInlineExpunction<TypeCache.SimpleKey>( TypeCache.Sort.WEAK );
this.basicProxyCache = new TypeCache.WithInlineExpunction<TypeCache.SimpleKey>( TypeCache.Sort.WEAK );

View File

@ -20,6 +20,7 @@ import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyHelper;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.NamingStrategy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
@ -49,8 +50,21 @@ public class BytecodeProviderImpl implements BytecodeProvider {
private final ByteBuddyProxyHelper byteBuddyProxyHelper;
/**
* Constructs a ByteBuddy BytecodeProvider instance which attempts to auto-detect the target JVM version
* from the currently running one, with a fallback on Java 8.
*/
public BytecodeProviderImpl() {
this.byteBuddyState = new ByteBuddyState();
this( ClassFileVersion.ofThisVm( ClassFileVersion.JAVA_V8 ) );
}
/**
* Constructs a ByteBuddy BytecodeProvider instance which aims to produce code compatible
* with the specified target JVM version.
* @param targetCompatibleJVM
*/
public BytecodeProviderImpl(ClassFileVersion targetCompatibleJVM) {
this.byteBuddyState = new ByteBuddyState( targetCompatibleJVM );
this.byteBuddyProxyHelper = new ByteBuddyProxyHelper( byteBuddyState );
}