Skip to content

#125 - Consolidate SourceModelContext and SourceModelBuildingContext #127

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

Merged
merged 1 commit into from
Apr 16, 2025
Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@
import java.util.Map;

import org.hibernate.models.bytebuddy.Settings;
import org.hibernate.models.internal.BasicModelBuildingContextImpl;
import org.hibernate.models.internal.BasicModelsContextImpl;
import org.hibernate.models.spi.ClassLoading;
import org.hibernate.models.spi.ModelsContext;
import org.hibernate.models.spi.RegistryPrimer;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.SourceModelBuildingContextProvider;
import org.hibernate.models.spi.ModelsContextProvider;

import net.bytebuddy.pool.TypePool;

/**
* @author Steve Ebersole
*/
public class ByteBuddyContextProvider implements SourceModelBuildingContextProvider {
public class ByteBuddyContextProvider implements ModelsContextProvider {
public static final ByteBuddyContextProvider BYTEBUDDY_PROVIDER = new ByteBuddyContextProvider();

@Override
public SourceModelBuildingContext produceContext(
public ModelsContext produceContext(
ClassLoading classLoading,
RegistryPrimer registryPrimer,
Map<Object, Object> configProperties) {
final TypePool typePool = resolveTypePool( configProperties );

if ( typePool != null ) {
return new ByteBuddyModelContextImpl( typePool, classLoading, registryPrimer );
return new ByteBuddyModelsContextImpl( typePool, classLoading, registryPrimer );
}

return new BasicModelBuildingContextImpl( classLoading, registryPrimer );
return new BasicModelsContextImpl( classLoading, registryPrimer );
}

private TypePool resolveTypePool(Map<Object, Object> configProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import java.util.Map;

import org.hibernate.models.bytebuddy.spi.ByteBuddyModelsContext;
import org.hibernate.models.bytebuddy.spi.ValueConverter;
import org.hibernate.models.bytebuddy.spi.ValueExtractor;
import org.hibernate.models.internal.AbstractModelBuildingContext;
import org.hibernate.models.internal.AbstractModelsContext;
import org.hibernate.models.internal.AnnotationDescriptorRegistryStandard;
import org.hibernate.models.internal.MutableAnnotationDescriptorRegistry;
import org.hibernate.models.internal.MutableClassDetailsRegistry;
import org.hibernate.models.internal.SimpleClassLoading;
import org.hibernate.models.serial.internal.StorableContextImpl;
import org.hibernate.models.serial.spi.StorableContext;
import org.hibernate.models.spi.ClassLoading;
Expand All @@ -24,29 +22,22 @@
import net.bytebuddy.pool.TypePool;

/**
* SourceModelBuildingContext implementation based on ByteBuddy, leveraging a
* {@linkplain TypePool} to inspect
* Implementation of ByteBuddyModelsContext
*
* @author Steve Ebersole
*/
public class ByteBuddyModelContextImpl
extends AbstractModelBuildingContext
public class ByteBuddyModelsContextImpl
extends AbstractModelsContext
implements ByteBuddyModelsContext {
private final TypePool typePool;

private final ClassDetailsRegistryImpl classDetailsRegistry;
private final AnnotationDescriptorRegistryStandard descriptorRegistry;

private final Map<ValueTypeDescriptor, ValueConverter> valueConverters = new HashMap<>();
@SuppressWarnings("rawtypes")
private final Map<ValueTypeDescriptor, ValueExtractor> valueExtractors = new HashMap<>();

public ByteBuddyModelContextImpl(
TypePool typePool,
RegistryPrimer registryPrimer) {
this( typePool, SimpleClassLoading.SIMPLE_CLASS_LOADING, registryPrimer );
}

public ByteBuddyModelContextImpl(
public ByteBuddyModelsContextImpl(
TypePool typePool,
ClassLoading classLoading,
RegistryPrimer registryPrimer) {
Expand Down Expand Up @@ -80,22 +71,6 @@ public StorableContext toStorableForm() {
return new StorableContextImpl( classDetailsRegistry.classDetailsMap(), descriptorRegistry.descriptorMap() );
}

@Override
public <V> ValueConverter<V> getValueConverter(ValueTypeDescriptor<V> valueTypeDescriptor) {
//noinspection unchecked
final ValueConverter<V> existing = valueConverters.get( valueTypeDescriptor );
if ( existing != null ) {
return existing;
}

return ByteBuddyBuilders.buildValueHandlersReturnConverter(
valueTypeDescriptor,
valueConverters::put,
valueExtractors::put,
this
);
}

@Override
public <V> ValueExtractor<V> getValueExtractor(ValueTypeDescriptor<V> valueTypeDescriptor) {
//noinspection unchecked
Expand All @@ -104,11 +79,11 @@ public <V> ValueExtractor<V> getValueExtractor(ValueTypeDescriptor<V> valueTypeD
return existing;
}

return ByteBuddyBuilders.buildValueHandlersReturnExtractor(
final ValueExtractor<V> valueExtractor = ByteBuddyBuilders.buildValueExtractor(
valueTypeDescriptor,
valueConverters::put,
valueExtractors::put,
this
);
valueExtractors.put( valueTypeDescriptor, valueExtractor );
return valueExtractor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.hibernate.models.bytebuddy.spi.ByteBuddyModelsContext;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsBuilder;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.ModelsContext;

/**
* @author Steve Ebersole
Expand All @@ -20,8 +20,8 @@ public ClassDetailsBuilderImpl(ByteBuddyModelsContext modelContext) {
}

@Override
public ClassDetails buildClassDetails(String name, SourceModelBuildingContext buildingContext) {
assert buildingContext == modelContext;
public ClassDetails buildClassDetails(String name, ModelsContext modelsContext) {
assert modelsContext == modelContext;
return ByteBuddyBuilders.buildDetails( name, modelContext );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import org.hibernate.models.spi.FieldDetails;
import org.hibernate.models.spi.MethodDetails;
import org.hibernate.models.spi.RecordComponentDetails;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.models.spi.SourceModelContext;
import org.hibernate.models.spi.ModelsContext;
import org.hibernate.models.spi.TypeDetails;
import org.hibernate.models.spi.TypeVariableDetails;

Expand Down Expand Up @@ -202,7 +201,7 @@ public <X> Class<X> toJavaClass() {
}

@Override
public <X> Class<X> toJavaClass(ClassLoading classLoading, SourceModelContext modelContext) {
public <X> Class<X> toJavaClass(ClassLoading classLoading, ModelsContext modelContext) {
return classLoading.classForName( getClassName() );
}

Expand All @@ -218,27 +217,27 @@ public String toString() {

private static ClassDetails determineSuperType(
TypeDescription typeDescription,
SourceModelBuildingContext buildingContext) {
ModelsContext modelsContext) {
if ( typeDescription.getSuperClass() == null ) {
return null;
}

return buildingContext
return modelsContext
.getClassDetailsRegistry()
.resolveClassDetails( typeDescription.getSuperClass().asRawType().getTypeName() );
}

private static TypeDetails determineGenericSuperType(TypeDescription typeDescription, SourceModelBuildingContext buildingContext) {
private static TypeDetails determineGenericSuperType(TypeDescription typeDescription, ModelsContext modelsContext) {
if ( typeDescription.getSuperClass() == null ) {
return null;
}

return TypeSwitchStandard.switchType( typeDescription.getSuperClass(), buildingContext );
return TypeSwitchStandard.switchType( typeDescription.getSuperClass(), modelsContext );
}

private static List<TypeDetails> determineInterfaces(
TypeDescription typeDescription,
SourceModelBuildingContext buildingContext) {
ModelsContext modelsContext) {
final TypeList.Generic interfaceTypes = typeDescription.getInterfaces();
if ( isEmpty( interfaceTypes ) ) {
return emptyList();
Expand All @@ -248,7 +247,7 @@ private static List<TypeDetails> determineInterfaces(
for ( TypeDescription.Generic interfaceType : interfaceTypes ) {
final TypeDetails switchedType = TypeSwitchStandard.switchType(
interfaceType,
buildingContext
modelsContext
);
result.add( switchedType );
}
Expand All @@ -258,15 +257,15 @@ private static List<TypeDetails> determineInterfaces(
private static List<TypeVariableDetails> determineTypeParameters(
TypeDescription typeDescription,
ClassDetailsImpl current,
SourceModelBuildingContext buildingContext) {
ModelsContext modelsContext) {
final TypeList.Generic typeArguments = typeDescription.getTypeVariables();
if ( CollectionHelper.isEmpty( typeArguments ) ) {
return emptyList();
}

final ArrayList<TypeVariableDetails> result = arrayList( typeArguments.size() );
for ( TypeDescription.Generic typeArgument : typeArguments ) {
result.add( (TypeVariableDetails) TypeSwitchStandard.switchType( typeArgument, current, buildingContext ) );
result.add( (TypeVariableDetails) TypeSwitchStandard.switchType( typeArgument, current, modelsContext ) );
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class ClassDetailsRegistryImpl extends AbstractClassDetailsRegistry {
private final ClassDetailsBuilderImpl classDetailsBuilder;

public ClassDetailsRegistryImpl(ByteBuddyModelContextImpl context) {
public ClassDetailsRegistryImpl(ByteBuddyModelsContextImpl context) {
super( context );
this.classDetailsBuilder = new ClassDetailsBuilderImpl( context );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.hibernate.models.spi.MutableClassDetails;
import org.hibernate.models.spi.MutableMemberDetails;
import org.hibernate.models.spi.RecordComponentDetails;
import org.hibernate.models.spi.SourceModelContext;
import org.hibernate.models.spi.ModelsContext;
import org.hibernate.models.spi.TypeDetails;

import net.bytebuddy.description.annotation.AnnotationSource;
Expand Down Expand Up @@ -107,7 +107,7 @@ public Field toJavaMember() {
public Field toJavaMember(
Class<?> declaringJavaClass,
ClassLoading classLoading,
SourceModelContext modelContext) {
ModelsContext modelContext) {
try {
return declaringJavaClass.getField( getName() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.hibernate.models.spi.MutableClassDetails;
import org.hibernate.models.spi.MutableMemberDetails;
import org.hibernate.models.spi.RecordComponentDetails;
import org.hibernate.models.spi.SourceModelContext;
import org.hibernate.models.spi.ModelsContext;
import org.hibernate.models.spi.TypeDetails;

import net.bytebuddy.description.annotation.AnnotationSource;
Expand Down Expand Up @@ -154,7 +154,7 @@ public Method toJavaMember() {
public Method toJavaMember(
Class<?> declaringJavaClass,
ClassLoading classLoading,
SourceModelContext modelContext) {
ModelsContext modelContext) {
methods: for ( Method method : declaringJavaClass.getDeclaredMethods() ) {
if ( !method.getName().equals( getName() ) ) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.hibernate.models.spi.MutableClassDetails;
import org.hibernate.models.spi.MutableMemberDetails;
import org.hibernate.models.spi.RecordComponentDetails;
import org.hibernate.models.spi.SourceModelContext;
import org.hibernate.models.spi.ModelsContext;
import org.hibernate.models.spi.TypeDetails;

import net.bytebuddy.description.annotation.AnnotationSource;
Expand Down Expand Up @@ -100,7 +100,7 @@ public Member toJavaMember() {
}

@Override
public Member toJavaMember(Class<?> declaringClass, ClassLoading classLoading, SourceModelContext modelContext) {
public Member toJavaMember(Class<?> declaringClass, ClassLoading classLoading, ModelsContext modelContext) {
// we could maybe resolve the corresponding method...
return null;
}
Expand Down
Loading