Skip to content

Commit

Permalink
Fix cast exception when unwrapping ArrayTypeDescrptor which component…
Browse files Browse the repository at this point in the history
… type is an Annotationusage
  • Loading branch information
dreab8 authored and sebersole committed Mar 25, 2024
1 parent a805073 commit 0c9041d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.models.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.List;

Expand All @@ -32,15 +33,17 @@
*/
public class ArrayTypeDescriptor<V> implements ValueTypeDescriptor<List<V>> {
private final ValueTypeDescriptor<V> elementTypeDescriptor;
private final Class<?> componentType;

private ValueWrapper<List<V>, AnnotationValue> jandexValueWrapper;
private ValueExtractor<AnnotationInstance,List<V>> jandexValueExtractor;

private ValueWrapper<List<V>,Object[]> jdkValueWrapper;
private ValueExtractor<Annotation,List<V>> jdkValueExtractor;

public ArrayTypeDescriptor(ValueTypeDescriptor<V> elementTypeDescriptor) {
public ArrayTypeDescriptor(ValueTypeDescriptor<V> elementTypeDescriptor, Class<?> componentType) {
this.elementTypeDescriptor = elementTypeDescriptor;
this.componentType = componentType;
}

@Override
Expand Down Expand Up @@ -123,9 +126,9 @@ public ValueWrapper<List<V>,Object[]> resolveJkWrapper(SourceModelBuildingContex

@Override
public Object unwrap(List<V> value) {
final Object[] result = new Object[value.size()];
final Object[] result = (Object[]) Array.newInstance( componentType, value.size() );
for ( int i = 0; i < value.size(); i++ ) {
result[i] = elementTypeDescriptor.unwrap( value.get(i) );
result[i] = elementTypeDescriptor.unwrap( value.get( i ) );
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static <T,W> ValueTypeDescriptor<W> resolveTypeDescriptor(Class<T> attrib
if ( attributeType.isArray() ) {
final Class<?> componentType = attributeType.getComponentType();
final ValueTypeDescriptor<?> elementTypeDescriptor = resolveTypeDescriptor( componentType );
return (ValueTypeDescriptor<W>) new ArrayTypeDescriptor<>( elementTypeDescriptor );
return (ValueTypeDescriptor<W>) new ArrayTypeDescriptor<>( elementTypeDescriptor, componentType );
}

if ( attributeType.isEnum() ) {
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/hibernate/models/dynamic/ToAnnotationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.hibernate.models.dynamic;

import org.hibernate.models.internal.SourceModelBuildingContextImpl;
import org.hibernate.models.internal.dynamic.DynamicAnnotationUsage;
import org.hibernate.models.internal.dynamic.DynamicClassDetails;
import org.hibernate.models.orm.JpaAnnotations;

import org.junit.jupiter.api.Test;

import jakarta.persistence.JoinTable;

import static org.hibernate.models.SourceModelTestHelper.createBuildingContext;

public class ToAnnotationTest {

@Test
void testAccessArrayOfAnnotations() {
final SourceModelBuildingContextImpl buildingContext = createBuildingContext();
final DynamicClassDetails dynamicEntity = new DynamicClassDetails( "DynamicEntity", buildingContext );
final DynamicAnnotationUsage<JoinTable> generatorAnn = new DynamicAnnotationUsage<>(
JpaAnnotations.JOIN_TABLE,
dynamicEntity,
buildingContext
);

JoinTable joinTableAnn = generatorAnn.toAnnotation();

joinTableAnn.indexes();
}
}

0 comments on commit 0c9041d

Please sign in to comment.