44 */
55package org .hibernate .bytecode .internal .bytebuddy ;
66
7- import java .io .File ;
8- import java .io .IOException ;
97import java .lang .invoke .MethodHandles ;
10- import java .lang .reflect .Method ;
118import java .util .ArrayList ;
129import java .util .List ;
1310import java .util .function .BiFunction ;
1714import net .bytebuddy .description .type .TypeDescription ;
1815import org .hibernate .HibernateException ;
1916import org .hibernate .bytecode .enhance .internal .bytebuddy .EnhancerImplConstants ;
20- import org .hibernate .bytecode .enhance .spi .EnhancerConstants ;
2117import org .hibernate .bytecode .spi .BasicProxyFactory ;
2218import org .hibernate .engine .spi .PrimeAmongSecondarySupertypes ;
2319import org .hibernate .proxy .ProxyConfiguration ;
4642import static net .bytebuddy .matcher .ElementMatchers .not ;
4743import static net .bytebuddy .matcher .ElementMatchers .returns ;
4844import static net .bytebuddy .matcher .ElementMatchers .takesNoArguments ;
49- import static org .hibernate .internal .CoreMessageLogger .CORE_LOGGER ;
45+ import static org .hibernate .bytecode .enhance .spi .EnhancerConstants .PERSISTENT_FIELD_READER_PREFIX ;
46+ import static org .hibernate .bytecode .enhance .spi .EnhancerConstants .PERSISTENT_FIELD_WRITER_PREFIX ;
5047
5148/**
5249 * A utility to hold all ByteBuddy related state, as in the current version of
@@ -57,8 +54,6 @@ public final class ByteBuddyState {
5754
5855 private static final MethodHandles .Lookup LOOKUP = MethodHandles .lookup ();
5956
60- private static final boolean DEBUG = false ;
61-
6257 private final ByteBuddy byteBuddy ;
6358
6459 private final ProxyDefinitionHelpers proxyDefinitionHelpers = new ProxyDefinitionHelpers ();
@@ -79,9 +74,9 @@ public ByteBuddyState() {
7974 }
8075
8176 ByteBuddyState (ClassFileVersion classFileVersion ) {
82- this . byteBuddy = new ByteBuddy ( classFileVersion ).with ( TypeValidation .DISABLED );
83- this . proxyCache = new TypeCache ( TypeCache .Sort .WEAK );
84- this . basicProxyCache = new TypeCache ( TypeCache .Sort .WEAK );
77+ byteBuddy = new ByteBuddy ( classFileVersion ).with ( TypeValidation .DISABLED );
78+ proxyCache = new TypeCache <> ( TypeCache .Sort .WEAK );
79+ basicProxyCache = new TypeCache <> ( TypeCache .Sort .WEAK );
8580 }
8681
8782 /**
@@ -166,12 +161,9 @@ public Class<?> load(Class<?> referenceClass, Function<ByteBuddy, DynamicType.Bu
166161 */
167162 public byte [] rewrite (TypePool typePool , String className ,
168163 Function <ByteBuddy , DynamicType .Builder <?>> rewriteClassFunction ) {
169- DynamicType .Builder <?> builder = rewriteClassFunction .apply ( byteBuddy );
170- if ( builder == null ) {
171- return null ;
172- }
164+ var builder = rewriteClassFunction .apply ( byteBuddy );
165+ return builder == null ? null : make ( typePool , builder ).getBytes ();
173166
174- return make ( typePool , builder ).getBytes ();
175167 }
176168
177169 /**
@@ -208,7 +200,7 @@ void clearState() {
208200 */
209201 public Class <?> load (Class <?> referenceClass , String className , BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeClassFunction ) {
210202 try {
211- Class <?> result = referenceClass .getClassLoader ().loadClass (className );
203+ final var result = referenceClass .getClassLoader ().loadClass ( className );
212204 if ( result .getClassLoader () == referenceClass .getClassLoader () ) {
213205 return result ;
214206 }
@@ -218,10 +210,8 @@ public Class<?> load(Class<?> referenceClass, String className, BiFunction<ByteB
218210 }
219211 try {
220212 return make ( makeClassFunction .apply ( byteBuddy , new FixedNamingStrategy ( className ) ) )
221- .load (
222- referenceClass .getClassLoader (),
223- resolveClassLoadingStrategy ( referenceClass )
224- )
213+ .load ( referenceClass .getClassLoader (),
214+ resolveClassLoadingStrategy ( referenceClass ) )
225215 .getLoaded ();
226216 }
227217 catch (LinkageError e ) {
@@ -240,10 +230,8 @@ private Class<?> load(Class<?> referenceClass, TypeCache<TypeCache.SimpleKey> ca
240230 referenceClass .getClassLoader (),
241231 cacheKey ,
242232 () -> make ( makeProxyFunction .apply ( byteBuddy ) )
243- .load (
244- referenceClass .getClassLoader (),
245- resolveClassLoadingStrategy ( referenceClass )
246- )
233+ .load ( referenceClass .getClassLoader (),
234+ resolveClassLoadingStrategy ( referenceClass ) )
247235 .getLoaded (),
248236 cache
249237 );
@@ -262,23 +250,7 @@ private Unloaded<?> make(DynamicType.Builder<?> builder) {
262250 }
263251
264252 private Unloaded <?> make (TypePool typePool , DynamicType .Builder <?> builder ) {
265- Unloaded <?> unloadedClass ;
266- if ( typePool != null ) {
267- unloadedClass = builder .make ( typePool );
268- }
269- else {
270- unloadedClass = builder .make ();
271- }
272-
273- if ( DEBUG ) {
274- try {
275- unloadedClass .saveIn ( new File ( System .getProperty ( "java.io.tmpdir" ) + "/bytebuddy/" ) );
276- }
277- catch (IOException e ) {
278- CORE_LOGGER .warn ( "Unable to save generated class %1$s" , unloadedClass .getTypeDescription ().getName (), e );
279- }
280- }
281- return unloadedClass ;
253+ return typePool == null ? builder .make () : builder .make ( typePool );
282254 }
283255
284256 public EnhancerImplConstants getEnhancerConstants () {
@@ -298,27 +270,38 @@ public static class ProxyDefinitionHelpers {
298270 private final FieldAccessor .PropertyConfigurable interceptorFieldAccessor ;
299271
300272 private ProxyDefinitionHelpers () {
301- this .groovyGetMetaClassFilter = isSynthetic ().and ( named ( "getMetaClass" )
302- .and ( returns ( td -> "groovy.lang.MetaClass" .equals ( td .getName () ) ) ) );
303- this .virtualNotFinalizerFilter = isVirtual ().and ( not ( isFinalizer () ) );
304- this .proxyNonInterceptedMethodFilter = nameStartsWith ( "$$_hibernate_" ).and ( isVirtual () )
305- // HHH-15090: Don't apply extended enhancement reader/writer methods to the proxy;
306- // those need to be executed on the actual entity.
307- .and ( not ( nameStartsWith ( EnhancerConstants .PERSISTENT_FIELD_READER_PREFIX ) ) )
308- .and ( not ( nameStartsWith ( EnhancerConstants .PERSISTENT_FIELD_WRITER_PREFIX ) ) );
273+ groovyGetMetaClassFilter =
274+ isSynthetic ().and ( named ( "getMetaClass" )
275+ .and ( returns ( td -> "groovy.lang.MetaClass" .equals ( td .getName () ) ) ) );
276+ virtualNotFinalizerFilter =
277+ isVirtual ().and ( not ( isFinalizer () ) );
278+ proxyNonInterceptedMethodFilter =
279+ nameStartsWith ( "$$_hibernate_" ).and ( isVirtual () )
280+ // HHH-15090: Don't apply extended enhancement reader/writer methods to the proxy;
281+ // those need to be executed on the actual entity.
282+ .and ( not ( nameStartsWith ( PERSISTENT_FIELD_READER_PREFIX ) ) )
283+ .and ( not ( nameStartsWith ( PERSISTENT_FIELD_WRITER_PREFIX ) ) );
309284
310285 // Populate the toFullyIgnore list
311- for ( Method m : PrimeAmongSecondarySupertypes .class .getMethods () ) {
286+ for ( var method : PrimeAmongSecondarySupertypes .class .getMethods () ) {
312287 //We need to ignore both the match of each default method on PrimeAmongSecondarySupertypes
313- toFullyIgnore .add ( isDeclaredBy ( PrimeAmongSecondarySupertypes .class ).and ( named ( m .getName () ) ).and ( takesNoArguments () ) );
288+ toFullyIgnore .add (
289+ isDeclaredBy ( PrimeAmongSecondarySupertypes .class )
290+ .and ( named ( method .getName () ) )
291+ .and ( takesNoArguments () ) );
314292 //And the override in the interface it belongs to - which we happen to have in the return type
315- toFullyIgnore .add ( isDeclaredBy ( m .getReturnType () ).and ( named ( m .getName () ) ).and ( takesNoArguments () ) );
293+ toFullyIgnore .add (
294+ isDeclaredBy ( method .getReturnType () )
295+ .and ( named ( method .getName () ) )
296+ .and ( takesNoArguments () ) );
316297 }
317298
318- this .delegateToInterceptorDispatcherMethodDelegation = MethodDelegation .to ( ProxyConfiguration .InterceptorDispatcher .class );
299+ delegateToInterceptorDispatcherMethodDelegation =
300+ MethodDelegation .to ( ProxyConfiguration .InterceptorDispatcher .class );
319301
320- this .interceptorFieldAccessor = FieldAccessor .ofField ( ProxyConfiguration .INTERCEPTOR_FIELD_NAME )
321- .withAssigner ( Assigner .DEFAULT , Assigner .Typing .DYNAMIC );
302+ interceptorFieldAccessor =
303+ FieldAccessor .ofField ( ProxyConfiguration .INTERCEPTOR_FIELD_NAME )
304+ .withAssigner ( Assigner .DEFAULT , Assigner .Typing .DYNAMIC );
322305 }
323306
324307 public ElementMatcher <? super MethodDescription > getGroovyGetMetaClassFilter () {
@@ -342,8 +325,8 @@ public FieldAccessor.PropertyConfigurable getInterceptorFieldAccessor() {
342325 }
343326
344327 public DynamicType .Builder <?> appendIgnoreAlsoAtEnd (DynamicType .Builder <?> builder ) {
345- for ( ElementMatcher <? super MethodDescription > m : toFullyIgnore ) {
346- builder = builder .ignoreAlso ( m );
328+ for ( var elementMatcher : toFullyIgnore ) {
329+ builder = builder .ignoreAlso ( elementMatcher );
347330 }
348331 return builder ;
349332 }
0 commit comments