@@ -124,22 +124,19 @@ private Hibernate() {
124124 * for example, if the {@code Session} was closed
125125 */
126126 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+ }
143140 }
144141 }
145142 }
@@ -158,17 +155,14 @@ public static boolean isInitialized(Object proxy) {
158155 return !lazyInitializer .isUninitialized ();
159156 }
160157 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 ;
169163 }
170- else if ( proxy instanceof LazyInitializable ) {
171- return ( ( LazyInitializable ) proxy ) .wasInitialized ();
164+ else if ( proxy instanceof LazyInitializable lazyInitializable ) {
165+ return lazyInitializable .wasInitialized ();
172166 }
173167 else {
174168 return true ;
@@ -185,8 +179,8 @@ else if ( proxy instanceof LazyInitializable ) {
185179 * @since 6.1.1
186180 */
187181 public static int size (Collection <?> collection ) {
188- return collection instanceof PersistentCollection
189- ? (( PersistentCollection <?>) collection ) .getSize ()
182+ return collection instanceof PersistentCollection <?> persistentCollection
183+ ? persistentCollection .getSize ()
190184 : collection .size ();
191185 }
192186
@@ -200,8 +194,8 @@ public static int size(Collection<?> collection) {
200194 * @since 7.0
201195 */
202196 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
205199 : collection .isEmpty ();
206200 }
207201
@@ -215,9 +209,9 @@ public static boolean isEmpty(Collection<?> collection) {
215209 * @since 6.1.1
216210 */
217211 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 );
221215 }
222216
223217 /**
@@ -230,10 +224,11 @@ public static <T> boolean contains(Collection<? super T> collection, T element)
230224 *
231225 * @since 6.1.1
232226 */
227+ @ SuppressWarnings ("unchecked" )
233228 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 );
237232 }
238233
239234 /**
@@ -246,10 +241,11 @@ public static <K,V> V get(Map<? super K, V> map, K key) {
246241 *
247242 * @since 6.1.1
248243 */
244+ @ SuppressWarnings ("unchecked" )
249245 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 );
253249 }
254250
255251 /**
@@ -261,16 +257,11 @@ public static <T> T get(List<T> list, int key) {
261257 */
262258 @ SuppressWarnings ("unchecked" )
263259 public static <T > Class <? extends T > getClass (T proxy ) {
264- Class <?> result ;
265260 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 ();
274265 return (Class <? extends T >) result ;
275266 }
276267
@@ -289,14 +280,11 @@ public static <T> Class<? extends T> getClass(T proxy) {
289280 */
290281 @ SuppressWarnings ("unchecked" )
291282 public static <T > Class <? extends T > getClassLazy (T proxy ) {
292- Class <?> result ;
293283 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 ();
300288 return (Class <? extends T >) result ;
301289 }
302290
@@ -353,15 +341,12 @@ public static boolean isPropertyInitialized(Object proxy, String attributeName)
353341 entity = proxy ;
354342 }
355343
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 ;
365350 }
366351
367352 /**
@@ -373,19 +358,10 @@ public static boolean isPropertyInitialized(Object proxy, String attributeName)
373358 * @param attributeName the name of a persistent attribute of the object
374359 */
375360 public static void initializeProperty (Object proxy , String attributeName ) {
376- final Object entity ;
377361 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 ;
385363 if ( isPersistentAttributeInterceptable ( entity ) ) {
386- PersistentAttributeInterceptor interceptor =
387- asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ();
388- interceptor .readObject ( entity , attributeName , null );
364+ getAttributeInterceptor ( entity ).readObject ( entity , attributeName , null );
389365 }
390366 }
391367
@@ -402,12 +378,7 @@ public static void initializeProperty(Object proxy, String attributeName) {
402378 */
403379 public static Object unproxy (Object proxy ) {
404380 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 ;
411382 }
412383
413384 /**
@@ -444,14 +415,14 @@ public static <T> T unproxy(T proxy, Class<T> entityClass) {
444415 */
445416 @ SuppressWarnings ("unchecked" )
446417 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 ) {
452423 throw new UnknownEntityTypeException ("unknown entity type" );
453424 }
454- return (E ) persister .createProxy (id , null );
425+ return (E ) persister .createProxy ( id , null );
455426 }
456427
457428 /**
@@ -600,4 +571,8 @@ else if (collectionClass == Collection.class) {
600571 throw new IllegalArgumentException ("illegal collection interface type" );
601572 }
602573 }
574+
575+ private static PersistentAttributeInterceptor getAttributeInterceptor (Object entity ) {
576+ return asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ();
577+ }
603578}
0 commit comments