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.JavaTypeDescriptor;
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.BasicPluralAttributeElementSource;
import org.hibernate.metamodel.source.spi.ComponentAttributeSource;
@ -522,12 +523,19 @@ class HibernateTypeHelper {
return typeFactory().list( role, propertyRef );
}
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(
role,
propertyRef,
classLoaderService.classForName(
pluralAttributeSource.getElementTypeDescriptor().getName().toString()
)
clazz
);
}
case MAP: {

View File

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

View File

@ -35,4 +35,12 @@ public interface PrimitiveTypeDescriptor extends JavaTypeDescriptor {
* @return The descriptor for the wrapper "equivalent".
*/
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.loader.collection.CollectionInitializer;
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.binding.AbstractPluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.Cascadeable;
@ -560,10 +562,15 @@ public abstract class AbstractCollectionPersister
isInverse = keyBinding.isInverse();
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 );
elementClass = cls.classForName(
collection.getAttribute().getElementType().getDescriptor().getName().toString()
);
elementClass = cls.classForName( descriptor.getName().toString() );
}
}
else {
// for non-arrays, we don't need to know the element class

View File

@ -23,6 +23,10 @@
*/
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.List;
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.PluralAttributeBinding;
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.util.SchemaUtil;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
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 Hardy Ferentschik
*/
@SuppressWarnings("unchecked")
@FailureExpectedWithNewMetamodel( jiraKey = "HHH-8962" )
public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test
public void testSimpleElement() throws Exception {