Skip to content

Commit f6ff6fa

Browse files
committed
code cleanup in Visitors
Signed-off-by: Gavin King <[email protected]>
1 parent 763401a commit f6ff6fa

File tree

10 files changed

+128
-186
lines changed

10 files changed

+128
-186
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/AbstractVisitor.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,8 @@ boolean includeProperty(Object[] values, int i) {
7474
* to processValue().
7575
*/
7676
Object processComponent(Object component, CompositeType componentType) throws HibernateException {
77-
if (component!=null) {
78-
processValues(
79-
componentType.getPropertyValues(component, session),
80-
componentType.getSubtypes()
81-
);
77+
if ( component!=null ) {
78+
processValues( componentType.getPropertyValues(component, session), componentType.getSubtypes() );
8279
}
8380
return null;
8481
}
@@ -88,19 +85,18 @@ Object processComponent(Object component, CompositeType componentType) throws Hi
8885
* correct handler for the property type.
8986
*/
9087
final Object processValue(Object value, Type type) throws HibernateException {
91-
92-
if ( type instanceof CollectionType ) {
88+
if ( type instanceof CollectionType collectionType ) {
9389
//even process null collections
94-
return processCollection( value, (CollectionType) type );
90+
return processCollection( value, collectionType );
9591
}
96-
else if ( type instanceof EntityType ) {
97-
return processEntity( value, (EntityType) type );
92+
else if ( type instanceof EntityType entityType ) {
93+
return processEntity( value, entityType );
9894
}
99-
else if ( type instanceof ComponentType ) {
100-
return processComponent( value, (ComponentType) type );
95+
else if ( type instanceof ComponentType componentType ) {
96+
return processComponent( value, componentType );
10197
}
102-
else if ( type instanceof AnyType ) {
103-
return processComponent( value, (AnyType) type );
98+
else if ( type instanceof AnyType anyType ) {
99+
return processComponent( value, anyType );
104100
}
105101
else {
106102
return null;
@@ -111,20 +107,15 @@ else if ( type instanceof AnyType ) {
111107
* Walk the tree starting from the given entity.
112108
*
113109
*/
114-
public void process(Object object, EntityPersister persister)
115-
throws HibernateException {
116-
processEntityPropertyValues(
117-
persister.getValues( object ),
118-
persister.getPropertyTypes()
119-
);
110+
public void process(Object object, EntityPersister persister) throws HibernateException {
111+
processEntityPropertyValues( persister.getValues( object ), persister.getPropertyTypes() );
120112
}
121113

122114
/**
123115
* Visit a collection. Default superclass
124116
* implementation is a no-op.
125117
*/
126-
Object processCollection(Object collection, CollectionType type)
127-
throws HibernateException {
118+
Object processCollection(Object collection, CollectionType type) throws HibernateException {
128119
return null;
129120
}
130121

@@ -133,8 +124,7 @@ Object processCollection(Object collection, CollectionType type)
133124
* entity. Default superclass implementation is
134125
* a no-op.
135126
*/
136-
Object processEntity(Object value, EntityType entityType)
137-
throws HibernateException {
127+
Object processEntity(Object value, EntityType entityType) throws HibernateException {
138128
return null;
139129
}
140130

hibernate-core/src/main/java/org/hibernate/event/internal/DirtyCollectionSearchVisitor.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public DirtyCollectionSearchVisitor(Object entity, EventSource session, boolean[
3737
super( session );
3838
EnhancementAsProxyLazinessInterceptor interceptor = null;
3939
if ( isPersistentAttributeInterceptable( entity ) ) {
40-
PersistentAttributeInterceptor attributeInterceptor = asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
40+
PersistentAttributeInterceptor attributeInterceptor =
41+
asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
4142
if ( attributeInterceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
4243
interceptor = (EnhancementAsProxyLazinessInterceptor) attributeInterceptor;
4344
}
@@ -53,31 +54,25 @@ public boolean wasDirtyCollectionFound() {
5354
Object processCollection(Object collection, CollectionType type) throws HibernateException {
5455
if ( collection != null ) {
5556
final SessionImplementor session = getSession();
56-
final PersistentCollection<?> persistentCollection;
5757
if ( type.isArrayType() ) {
58-
persistentCollection = session.getPersistenceContextInternal().getCollectionHolder( collection );
59-
// if no array holder we found an unwrapped array (this can't occur,
60-
// because we now always call wrap() before getting to here)
61-
// return (ah==null) ? true : searchForDirtyCollections(ah, type);
62-
}
63-
else {
64-
if ( interceptor != null && !interceptor.isAttributeLoaded( type.getName() ) ) {
65-
return null; //NOTE: EARLY EXIT!
66-
}
67-
else {
68-
// if not wrapped yet, it's dirty
69-
// (this can't occur, because we now always call wrap() before getting here)
70-
// return ( ! (obj instanceof PersistentCollection) )
71-
// ? true : searchForDirtyCollections( (PersistentCollection) obj, type );
72-
persistentCollection = (PersistentCollection<?>) collection;
58+
// if no array holder we found an unwrapped array, it's dirty
59+
// (this can't occur, because we now always call wrap() before getting to here)
60+
61+
// we need to check even if it was not initialized, because of delayed adds!
62+
if ( session.getPersistenceContextInternal().getCollectionHolder( collection ).isDirty() ) {
63+
dirty = true;
7364
}
7465
}
66+
else if ( interceptor == null || interceptor.isAttributeLoaded( type.getName() ) ) {
67+
// if not wrapped yet, it's dirty
68+
// (this can't occur, because we now always call wrap() before getting here)
7569

76-
if ( persistentCollection.isDirty() ) { //we need to check even if it was not initialized, because of delayed adds!
77-
dirty = true;
70+
// we need to check even if it was not initialized, because of delayed adds!
71+
if ( ((PersistentCollection<?>) collection).isDirty() ) {
72+
dirty = true;
73+
}
7874
}
7975
}
80-
8176
return null;
8277
}
8378

hibernate-core/src/main/java/org/hibernate/event/internal/EvictVisitor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,21 @@ Object processCollection(Object collection, CollectionType type) throws Hibernat
4242
if ( collection != null ) {
4343
evictCollection( collection, type );
4444
}
45-
4645
return null;
4746
}
4847

4948
public void evictCollection(Object value, CollectionType type) {
50-
final PersistentCollection<?> collection;
5149
final EventSource session = getSession();
50+
final PersistentCollection<?> collection;
5251
if ( type.hasHolder() ) {
53-
collection = session.getPersistenceContextInternal().removeCollectionHolder(value);
52+
collection = session.getPersistenceContextInternal().removeCollectionHolder( value );
5453
}
55-
else if ( value instanceof PersistentCollection ) {
56-
collection = (PersistentCollection<?>) value;
54+
else if ( value instanceof PersistentCollection<?> persistentCollection ) {
55+
collection = persistentCollection;
5756
}
5857
else if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
59-
final Object keyOfOwner = type.getKeyOfOwner( owner, session );
60-
collection = (PersistentCollection<?>) type.getCollection( keyOfOwner, session, owner, false );
58+
collection = (PersistentCollection<?>)
59+
type.getCollection( type.getKeyOfOwner( owner, session ), session, owner, false );
6160
}
6261
else {
6362
return; //EARLY EXIT!

hibernate-core/src/main/java/org/hibernate/event/internal/FlushVisitor.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import org.hibernate.HibernateException;
1010
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
1111
import org.hibernate.collection.spi.PersistentCollection;
12-
import org.hibernate.engine.internal.Collections;
1312
import org.hibernate.event.spi.EventSource;
1413
import org.hibernate.type.CollectionType;
1514

15+
import static org.hibernate.engine.internal.Collections.processReachableCollection;
16+
1617
/**
1718
* Process collections reachable from an entity. This
1819
* visitor assumes that wrap was already performed for
@@ -29,33 +30,32 @@ public FlushVisitor(EventSource session, Object owner) {
2930
}
3031

3132
Object processCollection(Object collection, CollectionType type) throws HibernateException {
32-
3333
if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
3434
return null;
3535
}
36-
37-
if ( collection != null ) {
36+
else if ( collection != null ) {
3837
final EventSource session = getSession();
39-
final PersistentCollection<?> coll;
38+
final PersistentCollection<?> persistentCollection;
4039
if ( type.hasHolder() ) {
41-
coll = session.getPersistenceContextInternal().getCollectionHolder(collection);
40+
persistentCollection = session.getPersistenceContextInternal().getCollectionHolder( collection );
4241
}
4342
else if ( collection == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
4443
final Object keyOfOwner = type.getKeyOfOwner( owner, session );
45-
coll = (PersistentCollection<?>) type.getCollection( keyOfOwner, session, owner, Boolean.FALSE );
44+
persistentCollection = (PersistentCollection<?>)
45+
type.getCollection( keyOfOwner, session, owner, Boolean.FALSE );
4646
}
47-
else if ( collection instanceof PersistentCollection ) {
48-
coll = (PersistentCollection<?>) collection;
47+
else if ( collection instanceof PersistentCollection<?> wrapper ) {
48+
persistentCollection = wrapper;
4949
}
5050
else {
5151
return null;
5252
}
53-
54-
Collections.processReachableCollection( coll, type, owner, session );
53+
processReachableCollection( persistentCollection, type, owner, session );
54+
return null;
55+
}
56+
else {
57+
return null;
5558
}
56-
57-
return null;
58-
5959
}
6060

6161
@Override

hibernate-core/src/main/java/org/hibernate/event/internal/OnLockVisitor.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515

1616
/**
1717
* When a transient entity is passed to lock(), we must inspect all its collections and
18-
* 1. associate any uninitialized PersistentCollections with this session
19-
* 2. associate any initialized PersistentCollections with this session, using the
20-
* existing snapshot
21-
* 3. throw an exception for each "new" collection
18+
* <ol>
19+
* <li> associate any uninitialized PersistentCollections with this session
20+
* <li> associate any initialized PersistentCollections with this session, using the
21+
* existing snapshot
22+
* <li> throw an exception for each "new" collection
23+
* </ol>
2224
*
2325
* @author Gavin King
2426
*/
@@ -35,13 +37,10 @@ public Object processCollection(Object collection, CollectionType type) throws H
3537
}
3638

3739
final SessionImplementor session = getSession();
38-
final CollectionPersister persister = session.getFactory()
39-
.getRuntimeMetamodels()
40-
.getMappingMetamodel()
41-
.getCollectionDescriptor( type.getRole() );
42-
43-
if ( collection instanceof PersistentCollection ) {
44-
final PersistentCollection<?> persistentCollection = (PersistentCollection<?>) collection;
40+
final CollectionPersister persister =
41+
session.getFactory().getMappingMetamodel()
42+
.getCollectionDescriptor( type.getRole() );
43+
if ( collection instanceof PersistentCollection<?> persistentCollection ) {
4544
if ( persistentCollection.setCurrentSession( session ) ) {
4645
if ( isOwnerUnchanged( persister, extractCollectionKeyFromOwner( persister ), persistentCollection ) ) {
4746
// a "detached" collection that originally belonged to the same entity
@@ -67,7 +66,6 @@ public Object processCollection(Object collection, CollectionType type) throws H
6766
//TODO: or an array!! we can't lock objects with arrays now??
6867
throw new HibernateException( "re-associated object has dirty collection reference (or an array)" );
6968
}
70-
7169
return null;
7270
}
7371

hibernate-core/src/main/java/org/hibernate/event/internal/OnReplicateVisitor.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
/**
1616
* When an entity is passed to replicate(), and there is an existing row, we must
1717
* inspect all its collections and
18-
* 1. associate any uninitialized PersistentCollections with this session
19-
* 2. associate any initialized PersistentCollections with this session, using the
20-
* existing snapshot
21-
* 3. execute a collection removal (SQL DELETE) for each null collection property
22-
* or "new" collection
23-
*
18+
* <ol>
19+
* <li> associate any uninitialized PersistentCollections with this session
20+
* <li> associate any initialized PersistentCollections with this session, using the
21+
* existing snapshot
22+
* <li> execute a collection removal (SQL DELETE) for each null collection property
23+
* or "new" collection
24+
*</ol>
2425
* @author Gavin King
2526
*/
2627
public class OnReplicateVisitor extends ReattachVisitor {
@@ -39,33 +40,28 @@ public Object processCollection(Object collection, CollectionType type) throws H
3940
}
4041

4142
final EventSource session = getSession();
42-
final CollectionPersister persister = session.getFactory()
43-
.getRuntimeMetamodels()
44-
.getMappingMetamodel()
45-
.getCollectionDescriptor( type.getRole() );
46-
43+
final CollectionPersister persister =
44+
session.getFactory().getMappingMetamodel()
45+
.getCollectionDescriptor( type.getRole() );
4746
if ( isUpdate ) {
4847
removeCollection( persister, extractCollectionKeyFromOwner( persister ), session );
4948
}
50-
if ( collection instanceof PersistentCollection ) {
51-
final PersistentCollection<?> wrapper = (PersistentCollection<?>) collection;
52-
wrapper.setCurrentSession( session );
53-
if ( wrapper.wasInitialized() ) {
54-
session.getPersistenceContextInternal().addNewCollection( persister, wrapper );
49+
if ( collection instanceof PersistentCollection<?> persistentCollection ) {
50+
persistentCollection.setCurrentSession( session );
51+
if ( persistentCollection.wasInitialized() ) {
52+
session.getPersistenceContextInternal().addNewCollection( persister, persistentCollection );
5553
}
5654
else {
57-
reattachCollection( wrapper, type );
55+
reattachCollection( persistentCollection, type );
5856
}
5957
}
6058
// else {
61-
// otherwise a null or brand new collection
59+
// otherwise a null or brand-new collection
6260
// this will also (inefficiently) handle arrays, which
6361
// have no snapshot, so we can't do any better
6462
//processArrayOrNewCollection(collection, type);
6563
// }
66-
6764
return null;
68-
6965
}
7066

7167
}

hibernate-core/src/main/java/org/hibernate/event/internal/OnUpdateVisitor.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,37 @@ public OnUpdateVisitor(EventSource session, Object key, Object owner) {
3030

3131
@Override
3232
Object processCollection(Object collection, CollectionType type) throws HibernateException {
33-
3433
if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
3534
return null;
3635
}
3736

3837
final EventSource session = getSession();
39-
40-
final CollectionPersister persister = session.getFactory()
41-
.getRuntimeMetamodels()
42-
.getMappingMetamodel()
43-
.getCollectionDescriptor( type.getRole() );
44-
38+
final CollectionPersister persister =
39+
session.getFactory().getMappingMetamodel()
40+
.getCollectionDescriptor( type.getRole() );
4541
final Object collectionKey = extractCollectionKeyFromOwner( persister );
46-
if ( ( collection instanceof PersistentCollection ) ) {
47-
PersistentCollection<?> wrapper = (PersistentCollection<?>) collection;
48-
if ( wrapper.setCurrentSession(session) ) {
42+
if ( collection instanceof PersistentCollection<?> persistentCollection ) {
43+
if ( persistentCollection.setCurrentSession( session ) ) {
4944
//a "detached" collection!
50-
if ( !isOwnerUnchanged( persister, collectionKey, wrapper ) ) {
45+
if ( !isOwnerUnchanged( persister, collectionKey, persistentCollection ) ) {
5146
// if the collection belonged to a different entity,
5247
// clean up the existing state of the collection
5348
removeCollection( persister, collectionKey, session );
5449
}
55-
reattachCollection(wrapper, type);
50+
reattachCollection( persistentCollection, type );
5651
}
5752
else {
5853
// a collection loaded in the current session
5954
// can not possibly be the collection belonging
6055
// to the entity passed to update()
61-
removeCollection(persister, collectionKey, session);
56+
removeCollection( persister, collectionKey, session );
6257
}
6358
}
6459
else {
6560
// null or brand-new collection
6661
// this will also (inefficiently) handle arrays, which have
6762
// no snapshot, so we can't do any better
68-
removeCollection(persister, collectionKey, session);
63+
removeCollection( persister, collectionKey, session );
6964
}
7065

7166
return null;

0 commit comments

Comments
 (0)