HHH-13061 Introduce a 'none' option for BytecodeProvider implementation choice

This commit is contained in:
Sanne Grinovero 2018-10-16 17:24:54 +01:00 committed by Guillaume Smet
parent 817e463764
commit 04fbabce2e
5 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/*
* 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.none;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.cfg.AvailableSettings;
/**
* This BytecodeProvider represents the "no-op" enhancer; mostly useful
* as an optimisation when not needing any byte code optimisation applied,
* for example when the entities have been enhanced at compile time.
* Choosing this BytecodeProvider allows to exclude the bytecode enhancement
* libraries from the runtime classpath, but is not compatible
* with the option AvailableSettings#USE_REFLECTION_OPTIMIZER .
*
* @since 5.4
*/
public final class BytecodeProviderImpl implements BytecodeProvider {
@Override
public ProxyFactoryFactory getProxyFactoryFactory() {
return new NoProxyFactoryFactory();
}
@Override
public ReflectionOptimizer getReflectionOptimizer(
Class clazz,
String[] getterNames,
String[] setterNames,
Class[] types) {
throw new HibernateException( "Using the ReflectionOptimizer is not possible when the configured BytecodeProvider is 'none'. Disable " + AvailableSettings.USE_REFLECTION_OPTIMIZER + " or use a different BytecodeProvider");
}
@Override
public Enhancer getEnhancer(EnhancementContext enhancementContext) {
return null;
}
}

View File

@ -0,0 +1,40 @@
/*
* 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.none;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.type.CompositeType;
final class DisallowedProxyFactory implements ProxyFactory {
static final DisallowedProxyFactory INSTANCE = new DisallowedProxyFactory();
@Override
public void postInstantiate(
String entityName,
Class persistentClass,
Set<Class> interfaces,
Method getIdentifierMethod,
Method setIdentifierMethod,
CompositeType componentIdType) throws HibernateException {
}
@Override
public HibernateProxy getProxy(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
throw new HibernateException( "Generation of HibernateProxy instances at runtime is not allowed when the configured BytecodeProvider is 'none'; your model requires a more advanced BytecodeProvider to be enabled." );
}
}

View File

@ -0,0 +1,29 @@
/*
* 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.none;
import org.hibernate.bytecode.spi.BasicProxyFactory;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.proxy.ProxyFactory;
/**
* When entities are enhanced in advance, proxies are not needed.
*/
final class NoProxyFactoryFactory implements ProxyFactoryFactory {
@Override
public ProxyFactory buildProxyFactory(SessionFactoryImplementor sessionFactory) {
return DisallowedProxyFactory.INSTANCE;
}
@Override
public BasicProxyFactory buildBasicProxyFactory(Class superClass, Class[] interfaces) {
return new NoneBasicProxyFactory( superClass, interfaces );
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.none;
import java.util.Arrays;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.spi.BasicProxyFactory;
/**
* This is used to instantiate "components" which are either Abstract or defined by an Interface.
*/
final class NoneBasicProxyFactory implements BasicProxyFactory {
private final Class superClass;
private final Class[] interfaces;
public NoneBasicProxyFactory(Class superClass, Class[] interfaces) {
this.superClass = superClass;
this.interfaces = interfaces;
}
@Override
public Object getProxy() {
throw new HibernateException( "NoneBasicProxyFactory is unable to generate a BasicProxy for type " + superClass + " and interfaces " + Arrays.toString( interfaces ) + ". Enable a different BytecodeProvider." );
}
}

View File

@ -329,6 +329,7 @@ public final class Environment implements AvailableSettings {
public static final String BYTECODE_PROVIDER_NAME_JAVASSIST = "javassist"; public static final String BYTECODE_PROVIDER_NAME_JAVASSIST = "javassist";
public static final String BYTECODE_PROVIDER_NAME_BYTEBUDDY = "bytebuddy"; public static final String BYTECODE_PROVIDER_NAME_BYTEBUDDY = "bytebuddy";
public static final String BYTECODE_PROVIDER_NAME_NONE = "none";
public static final String BYTECODE_PROVIDER_NAME_DEFAULT = BYTECODE_PROVIDER_NAME_BYTEBUDDY; public static final String BYTECODE_PROVIDER_NAME_DEFAULT = BYTECODE_PROVIDER_NAME_BYTEBUDDY;
public static BytecodeProvider buildBytecodeProvider(Properties properties) { public static BytecodeProvider buildBytecodeProvider(Properties properties) {
@ -337,6 +338,9 @@ public final class Environment implements AvailableSettings {
} }
private static BytecodeProvider buildBytecodeProvider(String providerName) { private static BytecodeProvider buildBytecodeProvider(String providerName) {
if ( BYTECODE_PROVIDER_NAME_NONE.equals( providerName ) ) {
return new org.hibernate.bytecode.internal.none.BytecodeProviderImpl();
}
if ( BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( providerName ) ) { if ( BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( providerName ) ) {
return new org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl(); return new org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl();
} }