HHH-13849 Convert BytecodeProvider into a Service as well

This commit is contained in:
Sanne Grinovero 2020-02-04 20:29:19 +00:00
parent e19acd5a9c
commit 5c81089d65
6 changed files with 55 additions and 11 deletions

View File

@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.bytecode.internal;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.cfg.Environment;
import org.hibernate.service.spi.ServiceRegistryImplementor;
public final class BytecodeProviderInitiator implements StandardServiceInitiator<BytecodeProvider> {
/**
* Singleton access
*/
public static final StandardServiceInitiator<BytecodeProvider> INSTANCE = new BytecodeProviderInitiator();
@Override
public BytecodeProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
// TODO in 6 this will no longer use Environment, which is configured via global environment variables,
// but move to a component which can be reconfigured differently in each registry.
return Environment.getBytecodeProvider();
}
@Override
public Class<BytecodeProvider> getServiceInitiated() {
return BytecodeProvider.class;
}
}

View File

@ -9,10 +9,16 @@ package org.hibernate.bytecode.internal;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.cfg.Environment;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* Most commonly the {@link ProxyFactoryFactory} will depend directly on the chosen {@link BytecodeProvider},
* however by registering them as two separate services we can allow to override either one
* or both of them.
* @author Sanne Grinovero
*/
public final class ProxyFactoryFactoryInitiator implements StandardServiceInitiator<ProxyFactoryFactory> {
/**
@ -22,7 +28,8 @@ public final class ProxyFactoryFactoryInitiator implements StandardServiceInitia
@Override
public ProxyFactoryFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
return Environment.getBytecodeProvider().getProxyFactoryFactory();
final BytecodeProvider bytecodeProvider = registry.getService( BytecodeProvider.class );
return bytecodeProvider.getProxyFactoryFactory();
}
@Override

View File

@ -8,6 +8,7 @@ package org.hibernate.bytecode.spi;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.service.Service;
/**
* Contract for providers of bytecode services to Hibernate.
@ -19,7 +20,7 @@ import org.hibernate.bytecode.enhance.spi.Enhancer;
*
* @author Steve Ebersole
*/
public interface BytecodeProvider {
public interface BytecodeProvider extends Service {
/**
* Retrieve the specific factory for this provider capable of
* generating run-time proxies for lazy-loading purposes.

View File

@ -12,6 +12,7 @@ import java.util.List;
import org.hibernate.boot.cfgxml.internal.CfgXmlAccessServiceInitiator;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.bytecode.internal.BytecodeProviderInitiator;
import org.hibernate.bytecode.internal.ProxyFactoryFactoryInitiator;
import org.hibernate.cache.internal.RegionFactoryInitiator;
import org.hibernate.engine.config.internal.ConfigurationServiceInitiator;
@ -53,6 +54,7 @@ public final class StandardServiceInitiators {
private static List<StandardServiceInitiator> buildStandardServiceInitiatorList() {
final ArrayList<StandardServiceInitiator> serviceInitiators = new ArrayList<StandardServiceInitiator>();
serviceInitiators.add( BytecodeProviderInitiator.INSTANCE );
serviceInitiators.add( ProxyFactoryFactoryInitiator.INSTANCE );
serviceInitiators.add( CfgXmlAccessServiceInitiator.INSTANCE );

View File

@ -5,12 +5,14 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.tuple.component;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.spi.BasicProxyFactory;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.cfg.Environment;
@ -68,9 +70,8 @@ public class PojoComponentTuplizer extends AbstractComponentTuplizer {
optimizer = null;
}
else {
// TODO: here is why we need to make bytecode provider global :(
// TODO : again, fix this after HHH-1907 is complete
optimizer = Environment.getBytecodeProvider().getReflectionOptimizer(
final BytecodeProvider bytecodeProvider = component.getServiceRegistry().getService( BytecodeProvider.class );
optimizer = bytecodeProvider.getReflectionOptimizer(
componentClass, getterNames, setterNames, propTypes
);
}

View File

@ -18,6 +18,7 @@ import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.cfg.Environment;
@ -76,16 +77,13 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer {
optimizer = null;
}
else {
// todo : YUCK!!!
optimizer = Environment.getBytecodeProvider().getReflectionOptimizer(
final BytecodeProvider bytecodeProvider = entityMetamodel.getSessionFactory().getServiceRegistry().getService( BytecodeProvider.class );
optimizer = bytecodeProvider.getReflectionOptimizer(
mappedClass,
getterNames,
setterNames,
propTypes
);
// optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
// mappedClass, getterNames, setterNames, propTypes
// );
}
}