Skip to content

Commit 76c4b3f

Browse files
committed
some cleanups in SessionFactoryImpl and LoadQueryInfluencers
make checker framework happy with CollectionHelper calls
1 parent 0d58851 commit 76c4b3f

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

hibernate-core/src/main/java/org/hibernate/engine/spi/LoadQueryInfluencers.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public LoadQueryInfluencers(SessionFactoryImplementor sessionFactory, SessionCre
7474
batchSize = options.getDefaultBatchFetchSize();
7575
subselectFetchEnabled = options.isSubselectFetchEnabled();
7676
effectiveEntityGraph = new EffectiveEntityGraph();
77-
for ( FilterDefinition filterDefinition : sessionFactory.getAutoEnabledFilters() ) {
78-
final FilterImpl filter = new FilterImpl( filterDefinition );
77+
for ( var filterDefinition : sessionFactory.getAutoEnabledFilters() ) {
78+
final var filter = new FilterImpl( filterDefinition );
7979
if ( enabledFilters == null ) {
8080
enabledFilters = new TreeMap<>();
8181
}
@@ -84,7 +84,7 @@ public LoadQueryInfluencers(SessionFactoryImplementor sessionFactory, SessionCre
8484
}
8585

8686
public EffectiveEntityGraph applyEntityGraph(@Nullable RootGraphImplementor<?> rootGraph, @Nullable GraphSemantic graphSemantic) {
87-
final EffectiveEntityGraph effectiveEntityGraph = getEffectiveEntityGraph();
87+
final var effectiveEntityGraph = getEffectiveEntityGraph();
8888
if ( graphSemantic != null ) {
8989
if ( rootGraph == null ) {
9090
throw new IllegalArgumentException( "Graph semantic specified, but no RootGraph was supplied" );
@@ -141,13 +141,13 @@ public boolean hasEnabledFilters() {
141141
}
142142

143143
public Map<String,Filter> getEnabledFilters() {
144-
final TreeMap<String, Filter> enabledFilters = this.enabledFilters;
144+
final var enabledFilters = this.enabledFilters;
145145
if ( enabledFilters == null ) {
146146
return emptyMap();
147147
}
148148
else {
149149
// First, validate all the enabled filters...
150-
for ( Filter filter : enabledFilters.values() ) {
150+
for ( var filter : enabledFilters.values() ) {
151151
//TODO: this implementation has bad performance
152152
filter.validate();
153153
}
@@ -187,15 +187,15 @@ public Object getFilterParameterValue(String filterParameterName) {
187187
if ( enabledFilters == null ) {
188188
throw new IllegalArgumentException( "Filter [" + parsed[0] + "] currently not enabled" );
189189
}
190-
final FilterImpl filter = (FilterImpl) enabledFilters.get( parsed[0] );
190+
final var filter = (FilterImpl) enabledFilters.get( parsed[0] );
191191
if ( filter == null ) {
192192
throw new IllegalArgumentException( "Filter [" + parsed[0] + "] currently not enabled" );
193193
}
194194
return filter.getParameter( parsed[1] );
195195
}
196196

197197
public static String [] parseFilterParameterName(String filterParameterName) {
198-
int dot = filterParameterName.lastIndexOf( '.' );
198+
final int dot = filterParameterName.lastIndexOf( '.' );
199199
if ( dot <= 0 ) {
200200
throw new IllegalArgumentException(
201201
"Invalid filter-parameter name format [" + filterParameterName + "]; expecting {filter-name}.{param-name}"
@@ -246,14 +246,10 @@ public void disableFetchProfile(String name) throws UnknownProfileException {
246246
@Internal
247247
public @Nullable HashSet<String> adjustFetchProfiles(
248248
@Nullable Set<String> disabledFetchProfiles, @Nullable Set<String> enabledFetchProfiles) {
249-
final HashSet<String> currentEnabledFetchProfileNames = this.enabledFetchProfileNames;
250-
final HashSet<String> oldFetchProfiles;
251-
if ( currentEnabledFetchProfileNames == null || currentEnabledFetchProfileNames.isEmpty() ) {
252-
oldFetchProfiles = null;
253-
}
254-
else {
255-
oldFetchProfiles = new HashSet<>( currentEnabledFetchProfileNames );
256-
}
249+
final var oldFetchProfiles =
250+
enabledFetchProfileNames != null && !enabledFetchProfileNames.isEmpty()
251+
? new HashSet<>( enabledFetchProfileNames )
252+
: null;
257253
if ( disabledFetchProfiles != null && enabledFetchProfileNames != null ) {
258254
enabledFetchProfileNames.removeAll( disabledFetchProfiles );
259255
}
@@ -292,7 +288,7 @@ public void setBatchSize(int batchSize) {
292288
}
293289

294290
public int effectiveBatchSize(CollectionPersister persister) {
295-
int persisterBatchSize = persister.getBatchSize();
291+
final int persisterBatchSize = persister.getBatchSize();
296292
// persister-specific batch size overrides global setting
297293
// (note that due to legacy, -1 means no explicit setting)
298294
return persisterBatchSize >= 0 ? persisterBatchSize : batchSize;
@@ -303,7 +299,7 @@ public boolean effectivelyBatchLoadable(CollectionPersister persister) {
303299
}
304300

305301
public int effectiveBatchSize(EntityPersister persister) {
306-
int persisterBatchSize = persister.getBatchSize();
302+
final int persisterBatchSize = persister.getBatchSize();
307303
// persister-specific batch size overrides global setting
308304
// (note that due to legacy, -1 means no explicit setting)
309305
return persisterBatchSize >= 0 ? persisterBatchSize : batchSize;
@@ -331,7 +327,8 @@ private boolean isSubselectFetchEnabledInProfile(CollectionPersister persister)
331327
if ( hasEnabledFetchProfiles() ) {
332328
for ( String profile : getEnabledFetchProfileNames() ) {
333329
final FetchProfile fetchProfile =
334-
persister.getFactory().getSqlTranslationEngine().getFetchProfile( profile ) ;
330+
persister.getFactory().getSqlTranslationEngine()
331+
.getFetchProfile( profile ) ;
335332
if ( fetchProfile != null ) {
336333
final Fetch fetch = fetchProfile.getFetchByRole( persister.getRole() );
337334
if ( fetch != null && fetch.getMethod() == SUBSELECT) {

hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.Serial;
1212
import java.sql.Connection;
1313
import java.util.ArrayList;
14+
import java.util.Collection;
1415
import java.util.HashMap;
1516
import java.util.HashSet;
1617
import java.util.List;
@@ -93,7 +94,6 @@
9394
import org.hibernate.metamodel.spi.RuntimeMetamodelsImplementor;
9495
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
9596
import org.hibernate.proxy.EntityNotFoundDelegate;
96-
import org.hibernate.proxy.LazyInitializer;
9797
import org.hibernate.query.internal.QueryEngineImpl;
9898
import org.hibernate.query.named.NamedObjectRepository;
9999
import org.hibernate.query.spi.QueryEngine;
@@ -192,7 +192,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
192192
private final transient CurrentSessionContext currentSessionContext;
193193

194194
private final transient Map<String, FilterDefinition> filters;
195-
private final transient java.util.Collection<FilterDefinition> autoEnabledFilters = new HashSet<>();
195+
private final transient Collection<FilterDefinition> autoEnabledFilters = new ArrayList<>();
196196
private final transient JavaType<Object> tenantIdentifierJavaType;
197197

198198
private final transient EventListenerGroups eventListenerGroups;
@@ -251,19 +251,11 @@ public SessionFactoryImpl(
251251

252252
filters = new HashMap<>( bootMetamodel.getFilterDefinitions() );
253253

254-
final var tenantFilter = filters.get( TenantIdBinder.FILTER_NAME );
255-
if ( tenantFilter == null ) {
256-
tenantIdentifierJavaType = options.getDefaultTenantIdentifierJavaType();
257-
}
258-
else {
259-
final var jdbcMapping = tenantFilter.getParameterJdbcMapping( TenantIdBinder.PARAMETER_NAME );
260-
assert jdbcMapping != null;
261-
//noinspection unchecked
262-
tenantIdentifierJavaType = jdbcMapping.getJavaTypeDescriptor();
263-
}
264-
for ( var filterEntry : filters.entrySet() ) {
265-
if ( filterEntry.getValue().isAutoEnabled() ) {
266-
autoEnabledFilters.add( filterEntry.getValue() );
254+
tenantIdentifierJavaType = tenantIdentifierType( options );
255+
256+
for ( var filter : filters.values() ) {
257+
if ( filter.isAutoEnabled() ) {
258+
autoEnabledFilters.add( filter );
267259
}
268260
}
269261

@@ -357,6 +349,19 @@ public SessionFactoryImpl(
357349
}
358350
}
359351

352+
private JavaType<Object> tenantIdentifierType(SessionFactoryOptions options) {
353+
final var tenantFilter = filters.get( TenantIdBinder.FILTER_NAME );
354+
if ( tenantFilter == null ) {
355+
return options.getDefaultTenantIdentifierJavaType();
356+
}
357+
else {
358+
final var jdbcMapping = tenantFilter.getParameterJdbcMapping( TenantIdBinder.PARAMETER_NAME );
359+
assert jdbcMapping != null;
360+
//noinspection unchecked
361+
return jdbcMapping.getJavaTypeDescriptor();
362+
}
363+
}
364+
360365
private EventMonitor loadEventMonitor() {
361366
final var eventMonitors = classLoaderService.loadJavaServices( EventMonitor.class );
362367
return eventMonitors.isEmpty() ? new EmptyEventMonitor() : eventMonitors.iterator().next();
@@ -642,36 +647,33 @@ public Session createEntityManager() {
642647
return buildEntityManager( SYNCHRONIZED, null );
643648
}
644649

645-
private <K,V> Session buildEntityManager(final SynchronizationType synchronizationType, final Map<K,V> map) {
650+
private Session buildEntityManager(SynchronizationType synchronizationType, Map<?,?> map) {
646651
assert status != Status.CLOSED;
647652

648-
SessionBuilderImplementor builder = withOptions();
653+
var builder = withOptions();
649654
builder.autoJoinTransactions( synchronizationType == SYNCHRONIZED );
650655

651656
if ( map != null ) {
652-
//noinspection SuspiciousMethodCalls
653657
final Object tenantIdHint = map.get( HINT_TENANT_ID );
654658
if ( tenantIdHint != null ) {
655-
builder = (SessionBuilderImplementor) builder.tenantIdentifier( tenantIdHint );
659+
builder = builder.tenantIdentifier( tenantIdHint );
656660
}
657661
}
658662

659-
final Session session = builder.openSession();
663+
final var session = builder.openSession();
660664
if ( map != null ) {
661-
for ( Map.Entry<K, V> o : map.entrySet() ) {
662-
final K key = o.getKey();
663-
if ( key instanceof String string ) {
665+
for ( var entry : map.entrySet() ) {
666+
if ( entry.getKey() instanceof String string ) {
664667
if ( !HINT_TENANT_ID.equals( string ) ) {
665-
session.setProperty( string, o.getValue() );
668+
session.setProperty( string, entry.getValue() );
666669
}
667670
}
668671
}
669672
}
670673
return session;
671674
}
672675

673-
@Override @SuppressWarnings("unchecked")
674-
public Session createEntityManager(Map map) {
676+
public Session createEntityManager(Map<?,?> map) {
675677
validateNotClosed();
676678
return buildEntityManager( SYNCHRONIZED, map );
677679
}
@@ -695,8 +697,8 @@ private void errorIfResourceLocalDueToExplicitSynchronizationType() {
695697
}
696698
}
697699

698-
@Override @SuppressWarnings("unchecked")
699-
public Session createEntityManager(SynchronizationType synchronizationType, Map map) {
700+
@Override
701+
public Session createEntityManager(SynchronizationType synchronizationType, Map<?,?> map) {
700702
validateNotClosed();
701703
errorIfResourceLocalDueToExplicitSynchronizationType();
702704
return buildEntityManager( synchronizationType, map );
@@ -737,7 +739,7 @@ public RootGraphImplementor<?> findEntityGraphByName(String name) {
737739

738740
@Override
739741
public String bestGuessEntityName(Object object) {
740-
final LazyInitializer initializer = extractLazyInitializer( object );
742+
final var initializer = extractLazyInitializer( object );
741743
if ( initializer != null ) {
742744
// it is possible for this method to be called during flush processing,
743745
// so make certain that we do not accidentally initialize an uninitialized proxy
@@ -803,8 +805,8 @@ public void close() {
803805
LOG.closingFactory( getUuid() );
804806
observer.sessionFactoryClosing( this );
805807

806-
// NOTE : the null checks below handle cases where close is called from
807-
// a failed attempt to create the SessionFactory
808+
// NOTE: the null checks below handle cases where close is called
809+
// from a failed attempt to create the SessionFactory
808810

809811
if ( cacheAccess != null ) {
810812
cacheAccess.close();
@@ -962,7 +964,7 @@ public FilterDefinition getFilterDefinition(String filterName) {
962964
}
963965

964966
@Override
965-
public java.util.Collection<FilterDefinition> getAutoEnabledFilters() {
967+
public Collection<FilterDefinition> getAutoEnabledFilters() {
966968
return autoEnabledFilters;
967969
}
968970

@@ -1072,10 +1074,10 @@ public static Interceptor configuredInterceptor(Interceptor interceptor, Session
10721074
}
10731075

10741076
public static Interceptor configuredInterceptor(Interceptor interceptor, boolean explicitNoInterceptor, SessionFactoryOptions options) {
1075-
// NOTE : DO NOT return EmptyInterceptor.INSTANCE from here as a "default for the Session"
1076-
// we "filter" that one out here. The return from here should represent the
1077-
// explicitly configured Interceptor (if one). Return null from here instead; Session
1078-
// will handle it
1077+
// NOTE: DO NOT return EmptyInterceptor.INSTANCE from here as a "default for the Session"
1078+
// we "filter" that one out here. The return from here should represent the
1079+
// explicitly configured Interceptor (if one). Return null from here instead;
1080+
// Session will handle it
10791081

10801082
if ( interceptor != null && interceptor != EmptyInterceptor.INSTANCE ) {
10811083
return interceptor;
@@ -1087,7 +1089,7 @@ public static Interceptor configuredInterceptor(Interceptor interceptor, boolean
10871089
return optionsInterceptor;
10881090
}
10891091

1090-
// If explicitly asking for no interceptor and there is no SessionFactory-scoped interceptors, then
1092+
// If explicitly asking for no interceptor and there is no SessionFactory-scoped interceptor, then
10911093
// no need to inherit from the configured stateless session ones.
10921094
if ( explicitNoInterceptor ) {
10931095
return null;
@@ -1349,7 +1351,7 @@ public SessionBuilderImpl eventListeners(SessionEventListener... listeners) {
13491351
public SessionBuilderImpl clearEventListeners() {
13501352
if ( listeners == null ) {
13511353
//Needs to initialize explicitly to an empty list as otherwise "null" implies the default listeners will be applied
1352-
this.listeners = new ArrayList<>( 3 );
1354+
listeners = new ArrayList<>( 3 );
13531355
}
13541356
else {
13551357
listeners.clear();

hibernate-core/src/main/java/org/hibernate/internal/util/collections/CollectionHelper.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package org.hibernate.internal.util.collections;
66

7+
import org.checkerframework.checker.nullness.qual.Nullable;
8+
79
import java.util.ArrayList;
810
import java.util.Collection;
911
import java.util.Collections;
@@ -258,27 +260,27 @@ public static <T> Set<T> makeCopy(Set<T> source) {
258260
}
259261
}
260262

261-
public static boolean isEmpty(Collection<?> collection) {
263+
public static boolean isEmpty(@Nullable Collection<?> collection) {
262264
return collection == null || collection.isEmpty();
263265
}
264266

265-
public static boolean isEmpty(Map<?,?> map) {
267+
public static boolean isEmpty(@Nullable Map<?,?> map) {
266268
return map == null || map.isEmpty();
267269
}
268270

269-
public static boolean isNotEmpty(Collection<?> collection) {
271+
public static boolean isNotEmpty(@Nullable Collection<?> collection) {
270272
return !isEmpty( collection );
271273
}
272274

273-
public static boolean isNotEmpty(Map<?,?> map) {
275+
public static boolean isNotEmpty(@Nullable Map<?,?> map) {
274276
return !isEmpty( map );
275277
}
276278

277-
public static boolean isEmpty(Object[] objects) {
279+
public static boolean isEmpty(@Nullable Object[] objects) {
278280
return objects == null || objects.length == 0;
279281
}
280282

281-
public static boolean isNotEmpty(Object[] objects) {
283+
public static boolean isNotEmpty(@Nullable Object[] objects) {
282284
return objects != null && objects.length > 0;
283285
}
284286

@@ -309,7 +311,7 @@ public static Properties asProperties(Map<?,?> map) {
309311
return properties;
310312
}
311313
else {
312-
final Properties properties = new Properties();
314+
final var properties = new Properties();
313315
if ( isNotEmpty( map ) ) {
314316
properties.putAll( map );
315317
}

0 commit comments

Comments
 (0)