Skip to content

Commit 5533993

Browse files
committed
some minor cleanups in interceptor package
1 parent d0a0f5e commit 5533993

File tree

5 files changed

+83
-91
lines changed

5 files changed

+83
-91
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/AbstractInterceptor.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ protected AbstractInterceptor() {
1818

1919
@Override
2020
public SharedSessionContractImplementor getLinkedSession() {
21-
return this.sessionAssociation != null ? this.sessionAssociation.session : null;
21+
return sessionAssociation != null ? sessionAssociation.session : null;
2222
}
2323

2424
@Override
2525
public void setSession(SharedSessionContractImplementor session) {
2626
if ( session != null ) {
27-
this.sessionAssociation = session.getSessionAssociationMarkers();
27+
sessionAssociation = session.getSessionAssociationMarkers();
2828
}
2929
else {
3030
unsetSession();
@@ -33,31 +33,24 @@ public void setSession(SharedSessionContractImplementor session) {
3333

3434
@Override
3535
public void unsetSession() {
36-
if ( this.sessionAssociation != null ) {
36+
if ( sessionAssociation != null ) {
3737
//We shouldn't mutate the original instance as it's shared across multiple entities,
3838
//but we can get a version of it which represents the same state except it doesn't have the session set:
39-
this.sessionAssociation = this.sessionAssociation.deAssociatedCopy();
39+
sessionAssociation = sessionAssociation.deAssociatedCopy();
4040
}
4141
}
4242

4343
@Override
4444
public boolean allowLoadOutsideTransaction() {
45-
if ( this.sessionAssociation != null ) {
46-
return this.sessionAssociation.allowLoadOutsideTransaction;
47-
}
48-
else {
49-
return false;
50-
}
45+
return sessionAssociation != null
46+
&& sessionAssociation.allowLoadOutsideTransaction;
5147
}
5248

5349
@Override
5450
public String getSessionFactoryUuid() {
55-
if ( this.sessionAssociation != null ) {
56-
return this.sessionAssociation.sessionFactoryUuid;
57-
}
58-
else {
59-
return null;
60-
}
51+
return sessionAssociation != null
52+
? sessionAssociation.sessionFactoryUuid
53+
: null;
6154
}
6255

6356
/**

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementAsProxyLazinessInterceptor.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.bytecode.enhance.spi.interceptor;
66

7-
import java.util.Collections;
87
import java.util.HashSet;
98
import java.util.Set;
109

@@ -17,6 +16,7 @@
1716
import org.hibernate.type.CompositeType;
1817
import org.hibernate.type.Type;
1918

19+
import static java.util.Collections.emptySet;
2020
import static java.util.Collections.unmodifiableSet;
2121
import static org.hibernate.bytecode.enhance.spi.interceptor.BytecodeInterceptorLogging.BYTECODE_INTERCEPTOR_LOGGER;
2222
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
@@ -27,7 +27,9 @@
2727
/**
2828
* @author Steve Ebersole
2929
*/
30-
public class EnhancementAsProxyLazinessInterceptor extends AbstractInterceptor implements BytecodeLazyAttributeInterceptor {
30+
public class EnhancementAsProxyLazinessInterceptor
31+
extends AbstractInterceptor
32+
implements BytecodeLazyAttributeInterceptor {
3133

3234
private final EntityKey entityKey;
3335
private final EntityRelatedState meta;
@@ -83,7 +85,7 @@ private Object read(
8385
if ( writtenFieldNames != null && !writtenFieldNames.isEmpty() ) {
8486

8587
// enhancement has dirty-tracking available and at least one attribute was explicitly set
86-
final EntityPersister entityPersister = meta.persister;
88+
final var entityPersister = meta.persister;
8789
if ( writtenFieldNames.contains( attributeName ) ) {
8890
// the requested attribute was one of the attributes explicitly set,
8991
// we can just return the explicitly-set value
@@ -116,7 +118,7 @@ private Object read(
116118
if ( writtenAttributeValues != null ) {
117119
// here is the replaying of the explicitly set values we prepared above
118120
for ( int i = 0; i < writtenAttributeMappings.length; i++ ) {
119-
final AttributeMapping attribute = writtenAttributeMappings[i];
121+
final var attribute = writtenAttributeMappings[i];
120122
attribute.setValue(target, writtenAttributeValues[i] );
121123
if ( meta.inLineDirtyChecking ) {
122124
asSelfDirtinessTracker(target).$$_hibernate_trackChange( attribute.getAttributeName() );
@@ -130,7 +132,7 @@ private Object read(
130132

131133
private Object extractIdValue(Object target, String attributeName) {
132134
// access to the id or part of it for non-aggregated cid
133-
final CompositeType nonAggregatedCidMapper = meta.nonAggregatedCidMapper;
135+
final var nonAggregatedCidMapper = meta.nonAggregatedCidMapper;
134136
if ( nonAggregatedCidMapper == null ) {
135137
return getIdentifier();
136138
}
@@ -200,7 +202,7 @@ protected Object handleWrite(Object target, String attributeName, Object oldValu
200202
}
201203
else {
202204
final int subAttrIndex = meta.nonAggregatedCidMapper.getPropertyIndex( attributeName );
203-
final Type subAttrType = meta.nonAggregatedCidMapper.getSubtypes()[subAttrIndex];
205+
final var subAttrType = meta.nonAggregatedCidMapper.getSubtypes()[subAttrIndex];
204206
changed = ! subAttrType.isEqual( oldValue, newValue );
205207
}
206208

@@ -245,7 +247,7 @@ protected Object handleWrite(Object target, String attributeName, Object oldValu
245247

246248
@Override
247249
public Set<String> getInitializedLazyAttributeNames() {
248-
return Collections.emptySet();
250+
return emptySet();
249251
}
250252

251253
@Override
@@ -347,18 +349,18 @@ public EntityRelatedState(EntityPersister persister,
347349
tmpCollectionAttributeNames.add( propertyNames[i] );
348350
}
349351
}
350-
this.collectionAttributeNames = toSmallSet( unmodifiableSet( tmpCollectionAttributeNames ) );
352+
collectionAttributeNames = toSmallSet( unmodifiableSet( tmpCollectionAttributeNames ) );
351353
}
352354
else {
353-
this.collectionAttributeNames = Collections.emptySet();
355+
collectionAttributeNames = emptySet();
354356
}
355357

356-
this.inLineDirtyChecking = isSelfDirtinessTrackerType( persister.getMappedClass() );
358+
inLineDirtyChecking = isSelfDirtinessTrackerType( persister.getMappedClass() );
357359
// if self-dirty tracking is enabled but DynamicUpdate is not enabled then we need to
358360
// initialize the entity because the precomputed update statement contains even not
359361
// dirty properties. And so we need all the values we have to initialize. Or, if it's
360362
// versioned, we need to fetch the current version.
361-
this.initializeBeforeWrite =
363+
initializeBeforeWrite =
362364
!inLineDirtyChecking
363365
|| !persister.isDynamicUpdate()
364366
|| persister.isVersioned();

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeLoadingInterceptor.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
package org.hibernate.bytecode.enhance.spi.interceptor;
66

77
import java.util.Collection;
8-
import java.util.Collections;
98
import java.util.HashSet;
109
import java.util.Set;
1110

1211
import org.hibernate.LockMode;
1312
import org.hibernate.bytecode.enhance.spi.CollectionTracker;
1413
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
1514
import org.hibernate.collection.spi.PersistentCollection;
16-
import org.hibernate.engine.spi.SelfDirtinessTracker;
1715
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1816
import org.hibernate.engine.spi.Status;
19-
import org.hibernate.persister.entity.EntityPersister;
2017

18+
import static java.util.Collections.emptySet;
2119
import static org.hibernate.engine.internal.ManagedTypeHelper.asSelfDirtinessTracker;
2220
import static org.hibernate.engine.internal.ManagedTypeHelper.isSelfDirtinessTracker;
2321

@@ -27,7 +25,9 @@
2725
* @author Luis Barreiro
2826
* @author Steve Ebersole
2927
*/
30-
public class LazyAttributeLoadingInterceptor extends AbstractInterceptor implements BytecodeLazyAttributeInterceptor {
28+
public class LazyAttributeLoadingInterceptor
29+
extends AbstractInterceptor
30+
implements BytecodeLazyAttributeInterceptor {
3131

3232
private final Object identifier;
3333
private EntityRelatedState entityMeta;
@@ -55,7 +55,7 @@ public Object getIdentifier() {
5555
@Override
5656
protected Object handleRead(Object target, String attributeName, Object value) {
5757
if ( !isAttributeLoaded( attributeName ) ) {
58-
Object loadedValue = fetchAttribute( target, attributeName );
58+
final Object loadedValue = fetchAttribute( target, attributeName );
5959
attributeInitialized( attributeName );
6060
return loadedValue;
6161
}
@@ -79,9 +79,9 @@ protected Object loadAttribute(final Object target, final String attributeName)
7979
return EnhancementHelper.performWork(
8080
this,
8181
(session, isTemporarySession) -> {
82-
final EntityPersister persister = session.getFactory()
83-
.getMappingMetamodel()
84-
.getEntityDescriptor( getEntityName() );
82+
final var persister =
83+
session.getFactory().getMappingMetamodel()
84+
.getEntityDescriptor( getEntityName() );
8585

8686
if ( isTemporarySession ) {
8787
final Object id = persister.getIdentifier( target, session );
@@ -105,12 +105,9 @@ protected Object loadAttribute(final Object target, final String attributeName)
105105
);
106106
}
107107

108-
final LazyPropertyInitializer initializer = (LazyPropertyInitializer) persister;
109-
final Object loadedValue = initializer.initializeLazyProperty(
110-
attributeName,
111-
target,
112-
session
113-
);
108+
final var initializer = (LazyPropertyInitializer) persister;
109+
final Object loadedValue =
110+
initializer.initializeLazyProperty( attributeName, target, session );
114111

115112
takeCollectionSizeSnapshot( target, attributeName, loadedValue );
116113
return loadedValue;
@@ -136,36 +133,30 @@ public boolean hasAnyUninitializedAttributes() {
136133
if ( entityMeta.lazyFields.isEmpty() ) {
137134
return false;
138135
}
139-
140-
if ( initializedLazyFields == null ) {
136+
else if ( initializedLazyFields == null ) {
141137
return true;
142138
}
143-
144-
for ( String fieldName : entityMeta.lazyFields ) {
145-
if ( !initializedLazyFields.contains( fieldName ) ) {
146-
return true;
139+
else {
140+
for ( String fieldName : entityMeta.lazyFields ) {
141+
if ( !initializedLazyFields.contains( fieldName ) ) {
142+
return true;
143+
}
147144
}
145+
return false;
148146
}
149-
150-
return false;
151147
}
152148

153149
@Override
154150
public String toString() {
155-
return getClass().getSimpleName() + "(entityName=" + getEntityName() + " ,lazyFields=" + entityMeta.lazyFields + ')';
151+
return getClass().getSimpleName()
152+
+ "(entityName=" + getEntityName() + " ,lazyFields=" + entityMeta.lazyFields + ')';
156153
}
157154

158155
private void takeCollectionSizeSnapshot(Object target, String fieldName, Object value) {
159156
if ( value instanceof Collection<?> collection && isSelfDirtinessTracker( target ) ) {
160157
// This must be called first, so that we remember that there is a collection out there,
161158
// even if we don't know its size (see below).
162-
final SelfDirtinessTracker targetSDT = asSelfDirtinessTracker( target );
163-
CollectionTracker tracker = targetSDT.$$_hibernate_getCollectionTracker();
164-
if ( tracker == null ) {
165-
targetSDT.$$_hibernate_clearDirtyAttributes();
166-
tracker = targetSDT.$$_hibernate_getCollectionTracker();
167-
}
168-
159+
var tracker = getCollectionTracker( target );
169160
if ( value instanceof PersistentCollection<?> persistentCollection
170161
&& !persistentCollection.wasInitialized() ) {
171162
// Cannot take a snapshot of an uninitialized collection.
@@ -175,20 +166,31 @@ private void takeCollectionSizeSnapshot(Object target, String fieldName, Object
175166
}
176167
}
177168

169+
private static CollectionTracker getCollectionTracker(Object target) {
170+
final var selfDirtinessTracker = asSelfDirtinessTracker( target );
171+
final var tracker = selfDirtinessTracker.$$_hibernate_getCollectionTracker();
172+
if ( tracker == null ) {
173+
selfDirtinessTracker.$$_hibernate_clearDirtyAttributes();
174+
return selfDirtinessTracker.$$_hibernate_getCollectionTracker();
175+
}
176+
else {
177+
return tracker;
178+
}
179+
}
180+
178181
@Override
179182
public void attributeInitialized(String name) {
180-
if ( !isLazyAttribute( name ) ) {
181-
return;
182-
}
183-
if ( initializedLazyFields == null ) {
184-
initializedLazyFields = new HashSet<>();
183+
if ( isLazyAttribute( name ) ) {
184+
if ( initializedLazyFields == null ) {
185+
initializedLazyFields = new HashSet<>();
186+
}
187+
initializedLazyFields.add( name );
185188
}
186-
initializedLazyFields.add( name );
187189
}
188190

189191
@Override
190192
public Set<String> getInitializedLazyAttributeNames() {
191-
return initializedLazyFields == null ? Collections.emptySet() : initializedLazyFields;
193+
return initializedLazyFields == null ? emptySet() : initializedLazyFields;
192194
}
193195

194196
public void addLazyFieldByGraph(String fieldName) {

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributesMetadata.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static LazyAttributesMetadata from(
4848
property,
4949
isEnhanced,
5050
entityName -> {
51-
final PersistentClass entityBinding = metadata.getEntityBinding( entityName );
51+
final var entityBinding = metadata.getEntityBinding( entityName );
5252
assert entityBinding != null;
5353
return entityBinding.hasSubclasses();
5454
},
@@ -58,7 +58,7 @@ public static LazyAttributesMetadata from(
5858
final var lazyAttributeDescriptor = LazyAttributeDescriptor.from( property, i, x++ );
5959
lazyAttributeDescriptorMap.put( lazyAttributeDescriptor.getName(), lazyAttributeDescriptor );
6060

61-
final Set<String> attributeSet = fetchGroupToAttributesMap.computeIfAbsent(
61+
final var attributeSet = fetchGroupToAttributesMap.computeIfAbsent(
6262
lazyAttributeDescriptor.getFetchGroupName(),
6363
k -> new LinkedHashSet<>()
6464
);
@@ -143,8 +143,9 @@ public Set<String> getAttributesInFetchGroup(String fetchGroupName) {
143143
}
144144

145145
public List<LazyAttributeDescriptor> getFetchGroupAttributeDescriptors(String groupName) {
146-
final List<LazyAttributeDescriptor> list = new ArrayList<>();
147-
for ( String attributeName : fetchGroupToAttributeMap.get( groupName ) ) {
146+
final var attributeNames = fetchGroupToAttributeMap.get( groupName );
147+
final List<LazyAttributeDescriptor> list = new ArrayList<>( attributeNames.size() );
148+
for ( String attributeName : attributeNames ) {
148149
list.add( lazyAttributeDescriptorMap.get( attributeName ) );
149150
}
150151
return list;

0 commit comments

Comments
 (0)