From 016625eb5d0bc1a64980c7d45320cb2a7e574dba Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 22 Dec 2020 16:23:10 +0000 Subject: [PATCH] HHH-14385 Allow specifying a target JVM version for compatibility of generated proxies via ByteBuddy --- .../internal/bytebuddy/ByteBuddyState.java | 7 ++++++- .../internal/bytebuddy/BytecodeProviderImpl.java | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java index 97f768d4c4..4894848cd9 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java @@ -30,6 +30,7 @@ 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 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.Sort.WEAK ); this.basicProxyCache = new TypeCache.WithInlineExpunction( TypeCache.Sort.WEAK ); diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java index 15f4c1ecc8..814d8c293b 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/BytecodeProviderImpl.java @@ -20,6 +20,7 @@ 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 ); }