Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-19050 Allow configuration of EntityManagerFactoryBuilderImpl to override the BytecodeProvider instance #9643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.hibernate.bytecode.enhance.spi;

import org.hibernate.bytecode.spi.BytecodeProvider;

import jakarta.persistence.metamodel.Type;
import org.hibernate.Incubating;

Expand Down Expand Up @@ -156,4 +158,15 @@ public interface EnhancementContext {
default UnsupportedEnhancementStrategy getUnsupportedEnhancementStrategy() {
return UnsupportedEnhancementStrategy.SKIP;
}

/**
* Allows to force the use of a specific instance of BytecodeProvider to perform the enhancement.
* @return When returning {code null} the default implementation will be used. Only return a different instance if
* you need to override the default implementation.
*/
@Incubating
default BytecodeProvider getBytecodeProvider() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public interface BytecodeSettings {
@Deprecated( forRemoval = true )
String BYTECODE_PROVIDER = "hibernate.bytecode.provider";

/**
* This is similar to the now deprecated legacy property {@code hibernate.bytecode.provider} except
* it's used specifically to pass an existing instance of a {@link org.hibernate.bytecode.spi.BytecodeProvider};
* this happens to also allow to override the implementation, but is primarily intended to allow reusing a
* specific instance; this could be useful when the implementation benefits from internal caches.
* When not set, Hibernate will create its default implementation.
*
* @settingDefault {@code null}
*/
String BYTECODE_PROVIDER_INSTANCE = "hibernate.enhancer.bytecodeprovider.instance";

/**
* Enable association management feature in runtime bytecode enhancement
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.hibernate.bytecode.enhance.spi.EnhancementException;
import org.hibernate.bytecode.enhance.spi.UnloadedClass;
import org.hibernate.bytecode.enhance.spi.UnloadedField;
import org.hibernate.bytecode.spi.BytecodeProvider;
import org.hibernate.bytecode.spi.ClassTransformer;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand Down Expand Up @@ -113,6 +114,7 @@
import static org.hibernate.cfg.AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY;
import static org.hibernate.cfg.AvailableSettings.URL;
import static org.hibernate.cfg.AvailableSettings.USER;
import static org.hibernate.cfg.BytecodeSettings.BYTECODE_PROVIDER_INSTANCE;
import static org.hibernate.cfg.BytecodeSettings.ENHANCER_ENABLE_ASSOCIATION_MANAGEMENT;
import static org.hibernate.cfg.BytecodeSettings.ENHANCER_ENABLE_DIRTY_TRACKING;
import static org.hibernate.cfg.BytecodeSettings.ENHANCER_ENABLE_LAZY_INITIALIZATION;
Expand Down Expand Up @@ -422,6 +424,11 @@ protected EnhancementContext getEnhancementContext(
final boolean dirtyTrackingEnabled,
final boolean lazyInitializationEnabled,
final boolean associationManagementEnabled ) {
final Object propValue = configurationValues.get( BYTECODE_PROVIDER_INSTANCE );
if ( propValue != null && ( ! ( propValue instanceof BytecodeProvider ) ) ) {
throw new PersistenceException( "Property " + BYTECODE_PROVIDER_INSTANCE + " was set to '" + propValue + "', which is not compatible with the expected type " + BytecodeProvider.class );
}
final BytecodeProvider overriddenBytecodeProvider = (BytecodeProvider) propValue;
return new DefaultEnhancementContext() {

@Override
Expand Down Expand Up @@ -461,6 +468,11 @@ public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
// doesn't make any sense to have extended enhancement enabled at runtime. we only enhance entities anyway.
return false;
}

@Override
public BytecodeProvider getBytecodeProvider() {
return overriddenBytecodeProvider;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class EnhancingClassTransformerImpl implements ClassTransformer {
public EnhancingClassTransformerImpl(EnhancementContext enhancementContext) {
Objects.requireNonNull( enhancementContext );
this.enhancementContext = enhancementContext;
this.bytecodeProvider = BytecodeProviderInitiator.buildDefaultBytecodeProvider();
final BytecodeProvider overriddenProvider = enhancementContext.getBytecodeProvider();
this.bytecodeProvider = overriddenProvider == null ? BytecodeProviderInitiator.buildDefaultBytecodeProvider() : overriddenProvider;
}

@Override
Expand Down
Loading