Skip to content

Commit 93ed85f

Browse files
committed
more typesafe logging
1 parent 2d1aade commit 93ed85f

File tree

11 files changed

+214
-169
lines changed

11 files changed

+214
-169
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HANALegacyServerConfiguration.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
1818
import org.hibernate.internal.util.StringHelper;
1919
import org.hibernate.internal.util.config.ConfigurationHelper;
20+
import org.jboss.logging.Logger;
2021

2122
import static org.hibernate.cfg.DialectSpecificSettings.HANA_MAX_LOB_PREFETCH_SIZE;
22-
import static org.hibernate.internal.CoreMessageLogger.CORE_LOGGER;
2323

2424
/**
2525
* Utility class that extracts some initial configuration from the database for {@link HANALegacyDialect}.
@@ -60,9 +60,8 @@ public static HANALegacyServerConfiguration fromDialectResolutionInfo(DialectRes
6060
}
6161
catch (SQLException e) {
6262
// Ignore
63-
CORE_LOGGER.debug(
64-
"An error occurred while trying to determine the database version.",
65-
e );
63+
Logger.getLogger( HANALegacyServerConfiguration.class )
64+
.debug( "An error occurred while trying to determine the database version.", e );
6665
}
6766

6867
if (databaseMajorVersion > 0 && databaseMajorVersion < 4) {
@@ -77,9 +76,8 @@ public static HANALegacyServerConfiguration fromDialectResolutionInfo(DialectRes
7776
}
7877
catch (SQLException e) {
7978
// Ignore
80-
CORE_LOGGER.debug(
81-
"An error occurred while trying to determine the value of the HANA parameter indexserver.ini / session / max_lob_prefetch_size.",
82-
e );
79+
Logger.getLogger( HANALegacyServerConfiguration.class )
80+
.debug( "An error occurred while trying to determine the value of the HANA parameter indexserver.ini / session / max_lob_prefetch_size.", e );
8381
}
8482
}
8583
else {
@@ -115,7 +113,8 @@ public static DatabaseVersion determineDatabaseVersion(DialectResolutionInfo inf
115113
}
116114
catch (SQLException e) {
117115
// Ignore
118-
CORE_LOGGER.debug( "An error occurred while trying to determine the HANA Cloud version.", e );
116+
Logger.getLogger( HANALegacyServerConfiguration.class )
117+
.debug( "An error occurred while trying to determine the HANA Cloud version.", e );
119118
}
120119
}
121120
return databaseVersion == null

hibernate-core/src/main/java/org/hibernate/bytecode/internal/bytebuddy/ByteBuddyState.java

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
*/
55
package org.hibernate.bytecode.internal.bytebuddy;
66

7-
import java.io.File;
8-
import java.io.IOException;
97
import java.lang.invoke.MethodHandles;
10-
import java.lang.reflect.Method;
118
import java.util.ArrayList;
129
import java.util.List;
1310
import java.util.function.BiFunction;
@@ -17,7 +14,6 @@
1714
import net.bytebuddy.description.type.TypeDescription;
1815
import org.hibernate.HibernateException;
1916
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImplConstants;
20-
import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
2117
import org.hibernate.bytecode.spi.BasicProxyFactory;
2218
import org.hibernate.engine.spi.PrimeAmongSecondarySupertypes;
2319
import org.hibernate.proxy.ProxyConfiguration;
@@ -46,7 +42,8 @@
4642
import static net.bytebuddy.matcher.ElementMatchers.not;
4743
import static net.bytebuddy.matcher.ElementMatchers.returns;
4844
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;
49-
import static org.hibernate.internal.CoreMessageLogger.CORE_LOGGER;
45+
import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX;
46+
import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX;
5047

5148
/**
5249
* A utility to hold all ByteBuddy related state, as in the current version of
@@ -57,8 +54,6 @@ public final class ByteBuddyState {
5754

5855
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
5956

60-
private static final boolean DEBUG = false;
61-
6257
private final ByteBuddy byteBuddy;
6358

6459
private final ProxyDefinitionHelpers proxyDefinitionHelpers = new ProxyDefinitionHelpers();
@@ -79,9 +74,9 @@ public ByteBuddyState() {
7974
}
8075

8176
ByteBuddyState(ClassFileVersion classFileVersion) {
82-
this.byteBuddy = new ByteBuddy( classFileVersion ).with( TypeValidation.DISABLED );
83-
this.proxyCache = new TypeCache( TypeCache.Sort.WEAK );
84-
this.basicProxyCache = new TypeCache( TypeCache.Sort.WEAK );
77+
byteBuddy = new ByteBuddy( classFileVersion ).with( TypeValidation.DISABLED );
78+
proxyCache = new TypeCache<>( TypeCache.Sort.WEAK );
79+
basicProxyCache = new TypeCache<>( TypeCache.Sort.WEAK );
8580
}
8681

8782
/**
@@ -166,12 +161,9 @@ public Class<?> load(Class<?> referenceClass, Function<ByteBuddy, DynamicType.Bu
166161
*/
167162
public byte[] rewrite(TypePool typePool, String className,
168163
Function<ByteBuddy, DynamicType.Builder<?>> rewriteClassFunction) {
169-
DynamicType.Builder<?> builder = rewriteClassFunction.apply( byteBuddy );
170-
if ( builder == null ) {
171-
return null;
172-
}
164+
var builder = rewriteClassFunction.apply( byteBuddy );
165+
return builder == null ? null : make( typePool, builder ).getBytes();
173166

174-
return make( typePool, builder ).getBytes();
175167
}
176168

177169
/**
@@ -208,7 +200,7 @@ void clearState() {
208200
*/
209201
public Class<?> load(Class<?> referenceClass, String className, BiFunction<ByteBuddy, NamingStrategy, DynamicType.Builder<?>> makeClassFunction) {
210202
try {
211-
Class<?> result = referenceClass.getClassLoader().loadClass(className);
203+
final var result = referenceClass.getClassLoader().loadClass( className );
212204
if ( result.getClassLoader() == referenceClass.getClassLoader() ) {
213205
return result;
214206
}
@@ -218,10 +210,8 @@ public Class<?> load(Class<?> referenceClass, String className, BiFunction<ByteB
218210
}
219211
try {
220212
return make( makeClassFunction.apply( byteBuddy, new FixedNamingStrategy( className ) ) )
221-
.load(
222-
referenceClass.getClassLoader(),
223-
resolveClassLoadingStrategy( referenceClass )
224-
)
213+
.load( referenceClass.getClassLoader(),
214+
resolveClassLoadingStrategy( referenceClass ) )
225215
.getLoaded();
226216
}
227217
catch (LinkageError e) {
@@ -240,10 +230,8 @@ private Class<?> load(Class<?> referenceClass, TypeCache<TypeCache.SimpleKey> ca
240230
referenceClass.getClassLoader(),
241231
cacheKey,
242232
() -> make( makeProxyFunction.apply( byteBuddy ) )
243-
.load(
244-
referenceClass.getClassLoader(),
245-
resolveClassLoadingStrategy( referenceClass )
246-
)
233+
.load( referenceClass.getClassLoader(),
234+
resolveClassLoadingStrategy( referenceClass ) )
247235
.getLoaded(),
248236
cache
249237
);
@@ -262,23 +250,7 @@ private Unloaded<?> make(DynamicType.Builder<?> builder) {
262250
}
263251

264252
private Unloaded<?> make(TypePool typePool, DynamicType.Builder<?> builder) {
265-
Unloaded<?> unloadedClass;
266-
if ( typePool != null ) {
267-
unloadedClass = builder.make( typePool );
268-
}
269-
else {
270-
unloadedClass = builder.make();
271-
}
272-
273-
if ( DEBUG ) {
274-
try {
275-
unloadedClass.saveIn( new File( System.getProperty( "java.io.tmpdir" ) + "/bytebuddy/" ) );
276-
}
277-
catch (IOException e) {
278-
CORE_LOGGER.warn( "Unable to save generated class %1$s", unloadedClass.getTypeDescription().getName(), e );
279-
}
280-
}
281-
return unloadedClass;
253+
return typePool == null ? builder.make() : builder.make( typePool );
282254
}
283255

284256
public EnhancerImplConstants getEnhancerConstants() {
@@ -298,27 +270,38 @@ public static class ProxyDefinitionHelpers {
298270
private final FieldAccessor.PropertyConfigurable interceptorFieldAccessor;
299271

300272
private ProxyDefinitionHelpers() {
301-
this.groovyGetMetaClassFilter = isSynthetic().and( named( "getMetaClass" )
302-
.and( returns( td -> "groovy.lang.MetaClass".equals( td.getName() ) ) ) );
303-
this.virtualNotFinalizerFilter = isVirtual().and( not( isFinalizer() ) );
304-
this.proxyNonInterceptedMethodFilter = nameStartsWith( "$$_hibernate_" ).and( isVirtual() )
305-
// HHH-15090: Don't apply extended enhancement reader/writer methods to the proxy;
306-
// those need to be executed on the actual entity.
307-
.and( not( nameStartsWith( EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX ) ) )
308-
.and( not( nameStartsWith( EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX ) ) );
273+
groovyGetMetaClassFilter =
274+
isSynthetic().and( named( "getMetaClass" )
275+
.and( returns( td -> "groovy.lang.MetaClass".equals( td.getName() ) ) ) );
276+
virtualNotFinalizerFilter =
277+
isVirtual().and( not( isFinalizer() ) );
278+
proxyNonInterceptedMethodFilter =
279+
nameStartsWith( "$$_hibernate_" ).and( isVirtual() )
280+
// HHH-15090: Don't apply extended enhancement reader/writer methods to the proxy;
281+
// those need to be executed on the actual entity.
282+
.and( not( nameStartsWith( PERSISTENT_FIELD_READER_PREFIX ) ) )
283+
.and( not( nameStartsWith( PERSISTENT_FIELD_WRITER_PREFIX ) ) );
309284

310285
// Populate the toFullyIgnore list
311-
for ( Method m : PrimeAmongSecondarySupertypes.class.getMethods() ) {
286+
for ( var method : PrimeAmongSecondarySupertypes.class.getMethods() ) {
312287
//We need to ignore both the match of each default method on PrimeAmongSecondarySupertypes
313-
toFullyIgnore.add( isDeclaredBy( PrimeAmongSecondarySupertypes.class ).and( named( m.getName() ) ).and( takesNoArguments() ) );
288+
toFullyIgnore.add(
289+
isDeclaredBy( PrimeAmongSecondarySupertypes.class )
290+
.and( named( method.getName() ) )
291+
.and( takesNoArguments() ) );
314292
//And the override in the interface it belongs to - which we happen to have in the return type
315-
toFullyIgnore.add( isDeclaredBy( m.getReturnType() ).and( named( m.getName() ) ).and( takesNoArguments() ) );
293+
toFullyIgnore.add(
294+
isDeclaredBy( method.getReturnType() )
295+
.and( named( method.getName() ) )
296+
.and( takesNoArguments() ) );
316297
}
317298

318-
this.delegateToInterceptorDispatcherMethodDelegation = MethodDelegation.to( ProxyConfiguration.InterceptorDispatcher.class );
299+
delegateToInterceptorDispatcherMethodDelegation =
300+
MethodDelegation.to( ProxyConfiguration.InterceptorDispatcher.class );
319301

320-
this.interceptorFieldAccessor = FieldAccessor.ofField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME )
321-
.withAssigner( Assigner.DEFAULT, Assigner.Typing.DYNAMIC );
302+
interceptorFieldAccessor =
303+
FieldAccessor.ofField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME )
304+
.withAssigner( Assigner.DEFAULT, Assigner.Typing.DYNAMIC );
322305
}
323306

324307
public ElementMatcher<? super MethodDescription> getGroovyGetMetaClassFilter() {
@@ -342,8 +325,8 @@ public FieldAccessor.PropertyConfigurable getInterceptorFieldAccessor() {
342325
}
343326

344327
public DynamicType.Builder<?> appendIgnoreAlsoAtEnd(DynamicType.Builder<?> builder) {
345-
for ( ElementMatcher<? super MethodDescription> m : toFullyIgnore ) {
346-
builder = builder.ignoreAlso( m );
328+
for ( var elementMatcher : toFullyIgnore ) {
329+
builder = builder.ignoreAlso( elementMatcher );
347330
}
348331
return builder;
349332
}

hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@ public Configuration(BootstrapServiceRegistry serviceRegistry) {
200200
}
201201

202202
private XmlMappingBinderAccess createMappingBinderAccess(BootstrapServiceRegistry serviceRegistry) {
203-
return new XmlMappingBinderAccess(
204-
serviceRegistry,
205-
(settingName) -> properties == null ? null : properties.get( settingName )
206-
);
203+
return new XmlMappingBinderAccess( serviceRegistry,
204+
settingName -> properties == null ? null : properties.get( settingName ) );
207205
}
208206

209207
/**
@@ -578,9 +576,7 @@ public Configuration registerTypeOverride(UserType<?> type, String[] keys) {
578576
if ( userTypeRegistrations == null ) {
579577
userTypeRegistrations = new ArrayList<>();
580578
}
581-
userTypeRegistrations.add(
582-
metadataBuilder -> metadataBuilder.applyBasicType( type, keys )
583-
);
579+
userTypeRegistrations.add( builder -> builder.applyBasicType( type, keys ) );
584580
return this;
585581
}
586582

@@ -759,7 +755,12 @@ public Configuration addClass(Class<?> entityClass) throws MappingException {
759755
if ( entityClass == null ) {
760756
throw new IllegalArgumentException( "The specified class cannot be null" );
761757
}
762-
return addResource( entityClass.getName().replace( '.', '/' ) + ".hbm.xml" );
758+
return addResource( hbmFileName( entityClass ) );
759+
}
760+
761+
private static String hbmFileName(Class<?> entityClass) {
762+
return entityClass.getName().replace( '.', '/' )
763+
+ ".hbm.xml";
763764
}
764765

765766
/**
@@ -1023,8 +1024,8 @@ public Configuration setColumnOrderingStrategy(ColumnOrderingStrategy columnOrde
10231024
* @throws HibernateException usually indicates an invalid configuration or invalid mapping information
10241025
*/
10251026
public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throws HibernateException {
1026-
CORE_LOGGER.trace( "Building session factory using provided StandardServiceRegistry" );
1027-
final MetadataBuilder metadataBuilder =
1027+
CORE_LOGGER.buildingFactoryWithProvidedRegistry();
1028+
final var metadataBuilder =
10281029
metadataSources.getMetadataBuilder( (StandardServiceRegistry) serviceRegistry );
10291030

10301031
if ( implicitNamingStrategy != null ) {
@@ -1043,11 +1044,11 @@ public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throw
10431044
metadataBuilder.applySharedCacheMode( sharedCacheMode );
10441045
}
10451046

1046-
for ( TypeContributor typeContributor : typeContributorRegistrations ) {
1047+
for ( var typeContributor : typeContributorRegistrations ) {
10471048
metadataBuilder.applyTypes( typeContributor );
10481049
}
10491050

1050-
for ( FunctionContributor functionContributor : functionContributorRegistrations ) {
1051+
for ( var functionContributor : functionContributorRegistrations ) {
10511052
metadataBuilder.applyFunctions( functionContributor );
10521053
}
10531054

@@ -1126,7 +1127,7 @@ public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throw
11261127
* @throws HibernateException usually indicates an invalid configuration or invalid mapping information
11271128
*/
11281129
public SessionFactory buildSessionFactory() throws HibernateException {
1129-
CORE_LOGGER.trace( "Building session factory using internal StandardServiceRegistryBuilder" );
1130+
CORE_LOGGER.buildingFactoryWithInternalRegistryBuilder();
11301131
standardServiceRegistryBuilder.applySettings( properties );
11311132
var serviceRegistry = standardServiceRegistryBuilder.build();
11321133
try {

hibernate-core/src/main/java/org/hibernate/engine/internal/Collections.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ private static void processDereferencedCollection(PersistentCollection<?> collec
4646
final var loadedPersister = entry.getLoadedPersister();
4747

4848
if ( loadedPersister != null && CORE_LOGGER.isTraceEnabled() ) {
49-
CORE_LOGGER.trace( "Collection dereferenced: "
50-
+ collectionInfoString( loadedPersister, collection, entry.getLoadedKey(), session ) );
49+
CORE_LOGGER.collectionDereferenced(
50+
collectionInfoString( loadedPersister, collection, entry.getLoadedKey(), session ) );
5151
}
5252

5353
// do a check
@@ -112,8 +112,8 @@ private static void processNeverReferencedCollection(PersistentCollection<?> col
112112
final Object loadedKey = entry.getLoadedKey();
113113

114114
if ( CORE_LOGGER.isTraceEnabled() ) {
115-
CORE_LOGGER.trace( "Found collection with unloaded owner: "
116-
+ collectionInfoString( loadedPersister, collection, loadedKey, session ) );
115+
CORE_LOGGER.collectionWithUnloadedOwner(
116+
collectionInfoString( loadedPersister, collection, loadedKey, session ) );
117117
}
118118

119119
entry.setCurrentPersister( loadedPersister );
@@ -161,8 +161,8 @@ public static void processReachableCollection(
161161
// the class of the collection owner is enhanced for lazy loading,
162162
// and we found an un-initialized PersistentCollection, so skip it
163163
if ( CORE_LOGGER.isTraceEnabled() ) {
164-
CORE_LOGGER.trace( "Skipping uninitialized bytecode-lazy collection: "
165-
+ collectionInfoString( persister, collection, collectionEntry.getCurrentKey(), session ) );
164+
CORE_LOGGER.skippingUninitializedBytecodeLazyCollection(
165+
collectionInfoString( persister, collection, collectionEntry.getCurrentKey(), session ) );
166166
}
167167
collectionEntry.setReached( true );
168168
collectionEntry.setProcessed( true );
@@ -187,8 +187,7 @@ private static void logReachedCollection(
187187
CollectionEntry collectionEntry) {
188188
if ( CORE_LOGGER.isTraceEnabled() ) {
189189
if ( collection.wasInitialized() ) {
190-
CORE_LOGGER.tracef(
191-
"Collection found: %s, was: %s (initialized)",
190+
CORE_LOGGER.collectionFoundInitialized(
192191
collectionInfoString(
193192
persister,
194193
collection,
@@ -204,8 +203,7 @@ private static void logReachedCollection(
204203
);
205204
}
206205
else {
207-
CORE_LOGGER.tracef(
208-
"Collection found: %s, was: %s (uninitialized)",
206+
CORE_LOGGER.collectionFoundUninitialized(
209207
collectionInfoString(
210208
persister,
211209
collection,
@@ -268,7 +266,7 @@ private static void prepareCollectionForUpdate(
268266
// we will need to remove the old entries
269267
collectionEntry.setDoremove( true );
270268
if ( collectionEntry.isDorecreate() ) {
271-
CORE_LOGGER.trace( "Forcing collection initialization" );
269+
CORE_LOGGER.forcingCollectionInitialization();
272270
collection.forceInitialization();
273271
}
274272
}

0 commit comments

Comments
 (0)