From 04fbabce2e51b79ee1d20b8093916113d450e903 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 16 Oct 2018 17:24:54 +0100 Subject: [PATCH] HHH-13061 Introduce a 'none' option for BytecodeProvider implementation choice --- .../internal/none/BytecodeProviderImpl.java | 47 +++++++++++++++++++ .../internal/none/DisallowedProxyFactory.java | 40 ++++++++++++++++ .../internal/none/NoProxyFactoryFactory.java | 29 ++++++++++++ .../internal/none/NoneBasicProxyFactory.java | 32 +++++++++++++ .../java/org/hibernate/cfg/Environment.java | 4 ++ 5 files changed, 152 insertions(+) create mode 100644 hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/BytecodeProviderImpl.java create mode 100644 hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/DisallowedProxyFactory.java create mode 100644 hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoProxyFactoryFactory.java create mode 100644 hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoneBasicProxyFactory.java diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/BytecodeProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/BytecodeProviderImpl.java new file mode 100644 index 0000000000..e249e2746d --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/BytecodeProviderImpl.java @@ -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 . + */ +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; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/DisallowedProxyFactory.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/DisallowedProxyFactory.java new file mode 100644 index 0000000000..1ae529ffd9 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/DisallowedProxyFactory.java @@ -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 . + */ +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 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." ); + } + +} diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoProxyFactoryFactory.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoProxyFactoryFactory.java new file mode 100644 index 0000000000..40b9b9e253 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoProxyFactoryFactory.java @@ -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 . + */ +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 ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoneBasicProxyFactory.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoneBasicProxyFactory.java new file mode 100644 index 0000000000..5d1986d16c --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/none/NoneBasicProxyFactory.java @@ -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 . + */ +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." ); + } + +} diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java b/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java index 8c58cc4a6b..f0f2c1376d 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java @@ -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_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 BytecodeProvider buildBytecodeProvider(Properties properties) { @@ -337,6 +338,9 @@ public final class Environment implements AvailableSettings { } 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 ) ) { return new org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl(); }