HHH-8962 allow PrimitiveTypeDescriptor to provide the actual Class

This commit is contained in:
Brett Meyer 2014-03-24 18:11:44 -04:00
parent e756bd0e0e
commit b6795294e6
5 changed files with 42 additions and 15 deletions

View File

@ -42,6 +42,7 @@ import org.hibernate.metamodel.reflite.spi.ClassDescriptor;
import org.hibernate.metamodel.reflite.spi.FieldDescriptor; import org.hibernate.metamodel.reflite.spi.FieldDescriptor;
import org.hibernate.metamodel.reflite.spi.JavaTypeDescriptor; import org.hibernate.metamodel.reflite.spi.JavaTypeDescriptor;
import org.hibernate.metamodel.reflite.spi.MethodDescriptor; import org.hibernate.metamodel.reflite.spi.MethodDescriptor;
import org.hibernate.metamodel.reflite.spi.PrimitiveTypeDescriptor;
import org.hibernate.metamodel.source.spi.AttributeSource; import org.hibernate.metamodel.source.spi.AttributeSource;
import org.hibernate.metamodel.source.spi.BasicPluralAttributeElementSource; import org.hibernate.metamodel.source.spi.BasicPluralAttributeElementSource;
import org.hibernate.metamodel.source.spi.ComponentAttributeSource; import org.hibernate.metamodel.source.spi.ComponentAttributeSource;
@ -522,12 +523,19 @@ class HibernateTypeHelper {
return typeFactory().list( role, propertyRef ); return typeFactory().list( role, propertyRef );
} }
case ARRAY: { case ARRAY: {
// TODO: Move into a util?
final JavaTypeDescriptor descriptor = pluralAttributeSource.getElementTypeDescriptor();
final Class clazz;
if (PrimitiveTypeDescriptor.class.isInstance( descriptor )) {
clazz = ( (PrimitiveTypeDescriptor) descriptor ).getClassType();
}
else {
clazz = classLoaderService.classForName( descriptor.getName().toString() );
}
return typeFactory().array( return typeFactory().array(
role, role,
propertyRef, propertyRef,
classLoaderService.classForName( clazz
pluralAttributeSource.getElementTypeDescriptor().getName().toString()
)
); );
} }
case MAP: { case MAP: {

View File

@ -219,16 +219,23 @@ public class Primitives {
} }
private static class PrimitiveDescriptorImpl implements PrimitiveTypeDescriptor { private static class PrimitiveDescriptorImpl implements PrimitiveTypeDescriptor {
private final Class clazz;
private final DotName name; private final DotName name;
private final int modifiers; private final int modifiers;
private final PrimitiveGroup group; private final PrimitiveGroup group;
protected PrimitiveDescriptorImpl(Class clazz, PrimitiveGroup group) { protected PrimitiveDescriptorImpl(Class clazz, PrimitiveGroup group) {
this.clazz = clazz;
this.name = DotName.createSimple( clazz.getName() ); this.name = DotName.createSimple( clazz.getName() );
this.modifiers = clazz.getModifiers(); this.modifiers = clazz.getModifiers();
this.group = group; this.group = group;
} }
@Override
public Class getClassType() {
return clazz;
}
@Override @Override
public DotName getName() { public DotName getName() {
return name; return name;

View File

@ -35,4 +35,12 @@ public interface PrimitiveTypeDescriptor extends JavaTypeDescriptor {
* @return The descriptor for the wrapper "equivalent". * @return The descriptor for the wrapper "equivalent".
*/ */
public PrimitiveWrapperTypeDescriptor getWrapperTypeDescriptor(); public PrimitiveWrapperTypeDescriptor getWrapperTypeDescriptor();
/**
* Since primitives are ClassLoader safe, allow the Class itself to be available. Needed for ArrayType
* handling, etc.
*
* @return The primitive's Class
*/
public Class getClassType();
} }

View File

@ -72,6 +72,8 @@ import org.hibernate.jdbc.Expectation;
import org.hibernate.jdbc.Expectations; import org.hibernate.jdbc.Expectations;
import org.hibernate.loader.collection.CollectionInitializer; import org.hibernate.loader.collection.CollectionInitializer;
import org.hibernate.metadata.CollectionMetadata; import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.metamodel.reflite.spi.JavaTypeDescriptor;
import org.hibernate.metamodel.reflite.spi.PrimitiveTypeDescriptor;
import org.hibernate.metamodel.spi.MetadataImplementor; import org.hibernate.metamodel.spi.MetadataImplementor;
import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding; import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.Cascadeable; import org.hibernate.metamodel.spi.binding.Cascadeable;
@ -560,10 +562,15 @@ public abstract class AbstractCollectionPersister
isInverse = keyBinding.isInverse(); isInverse = keyBinding.isInverse();
if ( isArray ) { if ( isArray ) {
// TODO: Move into a util?
final JavaTypeDescriptor descriptor = collection.getAttribute().getElementType().getDescriptor();
if (PrimitiveTypeDescriptor.class.isInstance( descriptor )) {
elementClass = ( (PrimitiveTypeDescriptor) descriptor ).getClassType();
}
else {
final ClassLoaderService cls = factory.getServiceRegistry().getService( ClassLoaderService.class ); final ClassLoaderService cls = factory.getServiceRegistry().getService( ClassLoaderService.class );
elementClass = cls.classForName( elementClass = cls.classForName( descriptor.getName().toString() );
collection.getAttribute().getElementType().getDescriptor().getName().toString() }
);
} }
else { else {
// for non-arrays, we don't need to know the element class // for non-arrays, we don't need to know the element class

View File

@ -23,6 +23,10 @@
*/ */
package org.hibernate.test.annotations.collectionelement; package org.hibernate.test.annotations.collectionelement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -34,23 +38,16 @@ import org.hibernate.Transaction;
import org.hibernate.metamodel.spi.binding.EntityBinding; import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding; import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.metamodel.spi.relational.TableSpecification; import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.annotations.Country; import org.hibernate.test.annotations.Country;
import org.hibernate.test.util.SchemaUtil; import org.hibernate.test.util.SchemaUtil;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@FailureExpectedWithNewMetamodel( jiraKey = "HHH-8962" )
public class CollectionElementTest extends BaseCoreFunctionalTestCase { public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testSimpleElement() throws Exception { public void testSimpleElement() throws Exception {