diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java index 0004d04995..476b9843f4 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/BootstrapContextImpl.java @@ -13,11 +13,8 @@ import java.util.HashMap; import java.util.Map; import org.hibernate.AssertionFailure; -import org.hibernate.annotations.common.reflection.ClassLoaderDelegate; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.java.JavaReflectionManager; -import org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl; import org.hibernate.boot.AttributeConverterInfo; import org.hibernate.boot.CacheRegionDefinition; import org.hibernate.boot.archive.scan.internal.StandardScanOptions; @@ -313,32 +310,7 @@ public class BootstrapContextImpl implements BootstrapContext { private JavaReflectionManager generateHcannReflectionManager() { final JavaReflectionManager reflectionManager = new JavaReflectionManager(); reflectionManager.setMetadataProvider( new JPAMetadataProvider( this ) ); - reflectionManager.injectClassLoaderDelegate( generateHcannClassLoaderDelegate() ); return reflectionManager; } - private ClassLoaderDelegate generateHcannClassLoaderDelegate() { - // class loading here needs to be drastically different for 7.0 - // but luckily 7.0 will do away with HCANN use and be easier to - // implement this. - // - // todo (6.0) : *if possible* make similar change in 6.0 - // possibly using the JPA temp class loader or create our own "throw awy" ClassLoader; - // the trouble there is that we eventually need to load the Class into the real - // ClassLoader prior to use - - final ClassLoaderService classLoaderService = getServiceRegistry().getService( ClassLoaderService.class ); - - return new ClassLoaderDelegate() { - @Override - public Class classForName(String className) throws ClassLoadingException { - try { - return classLoaderService.classForName( className ); - } - catch (org.hibernate.boot.registry.classloading.spi.ClassLoadingException e) { - return StandardClassLoaderDelegateImpl.INSTANCE.classForName( className ); - } - } - }; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/annotations/AnnotationMetadataSourceProcessorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/annotations/AnnotationMetadataSourceProcessorImpl.java index f939a1cd7d..4e543f5448 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/annotations/AnnotationMetadataSourceProcessorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/annotations/AnnotationMetadataSourceProcessorImpl.java @@ -17,8 +17,6 @@ import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.MappedSuperclass; -import org.hibernate.AnnotationException; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.MetadataProviderInjector; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; @@ -59,6 +57,7 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc private final LinkedHashSet annotatedPackages = new LinkedHashSet(); private final List xClasses = new ArrayList(); + private final ClassLoaderService classLoaderService; public AnnotationMetadataSourceProcessorImpl( ManagedResources managedResources, @@ -74,6 +73,7 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc } final AttributeConverterManager attributeConverterManager = new AttributeConverterManager( rootMetadataBuildingContext ); + this.classLoaderService = rootMetadataBuildingContext.getBuildingOptions().getServiceRegistry().getService( ClassLoaderService.class ); if ( rootMetadataBuildingContext.getBuildingOptions().isXmlMappingEnabled() ) { @@ -97,25 +97,24 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc final List classNames = jpaMetadataProvider.getXMLContext().addDocument( dom4jDocument ); for ( String className : classNames ) { - xClasses.add( toXClass( className, reflectionManager ) ); + xClasses.add( toXClass( className, reflectionManager, classLoaderService ) ); } } jpaMetadataProvider.getXMLContext().applyDiscoveredAttributeConverters( attributeConverterManager ); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } - final ClassLoaderService cls = rootMetadataBuildingContext.getBuildingOptions().getServiceRegistry().getService( ClassLoaderService.class ); for ( String className : managedResources.getAnnotatedClassNames() ) { - final Class annotatedClass = cls.classForName( className ); - categorizeAnnotatedClass( annotatedClass, attributeConverterManager ); + final Class annotatedClass = classLoaderService.classForName( className ); + categorizeAnnotatedClass( annotatedClass, attributeConverterManager, classLoaderService ); } for ( Class annotatedClass : managedResources.getAnnotatedClassReferences() ) { - categorizeAnnotatedClass( annotatedClass, attributeConverterManager ); + categorizeAnnotatedClass( annotatedClass, attributeConverterManager, classLoaderService ); } } - private void categorizeAnnotatedClass(Class annotatedClass, AttributeConverterManager attributeConverterManager) { + private void categorizeAnnotatedClass(Class annotatedClass, AttributeConverterManager attributeConverterManager, ClassLoaderService cls) { final XClass xClass = reflectionManager.toXClass( annotatedClass ); // categorize it, based on assumption it does not fall into multiple categories if ( xClass.isAnnotationPresent( Converter.class ) ) { @@ -135,13 +134,8 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc } @SuppressWarnings("deprecation") - private XClass toXClass(String className, ReflectionManager reflectionManager) { - try { - return reflectionManager.classForName( className ); - } - catch ( ClassLoadingException e ) { - throw new AnnotationException( "Unable to load class defined in XML: " + className, e ); - } + private XClass toXClass(String className, ReflectionManager reflectionManager, ClassLoaderService cls) { + return reflectionManager.toXClass( cls.classForName( className ) ); } // private Document toDom4jDocument(MappingBinder.DelayedOrmXmlData delayedOrmXmlData) { @@ -192,7 +186,7 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc AnnotationBinder.bindDefaults( rootMetadataBuildingContext ); for ( String annotatedPackage : annotatedPackages ) { - AnnotationBinder.bindPackage( annotatedPackage, rootMetadataBuildingContext ); + AnnotationBinder.bindPackage( classLoaderService, annotatedPackage, rootMetadataBuildingContext ); } } @@ -303,7 +297,7 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc @Override public void postProcessEntityHierarchies() { for ( String annotatedPackage : annotatedPackages ) { - AnnotationBinder.bindFetchProfilesForPackage( annotatedPackage, rootMetadataBuildingContext ); + AnnotationBinder.bindFetchProfilesForPackage( classLoaderService, annotatedPackage, rootMetadataBuildingContext ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java index 2f82d36162..554e8dca49 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl.java @@ -261,6 +261,22 @@ public class ClassLoaderServiceImpl implements ClassLoaderService { ); } + @Override + public Package packageForNameOrNull(String packageName) { + try { + Class aClass = Class.forName( packageName + ".package-info", true, getAggregatedClassLoader() ); + return aClass == null ? null : aClass.getPackage(); + } + catch (ClassNotFoundException e) { + log.packageNotFound( packageName ); + return null; + } + catch (LinkageError e) { + log.warn( "LinkageError while attempting to load Package named " + packageName, e ); + return null; + } + } + @Override public T workWithClassLoader(Work work) { return work.doWork( getAggregatedClassLoader() ); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/spi/ClassLoaderService.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/spi/ClassLoaderService.java index 1fcdb9c5f9..45c1fdb379 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/spi/ClassLoaderService.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/spi/ClassLoaderService.java @@ -75,6 +75,21 @@ public interface ClassLoaderService extends Service, Stoppable { T generateProxy(InvocationHandler handler, Class... interfaces); + /** + * Loading a Package from the classloader. In case it's not found or an + * internal error (such as @see {@link LinkageError} occurs, we + * return null rather than throwing an exception. + * This is significantly different than loading a Class, as in all + * currently known usages, being unable to load the Package will + * only result in ignoring annotations on it - which is totally + * fine when the object doesn't exist. + * In case of other errors, implementations are expected to log + * a warning but it's still not treated as a fatal error. + * @param packageName + * @return the matching Package, or null. + */ + Package packageForNameOrNull(String packageName); + interface Work { T doWork(ClassLoader classLoader); } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 50e5730652..592918887c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -123,7 +123,7 @@ import org.hibernate.annotations.Tuplizers; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.hibernate.annotations.Where; -import org.hibernate.annotations.common.reflection.ClassLoadingException; +import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XAnnotatedElement; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XMethod; @@ -132,6 +132,7 @@ import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.boot.model.IdGeneratorStrategyInterpreter; import org.hibernate.boot.model.IdentifierGeneratorDefinition; import org.hibernate.boot.model.TypeDefinition; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref; import org.hibernate.boot.spi.MetadataBuildingContext; @@ -146,8 +147,6 @@ import org.hibernate.cfg.annotations.QueryBinder; import org.hibernate.cfg.annotations.SimpleValueBinder; import org.hibernate.cfg.annotations.TableBinder; import org.hibernate.engine.OptimisticLockStyle; -import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.internal.CoreMessageLogger; @@ -306,19 +305,12 @@ public final class AnnotationBinder { } } - public static void bindPackage(String packageName, MetadataBuildingContext context) { - XPackage pckg; - try { - pckg = context.getBootstrapContext().getReflectionManager().packageForName( packageName ); - } - catch (ClassLoadingException e) { - LOG.packageNotFound( packageName ); - return; - } - catch ( ClassNotFoundException cnf ) { - LOG.packageNotFound( packageName ); + public static void bindPackage(ClassLoaderService cls, String packageName, MetadataBuildingContext context) { + final Package packaze = cls.packageForNameOrNull( packageName ); + if ( packaze == null ) { return; } + final XPackage pckg = context.getBootstrapContext().getReflectionManager().toXPackage( packaze ); if ( pckg.isAnnotationPresent( SequenceGenerator.class ) ) { SequenceGenerator ann = pckg.getAnnotation( SequenceGenerator.class ); @@ -1423,20 +1415,13 @@ public final class AnnotationBinder { bindFetchProfiles( clazzToProcess, context ); } - public static void bindFetchProfilesForPackage(String packageName, MetadataBuildingContext context) { - XPackage pckg; - try { - pckg = context.getBootstrapContext().getReflectionManager().packageForName( packageName ); - } - catch (ClassLoadingException e) { - LOG.packageNotFound( packageName ); + public static void bindFetchProfilesForPackage(ClassLoaderService cls, String packageName, MetadataBuildingContext context) { + final Package packaze = cls.packageForNameOrNull( packageName ); + if ( packaze == null ) { return; } - catch ( ClassNotFoundException cnf ) { - LOG.packageNotFound( packageName ); - return; - } - + final ReflectionManager reflectionManager = context.getBootstrapContext().getReflectionManager(); + final XPackage pckg = reflectionManager.toXPackage( packaze ); bindFetchProfiles( pckg, context ); } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java index 52d7f7ace8..93a08a145f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java @@ -31,7 +31,6 @@ import org.hibernate.annotations.AnyMetaDef; import org.hibernate.annotations.AnyMetaDefs; import org.hibernate.annotations.MetaValue; import org.hibernate.annotations.SqlFragmentAlias; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.XAnnotatedElement; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XPackage; @@ -1097,14 +1096,8 @@ public class BinderHelper { PropertyHolder propertyHolder, String propertyName, MetadataBuildingContext buildingContext) { - final XClass persistentXClass; - try { - persistentXClass = buildingContext.getBootstrapContext().getReflectionManager() - .classForName( propertyHolder.getPersistentClass().getClassName() ); - } - catch ( ClassLoadingException e ) { - throw new AssertionFailure( "PersistentClass name cannot be converted into a Class", e); - } + final XClass persistentXClass = buildingContext.getBootstrapContext().getReflectionManager() + .toXClass( propertyHolder.getPersistentClass().getMappedClass() ); if ( propertyHolder.isInIdClass() ) { PropertyData pd = buildingContext.getMetadataCollector().getPropertyAnnotatedWithIdAndToOne( persistentXClass, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java index 00a3334677..4a4f83fe1c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java @@ -23,9 +23,9 @@ import org.hibernate.AnnotationException; import org.hibernate.AssertionFailure; import org.hibernate.FetchMode; import org.hibernate.MappingException; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XProperty; +import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.cfg.AccessType; import org.hibernate.cfg.AnnotatedClassType; @@ -217,19 +217,17 @@ public class MapBinder extends CollectionBinder { //does not make sense for a map key element.setIgnoreNotFound( ignoreNotFound ); } else { - XClass keyXClass; + final XClass keyXClass; AnnotatedClassType classType; if ( BinderHelper.PRIMITIVE_NAMES.contains( mapKeyType ) ) { classType = AnnotatedClassType.NONE; keyXClass = null; } else { - try { - keyXClass = buildingContext.getBootstrapContext().getReflectionManager().classForName( mapKeyType ); - } - catch (ClassLoadingException e) { - throw new AnnotationException( "Unable to find class: " + mapKeyType, e ); - } + final BootstrapContext bootstrapContext = buildingContext.getBootstrapContext(); + final Class mapKeyClass = bootstrapContext.getClassLoaderAccess().classForName( mapKeyType ); + keyXClass = bootstrapContext.getReflectionManager().toXClass( mapKeyClass ); + classType = buildingContext.getMetadataCollector().getClassType( keyXClass ); // force in case of attribute override naming the key if ( isEmbedded || mappingDefinedAttributeOverrideOnMapKey( property ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java index 6808459c11..b5ff7ef3f2 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java @@ -26,7 +26,6 @@ import org.hibernate.annotations.MapKeyType; import org.hibernate.annotations.Nationalized; import org.hibernate.annotations.Parameter; import org.hibernate.annotations.Type; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.boot.model.TypeDefinition; @@ -534,27 +533,22 @@ public class SimpleValueBinder { if ( simpleValue.getTypeName() != null && simpleValue.getTypeName().length() > 0 && simpleValue.getMetadata().getTypeResolver().basic( simpleValue.getTypeName() ) == null ) { - try { - Class typeClass = buildingContext.getBootstrapContext().getClassLoaderAccess().classForName( simpleValue.getTypeName() ); + Class typeClass = buildingContext.getBootstrapContext().getClassLoaderAccess().classForName( simpleValue.getTypeName() ); - if ( typeClass != null && DynamicParameterizedType.class.isAssignableFrom( typeClass ) ) { - Properties parameters = simpleValue.getTypeParameters(); - if ( parameters == null ) { - parameters = new Properties(); - } - parameters.put( DynamicParameterizedType.IS_DYNAMIC, Boolean.toString( true ) ); - parameters.put( DynamicParameterizedType.RETURNED_CLASS, returnedClassName ); - parameters.put( DynamicParameterizedType.IS_PRIMARY_KEY, Boolean.toString( key ) ); - - parameters.put( DynamicParameterizedType.ENTITY, persistentClassName ); - parameters.put( DynamicParameterizedType.XPROPERTY, xproperty ); - parameters.put( DynamicParameterizedType.PROPERTY, xproperty.getName() ); - parameters.put( DynamicParameterizedType.ACCESS_TYPE, accessType.getType() ); - simpleValue.setTypeParameters( parameters ); + if ( typeClass != null && DynamicParameterizedType.class.isAssignableFrom( typeClass ) ) { + Properties parameters = simpleValue.getTypeParameters(); + if ( parameters == null ) { + parameters = new Properties(); } - } - catch (ClassLoadingException e) { - throw new MappingException( "Could not determine type for: " + simpleValue.getTypeName(), e ); + parameters.put( DynamicParameterizedType.IS_DYNAMIC, Boolean.toString( true ) ); + parameters.put( DynamicParameterizedType.RETURNED_CLASS, returnedClassName ); + parameters.put( DynamicParameterizedType.IS_PRIMARY_KEY, Boolean.toString( key ) ); + + parameters.put( DynamicParameterizedType.ENTITY, persistentClassName ); + parameters.put( DynamicParameterizedType.XPROPERTY, xproperty ); + parameters.put( DynamicParameterizedType.PROPERTY, xproperty.getName() ); + parameters.put( DynamicParameterizedType.ACCESS_TYPE, accessType.getType() ); + simpleValue.setTypeParameters( parameters ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/EventEngine.java b/hibernate-core/src/main/java/org/hibernate/event/spi/EventEngine.java index dbf18b0995..07a7bd228c 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/EventEngine.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/EventEngine.java @@ -61,7 +61,7 @@ public class EventEngine { continue; } - this.callbackBuilder.buildCallbacksForEntity( persistentClass.getClassName(), callbackRegistry ); + this.callbackBuilder.buildCallbacksForEntity( persistentClass.getMappedClass(), callbackRegistry ); for ( Iterator propertyIterator = persistentClass.getDeclaredPropertyIterator(); propertyIterator.hasNext(); ) { final Property property = propertyIterator.next(); @@ -69,7 +69,7 @@ public class EventEngine { if ( property.getType().isComponentType() ) { this.callbackBuilder.buildCallbacksForEmbeddable( property, - persistentClass.getClassName(), + persistentClass.getMappedClass(), callbackRegistry ); } diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/CallbackBuilderLegacyImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/CallbackBuilderLegacyImpl.java index c96c465aa4..d7fac04bc1 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/CallbackBuilderLegacyImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/CallbackBuilderLegacyImpl.java @@ -20,7 +20,6 @@ import javax.persistence.MappedSuperclass; import javax.persistence.PersistenceException; import org.hibernate.MappingException; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XMethod; @@ -52,53 +51,38 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { } @Override - public void buildCallbacksForEntity(String entityClassName, CallbackRegistrar callbackRegistrar) { - try { - final XClass entityXClass = reflectionManager.classForName( entityClassName ); - final Class entityClass = reflectionManager.toClass( entityXClass ); - for ( CallbackType callbackType : CallbackType.values() ) { - if ( callbackRegistrar.hasRegisteredCallbacks( entityClass, callbackType ) ) { - // this most likely means we have a class mapped multiple times using the hbm.xml - // "entity name" feature - if ( log.isDebugEnabled() ) { - log.debugf( - "CallbackRegistry reported that Class [%s] already had %s callbacks registered; " + - "assuming this means the class was mapped twice " + - "(using hbm.xml entity-name support) - skipping subsequent registrations", - entityClassName, - callbackType.getCallbackAnnotation().getSimpleName() - ); - } - continue; + public void buildCallbacksForEntity(Class entityClass, CallbackRegistrar callbackRegistrar) { + for ( CallbackType callbackType : CallbackType.values() ) { + if ( callbackRegistrar.hasRegisteredCallbacks( entityClass, callbackType ) ) { + // this most likely means we have a class mapped multiple times using the hbm.xml + // "entity name" feature + if ( log.isDebugEnabled() ) { + log.debugf( + "CallbackRegistry reported that Class [%s] already had %s callbacks registered; " + + "assuming this means the class was mapped twice " + + "(using hbm.xml entity-name support) - skipping subsequent registrations", + entityClass.getName(), + callbackType.getCallbackAnnotation().getSimpleName() + ); } - final Callback[] callbacks = resolveEntityCallbacks( entityXClass, callbackType, reflectionManager ); - callbackRegistrar.registerCallbacks( entityClass, callbacks ); + continue; } - } - catch (ClassLoadingException e) { - throw new MappingException( "entity class not found: " + entityClassName, e ); + final Callback[] callbacks = resolveEntityCallbacks( entityClass, callbackType, reflectionManager ); + callbackRegistrar.registerCallbacks( entityClass, callbacks ); } } @Override public void buildCallbacksForEmbeddable( - Property embeddableProperty, String entityClassName, CallbackRegistrar callbackRegistrar) { - try { - final XClass entityXClass = reflectionManager.classForName( entityClassName ); - final Class entityClass = reflectionManager.toClass( entityXClass ); - - for ( CallbackType callbackType : CallbackType.values() ) { - final Callback[] callbacks = resolveEmbeddableCallbacks( - entityClass, - embeddableProperty, - callbackType, - reflectionManager - ); - callbackRegistrar.registerCallbacks( entityClass, callbacks ); - } - } - catch (ClassLoadingException e) { - throw new MappingException( "Class not found: ", e ); + Property embeddableProperty, Class entityClass, CallbackRegistrar callbackRegistrar) { + for ( CallbackType callbackType : CallbackType.values() ) { + final Callback[] callbacks = resolveEmbeddableCallbacks( + entityClass, + embeddableProperty, + callbackType, + reflectionManager + ); + callbackRegistrar.registerCallbacks( entityClass, callbacks ); } } @@ -108,11 +92,11 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { } @SuppressWarnings({"unchecked", "WeakerAccess"}) - public Callback[] resolveEntityCallbacks(XClass beanClass, CallbackType callbackType, ReflectionManager reflectionManager) { + public Callback[] resolveEntityCallbacks(Class entityClass, CallbackType callbackType, ReflectionManager reflectionManager) { List callbacks = new ArrayList<>(); List callbacksMethodNames = new ArrayList<>(); List orderedListeners = new ArrayList<>(); - XClass currentClazz = beanClass; + XClass currentClazz = reflectionManager.toXClass( entityClass ); boolean stopListeners = false; boolean stopDefaultListeners = false; do { @@ -140,7 +124,7 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { "Adding %s as %s callback for entity %s", methodName, callbackType.getCallbackAnnotation().getSimpleName(), - beanClass.getName() + entityClass.getName() ); } callbacks.add( 0, callback ); //superclass first @@ -149,7 +133,7 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { else { throw new PersistenceException( "You can only annotate one callback method with " - + callbackType.getCallbackAnnotation().getName() + " in bean class: " + beanClass.getName() + + callbackType.getCallbackAnnotation().getName() + " in bean class: " + entityClass.getName() ); } } @@ -216,7 +200,7 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { "Adding %s as %s callback for entity %s", methodName, callbackType.getCallbackAnnotation().getSimpleName(), - beanClass.getName() + entityClass.getName() ); } callbacks.add( 0, callback ); // listeners first @@ -225,7 +209,7 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { throw new PersistenceException( "You can only annotate one callback method with " + callbackType.getCallbackAnnotation().getName() - + " in bean class: " + beanClass.getName() + + " in bean class: " + entityClass.getName() + " and callback listener: " + listener.getName() ); } @@ -240,8 +224,8 @@ final class CallbackBuilderLegacyImpl implements CallbackBuilder { @SuppressWarnings({"unchecked", "WeakerAccess"}) public Callback[] resolveEmbeddableCallbacks(Class entityClass, Property embeddableProperty, CallbackType callbackType, ReflectionManager reflectionManager) { - final String embeddableClassName = embeddableProperty.getType().getReturnedClass().getName(); - final XClass embeddableXClass = reflectionManager.classForName( embeddableClassName ); + final Class embeddableClass = embeddableProperty.getType().getReturnedClass(); + final XClass embeddableXClass = reflectionManager.toXClass( embeddableClass ); final Getter embeddableGetter = embeddableProperty.getGetter( entityClass ); final List callbacks = new ArrayList<>(); final List callbacksMethodNames = new ArrayList<>(); diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/EmptyCallbackBuilder.java b/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/EmptyCallbackBuilder.java index dbc557e0d8..894790f14c 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/EmptyCallbackBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/event/internal/EmptyCallbackBuilder.java @@ -12,12 +12,12 @@ import org.hibernate.mapping.Property; final class EmptyCallbackBuilder implements CallbackBuilder { @Override - public void buildCallbacksForEntity(String entityClassName, CallbackRegistrar callbackRegistrar) { + public void buildCallbacksForEntity(Class entityClass, CallbackRegistrar callbackRegistrar) { //no-op } @Override - public void buildCallbacksForEmbeddable(Property embeddableProperty, String entityClassName, CallbackRegistrar callbackRegistrar) { + public void buildCallbacksForEmbeddable(Property embeddableProperty, Class entityClass, CallbackRegistrar callbackRegistrar) { //no-op } diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/event/spi/CallbackBuilder.java b/hibernate-core/src/main/java/org/hibernate/jpa/event/spi/CallbackBuilder.java index dd84e74e60..41bfcda81a 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/event/spi/CallbackBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/event/spi/CallbackBuilder.java @@ -28,11 +28,11 @@ public interface CallbackBuilder { void registerCallbacks(Class entityClass, Callback[] callbacks); } - void buildCallbacksForEntity(String entityClassName, CallbackRegistrar callbackRegistrar); + void buildCallbacksForEntity(Class entityClass, CallbackRegistrar callbackRegistrar); void buildCallbacksForEmbeddable( Property embeddableProperty, - String entityClassName, + Class entityClass, CallbackRegistrar callbackRegistrar); void release(); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java index 97a7c0c35d..837b34387a 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java @@ -11,7 +11,6 @@ import java.util.Set; import javax.persistence.Column; import org.hibernate.MappingException; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XProperty; @@ -310,14 +309,7 @@ public class RevisionInfoConfiguration { for ( PersistentClass persistentClass : metadata.getEntityBindings() ) { // Ensure we're in POJO, not dynamic model, mapping. if (persistentClass.getClassName() != null) { - XClass clazz; - try { - clazz = reflectionManager.classForName( persistentClass.getClassName() ); - } - catch (ClassLoadingException e) { - throw new MappingException( e ); - } - + XClass clazz = reflectionManager.toXClass( persistentClass.getMappedClass() ); final RevisionEntity revisionEntity = clazz.getAnnotation( RevisionEntity.class ); if ( revisionEntity != null ) { if (revisionEntityFound) { diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java index 14924532bf..a58334bca0 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java @@ -22,6 +22,7 @@ import javax.persistence.JoinColumn; import org.dom4j.Element; import org.hibernate.MappingException; import org.hibernate.annotations.common.reflection.ReflectionManager; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.envers.ModificationStore; import org.hibernate.envers.RelationTargetAuditMode; import org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader; @@ -658,9 +659,12 @@ public final class CollectionMetadataGenerator { .getMetadataBuildingOptions() .getReflectionManager(); + final ClassLoaderService classLoaderService = mainGenerator.getGlobalCfg() + .getEnversService() + .getClassLoaderService(); new ComponentAuditedPropertiesReader( ModificationStore.FULL, - new AuditedPropertiesReader.ComponentPropertiesSource( reflectionManager, component ), + new AuditedPropertiesReader.ComponentPropertiesSource( classLoaderService, reflectionManager, component ), auditData, mainGenerator.getGlobalCfg(), reflectionManager, "" ).read(); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AnnotationsMetadataReader.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AnnotationsMetadataReader.java index f31c67a394..aba0eb3886 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AnnotationsMetadataReader.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AnnotationsMetadataReader.java @@ -7,7 +7,6 @@ package org.hibernate.envers.configuration.internal.metadata.reader; import org.hibernate.MappingException; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.envers.AuditTable; @@ -96,30 +95,25 @@ public final class AnnotationsMetadataReader { return auditData; } - try { - final XClass xclass = reflectionManager.classForName( pc.getClassName() ); + final XClass xclass = reflectionManager.toXClass( pc.getMappedClass() ); - final ModificationStore defaultStore = getDefaultAudited( xclass ); - if ( defaultStore != null ) { - auditData.setDefaultAudited( true ); - } - - new AuditedPropertiesReader( - defaultStore, - new PersistentClassPropertiesSource( xclass ), - auditData, - globalCfg, - reflectionManager, - "" - ).read(); - - addAuditTable( xclass ); - addAuditSecondaryTables( xclass ); - } - catch (ClassLoadingException e) { - throw new MappingException( e ); + final ModificationStore defaultStore = getDefaultAudited( xclass ); + if ( defaultStore != null ) { + auditData.setDefaultAudited( true ); } + new AuditedPropertiesReader( + defaultStore, + new PersistentClassPropertiesSource( xclass ), + auditData, + globalCfg, + reflectionManager, + "" + ).read(); + + addAuditTable( xclass ); + addAuditSecondaryTables( xclass ); + return auditData; } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java index 291028d16b..25494642b9 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/reader/AuditedPropertiesReader.java @@ -25,10 +25,11 @@ import javax.persistence.Version; import org.hibernate.HibernateException; import org.hibernate.MappingException; -import org.hibernate.annotations.common.reflection.ClassLoadingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XProperty; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.envers.AuditJoinTable; import org.hibernate.envers.AuditMappedBy; import org.hibernate.envers.AuditOverride; @@ -432,8 +433,10 @@ public class AuditedPropertiesReader { // Marking component properties as placed directly in class (not inside another component). componentData.setBeanName( null ); + final ClassLoaderService classLoaderService = globalCfg.getEnversService().getClassLoaderService(); + final PersistentPropertiesSource componentPropertiesSource = new ComponentPropertiesSource( - reflectionManager, + classLoaderService, reflectionManager, propertyValue ); final AuditedPropertiesReader audPropReader = new AuditedPropertiesReader( @@ -459,7 +462,10 @@ public class AuditedPropertiesReader { componentPropertiesSource = new DynamicComponentSource( reflectionManager, propertyValue, property ); } else { - componentPropertiesSource = new ComponentPropertiesSource( reflectionManager, propertyValue ); + final ClassLoaderService classLoaderService = this.globalCfg.getEnversService().getClassLoaderService(); + componentPropertiesSource = new ComponentPropertiesSource( + classLoaderService, + reflectionManager, propertyValue ); } final ComponentAuditedPropertiesReader audPropReader = new ComponentAuditedPropertiesReader( @@ -786,14 +792,17 @@ public class AuditedPropertiesReader { this.component = component; } - public ComponentPropertiesSource(ReflectionManager reflectionManager, Component component) { + public ComponentPropertiesSource( + ClassLoaderService classLoaderService, + ReflectionManager reflectionManager, + Component component) { try { - this.xclass = reflectionManager.classForName( component.getComponentClassName() ); + Class objectClass = classLoaderService.classForName( component.getComponentClassName() ); + this.xclass = reflectionManager.toXClass( objectClass ); } catch ( ClassLoadingException e ) { throw new MappingException( e ); } - this.component = component; }