From 9d030f55d28811549cba9874d9572b572c8e4420 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 22 Oct 2024 12:29:56 -0500 Subject: [PATCH] #103 - Problem with ArrayTypeDetailsImpl#getName --- .../models/internal/ArrayTypeDetailsImpl.java | 24 +----- .../MultiDimensionalArrayTypeTests.java | 80 +++++++++++++++++++ .../models/SourceModelTestHelper.java | 19 ++++- 3 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 hibernate-models/src/test/java/org/hibernate/models/MultiDimensionalArrayTypeTests.java diff --git a/hibernate-models/src/main/java/org/hibernate/models/internal/ArrayTypeDetailsImpl.java b/hibernate-models/src/main/java/org/hibernate/models/internal/ArrayTypeDetailsImpl.java index 61d2651..a4ebd42 100644 --- a/hibernate-models/src/main/java/org/hibernate/models/internal/ArrayTypeDetailsImpl.java +++ b/hibernate-models/src/main/java/org/hibernate/models/internal/ArrayTypeDetailsImpl.java @@ -43,29 +43,7 @@ public int getDimensions() { @Override public String getName() { - StringBuilder builder = new StringBuilder(); - - TypeDetails type = this; - while ( type.getTypeKind() == Kind.ARRAY ) { - int dimensions = type.asArrayType().getDimensions(); - while ( dimensions-- > 0 ) { - builder.append( '[' ); - } - type = type.asArrayType().getConstituentType(); - } - - // here, `type` is an element type of the array, i.e., never array - if ( type.getTypeKind() == Kind.PRIMITIVE ) { - builder.append( type.asPrimitiveType().toCode() ); - } - else { - // This relies on name() representing the erased type name - // For historical 1.x reasons, we follow the Java reflection format - // instead of the Java descriptor format. - builder.append( 'L' ).append( type.getName() ).append( ';' ); - } - - return builder.toString(); + return arrayClassDetails.getName(); } @Override diff --git a/hibernate-models/src/test/java/org/hibernate/models/MultiDimensionalArrayTypeTests.java b/hibernate-models/src/test/java/org/hibernate/models/MultiDimensionalArrayTypeTests.java new file mode 100644 index 0000000..34e8bc5 --- /dev/null +++ b/hibernate-models/src/test/java/org/hibernate/models/MultiDimensionalArrayTypeTests.java @@ -0,0 +1,80 @@ +package org.hibernate.models; + +import org.hibernate.models.internal.ArrayTypeDetailsImpl; +import org.hibernate.models.internal.ClassTypeDetailsImpl; +import org.hibernate.models.internal.jdk.JdkClassDetails; +import org.hibernate.models.spi.ClassDetails; +import org.hibernate.models.spi.SourceModelBuildingContext; +import org.hibernate.models.spi.TypeDetails; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Steve Ebersole + */ +public class MultiDimensionalArrayTypeTests { + + @Test + void testIntegerArrays() { + final SourceModelBuildingContext buildingContext = SourceModelTestHelper.createBuildingContext( + (contributions, ctx) -> { + final ClassDetails intDetails = new JdkClassDetails( Integer.class, ctx ); + final ClassDetails intArrayDetails = new JdkClassDetails( Integer[].class, ctx ); + final ClassDetails intIntArrayDetails = new JdkClassDetails( Integer[][].class, ctx ); + final ClassDetails intIntIntArrayDetails = new JdkClassDetails( Integer[][][].class, ctx ); + + contributions.registerClass( intDetails ); + contributions.registerClass( intArrayDetails ); + contributions.registerClass( intIntArrayDetails ); + contributions.registerClass( intIntIntArrayDetails ); + } + ); + + final ClassDetails intDetails = buildingContext.getClassDetailsRegistry().getClassDetails( Integer[][][].class.getName() ); + final ClassTypeDetailsImpl intTypeDetails = new ClassTypeDetailsImpl( intDetails, TypeDetails.Kind.CLASS ); + + final ClassDetails intArrayDetails = buildingContext.getClassDetailsRegistry().getClassDetails( Integer[].class.getName() ); + final ArrayTypeDetailsImpl intArrayTypeDetails = new ArrayTypeDetailsImpl( intArrayDetails, intTypeDetails ); + + final ClassDetails intIntArrayDetails = buildingContext.getClassDetailsRegistry().getClassDetails( Integer[][].class.getName() ); + final ArrayTypeDetailsImpl intIntArrayTypeDetails = new ArrayTypeDetailsImpl( intIntArrayDetails, intArrayTypeDetails ); + + final ClassDetails intIntIntArrayDetails = buildingContext.getClassDetailsRegistry().getClassDetails( Integer[][][].class.getName() ); + final ArrayTypeDetailsImpl intIntIntArrayTypeDetails = new ArrayTypeDetailsImpl( intIntIntArrayDetails, intIntArrayTypeDetails ); + + assertThat( intIntIntArrayTypeDetails.getName() ).isEqualTo( "[[[Ljava.lang.Integer;" ); + } + + @Test + void testIntArrays() { + final SourceModelBuildingContext buildingContext = SourceModelTestHelper.createBuildingContext( + (contributions, ctx) -> { + final ClassDetails intDetails = new JdkClassDetails( int.class, ctx ); + final ClassDetails intArrayDetails = new JdkClassDetails( int[].class, ctx ); + final ClassDetails intIntArrayDetails = new JdkClassDetails( int[][].class, ctx ); + final ClassDetails intIntIntArrayDetails = new JdkClassDetails( int[][][].class, ctx ); + + contributions.registerClass( intDetails ); + contributions.registerClass( intArrayDetails ); + contributions.registerClass( intIntArrayDetails ); + contributions.registerClass( intIntIntArrayDetails ); + } + ); + + final ClassDetails intDetails = buildingContext.getClassDetailsRegistry().getClassDetails( int[][][].class.getName() ); + final ClassTypeDetailsImpl intTypeDetails = new ClassTypeDetailsImpl( intDetails, TypeDetails.Kind.CLASS ); + + final ClassDetails intArrayDetails = buildingContext.getClassDetailsRegistry().getClassDetails( int[].class.getName() ); + final ArrayTypeDetailsImpl intArrayTypeDetails = new ArrayTypeDetailsImpl( intArrayDetails, intTypeDetails ); + + final ClassDetails intIntArrayDetails = buildingContext.getClassDetailsRegistry().getClassDetails( int[][].class.getName() ); + final ArrayTypeDetailsImpl intIntArrayTypeDetails = new ArrayTypeDetailsImpl( intIntArrayDetails, intArrayTypeDetails ); + + final ClassDetails intIntIntArrayDetails = buildingContext.getClassDetailsRegistry().getClassDetails( int[][][].class.getName() ); + final ArrayTypeDetailsImpl intIntIntArrayTypeDetails = new ArrayTypeDetailsImpl( intIntIntArrayDetails, intIntArrayTypeDetails ); + + assertThat( intIntIntArrayTypeDetails.getName() ).isEqualTo( "[[[I" ); + } +} diff --git a/hibernate-models/src/test/java/org/hibernate/models/SourceModelTestHelper.java b/hibernate-models/src/test/java/org/hibernate/models/SourceModelTestHelper.java index b36c43d..ceff578 100644 --- a/hibernate-models/src/test/java/org/hibernate/models/SourceModelTestHelper.java +++ b/hibernate-models/src/test/java/org/hibernate/models/SourceModelTestHelper.java @@ -8,6 +8,7 @@ import org.hibernate.models.internal.util.CollectionHelper; import org.hibernate.models.orm.JpaAnnotations; import org.hibernate.models.spi.ClassLoading; +import org.hibernate.models.spi.RegistryPrimer; import org.hibernate.models.spi.SourceModelBuildingContext; import static org.hibernate.models.internal.SimpleClassLoading.SIMPLE_CLASS_LOADING; @@ -25,10 +26,24 @@ public static SourceModelBuildingContext createBuildingContext(Class... model public static SourceModelBuildingContext createBuildingContext( ClassLoading classLoadingAccess, Class... modelClasses) { - final SourceModelBuildingContext ctx = new BasicModelBuildingContextImpl( + return createBuildingContext( classLoadingAccess, - (contributions, buildingContext1) -> JpaAnnotations.forEachAnnotation( contributions::registerAnnotation ) + (contributions, buildingContext) -> JpaAnnotations.forEachAnnotation( contributions::registerAnnotation ), + modelClasses ); + } + + public static SourceModelBuildingContext createBuildingContext( + RegistryPrimer registryPrimer, + Class... modelClasses) { + return createBuildingContext( SIMPLE_CLASS_LOADING, registryPrimer, modelClasses ); + } + + public static SourceModelBuildingContext createBuildingContext( + ClassLoading classLoadingAccess, + RegistryPrimer registryPrimer, + Class... modelClasses) { + final SourceModelBuildingContext ctx = new BasicModelBuildingContextImpl( classLoadingAccess, registryPrimer ); if ( CollectionHelper.isNotEmpty( modelClasses ) ) { for ( Class modelClass : modelClasses ) {