@@ -124,22 +124,19 @@ private Hibernate() {
124
124
* for example, if the {@code Session} was closed
125
125
*/
126
126
public static void initialize (Object proxy ) throws HibernateException {
127
- if ( proxy == null ) {
128
- return ;
129
- }
130
-
131
- final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
132
- if ( lazyInitializer != null ) {
133
- lazyInitializer .initialize ();
134
- }
135
- else if ( proxy instanceof LazyInitializable ) {
136
- ( (LazyInitializable ) proxy ).forceInitialization ();
137
- }
138
- else if ( isPersistentAttributeInterceptable ( proxy ) ) {
139
- final PersistentAttributeInterceptor interceptor =
140
- asPersistentAttributeInterceptable ( proxy ).$$_hibernate_getInterceptor ();
141
- if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
142
- ( (EnhancementAsProxyLazinessInterceptor ) interceptor ).forceInitialize ( proxy , null );
127
+ if ( proxy != null ) {
128
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
129
+ if ( lazyInitializer != null ) {
130
+ lazyInitializer .initialize ();
131
+ }
132
+ else if ( proxy instanceof LazyInitializable lazyInitializable ) {
133
+ lazyInitializable .forceInitialization ();
134
+ }
135
+ else if ( isPersistentAttributeInterceptable ( proxy ) ) {
136
+ if ( getAttributeInterceptor ( proxy )
137
+ instanceof EnhancementAsProxyLazinessInterceptor enhancementInterceptor ) {
138
+ enhancementInterceptor .forceInitialize ( proxy , null );
139
+ }
143
140
}
144
141
}
145
142
}
@@ -158,17 +155,14 @@ public static boolean isInitialized(Object proxy) {
158
155
return !lazyInitializer .isUninitialized ();
159
156
}
160
157
else if ( isPersistentAttributeInterceptable ( proxy ) ) {
161
- final PersistentAttributeInterceptor interceptor =
162
- asPersistentAttributeInterceptable ( proxy ).$$_hibernate_getInterceptor ();
163
- if (interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
164
- return ( (EnhancementAsProxyLazinessInterceptor ) interceptor ).isInitialized ();
165
- }
166
- else {
167
- return true ;
168
- }
158
+ final boolean uninitialized =
159
+ getAttributeInterceptor ( proxy )
160
+ instanceof EnhancementAsProxyLazinessInterceptor enhancementInterceptor
161
+ && !enhancementInterceptor .isInitialized ();
162
+ return !uninitialized ;
169
163
}
170
- else if ( proxy instanceof LazyInitializable ) {
171
- return ( ( LazyInitializable ) proxy ) .wasInitialized ();
164
+ else if ( proxy instanceof LazyInitializable lazyInitializable ) {
165
+ return lazyInitializable .wasInitialized ();
172
166
}
173
167
else {
174
168
return true ;
@@ -185,8 +179,8 @@ else if ( proxy instanceof LazyInitializable ) {
185
179
* @since 6.1.1
186
180
*/
187
181
public static int size (Collection <?> collection ) {
188
- return collection instanceof PersistentCollection
189
- ? (( PersistentCollection <?>) collection ) .getSize ()
182
+ return collection instanceof PersistentCollection <?> persistentCollection
183
+ ? persistentCollection .getSize ()
190
184
: collection .size ();
191
185
}
192
186
@@ -200,8 +194,8 @@ public static int size(Collection<?> collection) {
200
194
* @since 7.0
201
195
*/
202
196
public static boolean isEmpty (Collection <?> collection ) {
203
- return collection instanceof PersistentCollection
204
- ? (( PersistentCollection <?>) collection ) .getSize () == 0
197
+ return collection instanceof PersistentCollection <?> persistentCollection
198
+ ? persistentCollection .getSize () == 0
205
199
: collection .isEmpty ();
206
200
}
207
201
@@ -215,9 +209,9 @@ public static boolean isEmpty(Collection<?> collection) {
215
209
* @since 6.1.1
216
210
*/
217
211
public static <T > boolean contains (Collection <? super T > collection , T element ) {
218
- return collection instanceof PersistentCollection
219
- ? (( PersistentCollection <?>) collection ) .elementExists (element )
220
- : collection .contains (element );
212
+ return collection instanceof PersistentCollection <?> persistentCollection
213
+ ? persistentCollection .elementExists ( element )
214
+ : collection .contains ( element );
221
215
}
222
216
223
217
/**
@@ -230,10 +224,11 @@ public static <T> boolean contains(Collection<? super T> collection, T element)
230
224
*
231
225
* @since 6.1.1
232
226
*/
227
+ @ SuppressWarnings ("unchecked" )
233
228
public static <K ,V > V get (Map <? super K , V > map , K key ) {
234
- return map instanceof PersistentCollection
235
- ? (V ) (( PersistentCollection <?>) map ) .elementByIndex (key )
236
- : map .get (key );
229
+ return map instanceof PersistentCollection <?> persistentCollection
230
+ ? (V ) persistentCollection .elementByIndex ( key )
231
+ : map .get ( key );
237
232
}
238
233
239
234
/**
@@ -246,10 +241,11 @@ public static <K,V> V get(Map<? super K, V> map, K key) {
246
241
*
247
242
* @since 6.1.1
248
243
*/
244
+ @ SuppressWarnings ("unchecked" )
249
245
public static <T > T get (List <T > list , int key ) {
250
- return list instanceof PersistentCollection
251
- ? (T ) (( PersistentCollection <?>) list ) .elementByIndex (key )
252
- : list .get (key );
246
+ return list instanceof PersistentCollection <?> persistentCollection
247
+ ? (T ) persistentCollection .elementByIndex ( key )
248
+ : list .get ( key );
253
249
}
254
250
255
251
/**
@@ -261,16 +257,11 @@ public static <T> T get(List<T> list, int key) {
261
257
*/
262
258
@ SuppressWarnings ("unchecked" )
263
259
public static <T > Class <? extends T > getClass (T proxy ) {
264
- Class <?> result ;
265
260
final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
266
- if ( lazyInitializer != null ) {
267
- result = lazyInitializer
268
- .getImplementation ()
269
- .getClass ();
270
- }
271
- else {
272
- result = proxy .getClass ();
273
- }
261
+ final Class <?> result =
262
+ lazyInitializer != null
263
+ ? lazyInitializer .getImplementation ().getClass ()
264
+ : proxy .getClass ();
274
265
return (Class <? extends T >) result ;
275
266
}
276
267
@@ -289,14 +280,11 @@ public static <T> Class<? extends T> getClass(T proxy) {
289
280
*/
290
281
@ SuppressWarnings ("unchecked" )
291
282
public static <T > Class <? extends T > getClassLazy (T proxy ) {
292
- Class <?> result ;
293
283
final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
294
- if ( lazyInitializer != null ) {
295
- result = lazyInitializer .getImplementationClass ();
296
- }
297
- else {
298
- result = proxy .getClass ();
299
- }
284
+ final Class <?> result =
285
+ lazyInitializer != null
286
+ ? lazyInitializer .getImplementationClass ()
287
+ : proxy .getClass ();
300
288
return (Class <? extends T >) result ;
301
289
}
302
290
@@ -353,15 +341,12 @@ public static boolean isPropertyInitialized(Object proxy, String attributeName)
353
341
entity = proxy ;
354
342
}
355
343
356
- if ( isPersistentAttributeInterceptable ( entity ) ) {
357
- PersistentAttributeInterceptor interceptor =
358
- asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ();
359
- if ( interceptor instanceof BytecodeLazyAttributeInterceptor ) {
360
- return ( (BytecodeLazyAttributeInterceptor ) interceptor ).isAttributeLoaded ( attributeName );
361
- }
362
- }
363
-
364
- return true ;
344
+ final boolean attributeUnloaded =
345
+ isPersistentAttributeInterceptable ( entity )
346
+ && getAttributeInterceptor ( entity )
347
+ instanceof BytecodeLazyAttributeInterceptor lazyAttributeInterceptor
348
+ && !lazyAttributeInterceptor .isAttributeLoaded ( attributeName );
349
+ return !attributeUnloaded ;
365
350
}
366
351
367
352
/**
@@ -373,19 +358,10 @@ public static boolean isPropertyInitialized(Object proxy, String attributeName)
373
358
* @param attributeName the name of a persistent attribute of the object
374
359
*/
375
360
public static void initializeProperty (Object proxy , String attributeName ) {
376
- final Object entity ;
377
361
final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
378
- if ( lazyInitializer != null ) {
379
- entity = lazyInitializer .getImplementation ();
380
- }
381
- else {
382
- entity = proxy ;
383
- }
384
-
362
+ final Object entity = lazyInitializer != null ? lazyInitializer .getImplementation () : proxy ;
385
363
if ( isPersistentAttributeInterceptable ( entity ) ) {
386
- PersistentAttributeInterceptor interceptor =
387
- asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ();
388
- interceptor .readObject ( entity , attributeName , null );
364
+ getAttributeInterceptor ( entity ).readObject ( entity , attributeName , null );
389
365
}
390
366
}
391
367
@@ -402,12 +378,7 @@ public static void initializeProperty(Object proxy, String attributeName) {
402
378
*/
403
379
public static Object unproxy (Object proxy ) {
404
380
final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
405
- if ( lazyInitializer != null ) {
406
- return lazyInitializer .getImplementation ();
407
- }
408
- else {
409
- return proxy ;
410
- }
381
+ return lazyInitializer != null ? lazyInitializer .getImplementation () : proxy ;
411
382
}
412
383
413
384
/**
@@ -444,14 +415,14 @@ public static <T> T unproxy(T proxy, Class<T> entityClass) {
444
415
*/
445
416
@ SuppressWarnings ("unchecked" )
446
417
public static <E > E createDetachedProxy (SessionFactory sessionFactory , Class <E > entityClass , Object id ) {
447
- final EntityPersister persister = sessionFactory . unwrap ( SessionFactoryImplementor . class )
448
- . getRuntimeMetamodels ( )
449
- .getMappingMetamodel ()
450
- .findEntityDescriptor (entityClass );
451
- if (persister == null ) {
418
+ final EntityPersister persister =
419
+ sessionFactory . unwrap ( SessionFactoryImplementor . class )
420
+ .getMappingMetamodel ()
421
+ .findEntityDescriptor ( entityClass );
422
+ if ( persister == null ) {
452
423
throw new UnknownEntityTypeException ("unknown entity type" );
453
424
}
454
- return (E ) persister .createProxy (id , null );
425
+ return (E ) persister .createProxy ( id , null );
455
426
}
456
427
457
428
/**
@@ -600,4 +571,8 @@ else if (collectionClass == Collection.class) {
600
571
throw new IllegalArgumentException ("illegal collection interface type" );
601
572
}
602
573
}
574
+
575
+ private static PersistentAttributeInterceptor getAttributeInterceptor (Object entity ) {
576
+ return asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ();
577
+ }
603
578
}
0 commit comments