Skip to content

Commit 48d0a60

Browse files
committed
code cleanups in PersistenceUtilHelper
1 parent 88a5e8a commit 48d0a60

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

hibernate-core/src/main/java/org/hibernate/jpa/internal/util/PersistenceUtilHelper.java

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public static LoadState getLoadState(Object reference) {
8888
else if ( isPersistentAttributeInterceptable( reference ) ) {
8989
return isInitialized( asPersistentAttributeInterceptable( reference ) ) ? LOADED : NOT_LOADED;
9090
}
91-
else if ( reference instanceof LazyInitializable) {
92-
return ( (LazyInitializable) reference ).wasInitialized() ? LOADED : NOT_LOADED;
91+
else if ( reference instanceof LazyInitializable lazyInitializable) {
92+
return lazyInitializable.wasInitialized() ? LOADED : NOT_LOADED;
9393
}
9494
else {
9595
return UNKNOWN;
@@ -116,7 +116,7 @@ private static BytecodeLazyAttributeInterceptor extractInterceptor(PersistentAtt
116116
* @return The LoadState
117117
*/
118118
public static LoadState isLoadedWithoutReference(Object entity, String attributeName, MetadataCache cache) {
119-
boolean sureFromUs = false;
119+
final boolean sureFromUs;
120120
final LazyInitializer lazyInitializer = extractLazyInitializer( entity );
121121
if ( lazyInitializer != null ) {
122122
if ( lazyInitializer.isUninitialized() ) {
@@ -129,54 +129,57 @@ public static LoadState isLoadedWithoutReference(Object entity, String attribute
129129
}
130130
sureFromUs = true;
131131
}
132+
else {
133+
sureFromUs = false;
134+
}
132135

133136
// we are instrumenting, but we can't assume we are the only ones
134137
if ( isPersistentAttributeInterceptable( entity ) ) {
135-
final BytecodeLazyAttributeInterceptor interceptor = extractInterceptor( asPersistentAttributeInterceptable( entity ) );
138+
final BytecodeLazyAttributeInterceptor interceptor =
139+
extractInterceptor( asPersistentAttributeInterceptable( entity ) );
136140
final boolean isInitialized = interceptor == null || interceptor.isAttributeLoaded( attributeName );
137-
LoadState state;
138-
if (isInitialized && interceptor != null) {
139-
// attributeName is loaded according to bytecode enhancement, but is it loaded as far as association?
140-
// it's ours, we can read
141-
try {
142-
state = getLoadState( getAttributeValue( entity, attributeName, cache ) );
143-
// it's ours so we know it's loaded
144-
if ( state == UNKNOWN ) {
145-
state = LOADED;
146-
}
147-
}
148-
catch (AttributeExtractionException ignore) {
149-
state = UNKNOWN;
150-
}
151-
}
152-
else if ( interceptor != null ) {
153-
state = NOT_LOADED;
154-
}
155-
else if ( sureFromUs ) {
156-
// property is loaded according to bytecode enhancement, but is it loaded as far as association?
157-
// it's ours, we can read
158-
try {
159-
state = getLoadState( getAttributeValue( entity, attributeName, cache ) );
160-
// it's ours so we know it's loaded
161-
if ( state == UNKNOWN ) {
162-
state = LOADED;
163-
}
164-
}
165-
catch (AttributeExtractionException ignore) {
166-
state = UNKNOWN;
167-
}
168-
}
169-
else {
170-
state = UNKNOWN;
171-
}
141+
return getLoadState( entity, attributeName, cache, isInitialized, interceptor, sureFromUs );
142+
}
143+
else {
144+
return UNKNOWN;
145+
}
146+
}
172147

173-
return state;
148+
private static LoadState getLoadState(
149+
Object entity, String attributeName,
150+
MetadataCache cache,
151+
boolean isInitialized,
152+
BytecodeLazyAttributeInterceptor interceptor,
153+
boolean sureFromUs) {
154+
if ( isInitialized && interceptor != null) {
155+
// attributeName is loaded according to bytecode enhancement, but is it loaded as far as association?
156+
// it's ours, we can read
157+
return getLoadState( entity, attributeName, cache );
158+
}
159+
else if ( interceptor != null ) {
160+
return NOT_LOADED;
161+
}
162+
else if ( sureFromUs ) {
163+
// property is loaded according to bytecode enhancement, but is it loaded as far as association?
164+
// it's ours, we can read
165+
return getLoadState( entity, attributeName, cache );
174166
}
175167
else {
176168
return UNKNOWN;
177169
}
178170
}
179171

172+
private static LoadState getLoadState(Object entity, String attributeName, MetadataCache cache) {
173+
try {
174+
final LoadState state = getLoadState( getAttributeValue( entity, attributeName, cache ) );
175+
// it's ours so we know it's loaded
176+
return state == UNKNOWN ? LOADED : state;
177+
}
178+
catch (AttributeExtractionException ignore) {
179+
return UNKNOWN;
180+
}
181+
}
182+
180183
/**
181184
* Is the given attribute (by name) loaded? This form must take care to not access the attribute (trigger
182185
* initialization).
@@ -328,7 +331,7 @@ public ClassMetadataCache(Class<?> clazz) {
328331
}
329332

330333
private static List<Class<?>> findClassHierarchy(Class<?> clazz) {
331-
List<Class<?>> classes = new ArrayList<>();
334+
final List<Class<?>> classes = new ArrayList<>();
332335
Class<?> current = clazz;
333336
do {
334337
classes.add( current );
@@ -374,9 +377,9 @@ private AttributeAccess buildAttributeAccess(final String attributeName) {
374377
*/
375378
private static Method getMethod(Class<?> clazz, String attributeName) {
376379
try {
377-
char[] string = attributeName.toCharArray();
380+
final char[] string = attributeName.toCharArray();
378381
string[0] = Character.toUpperCase( string[0] );
379-
String casedAttributeName = new String( string );
382+
final String casedAttributeName = new String( string );
380383
try {
381384
return clazz.getDeclaredMethod( "get" + casedAttributeName );
382385
}

0 commit comments

Comments
 (0)