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

Fix cast exception when unwrapping ArrayTypeDescrptor which component type is an Annotationusage #51

Merged
merged 1 commit into from
Mar 25, 2024
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
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();
}
}
Loading