Skip to content

Commit

Permalink
#103 - Problem with ArrayTypeDetailsImpl#getName
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Oct 22, 2024
1 parent 9404a36 commit 9d030f5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ) {
Expand Down

0 comments on commit 9d030f5

Please sign in to comment.