From 234231851430cb37f37c33cb1f693b4c803d78d5 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Tue, 7 Feb 2012 17:13:34 -0600 Subject: [PATCH] HHH-6561 plural attribute java type detecition, not perfect, but works --- .../metamodel/internal/source/Binder.java | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java index 300ecdfa8b..43e2a310ae 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/Binder.java @@ -1082,7 +1082,6 @@ public class Binder { private class PluralAttributeJavaTypeDeterminerDelegate implements BeanInfoHelper.BeanInfoDelegate { private final Class ownerClass; private final String attributeName; - private Class javaType = null; private PluralAttributeJavaTypeDeterminerDelegate(Class ownerClass, String attributeName) { @@ -1092,48 +1091,43 @@ public class Binder { @Override public void processBeanInfo(BeanInfo beanInfo) throws Exception { - for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ) { - if ( propertyDescriptor.getName().equals( attributeName ) ) { - javaType = extractCollectionComponentType( beanInfo, propertyDescriptor ); - break; - } - } - } - - @SuppressWarnings( { "unchecked" }) - private Class extractCollectionComponentType(BeanInfo beanInfo, PropertyDescriptor propertyDescriptor) { - final java.lang.reflect.Type collectionAttributeType; - if ( propertyDescriptor.getReadMethod() != null ) { - collectionAttributeType = propertyDescriptor.getReadMethod().getGenericReturnType(); - } - else if ( propertyDescriptor.getWriteMethod() != null ) { - collectionAttributeType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; + java.lang.reflect.Type collectionAttributeType = null; + if ( beanInfo.getPropertyDescriptors() == null || beanInfo.getPropertyDescriptors().length == 0 ) { + // we need to look for the field and look at it... + collectionAttributeType = ownerClass.getField( attributeName ).getGenericType(); } else { - // we need to look for the field and look at it... - try { - collectionAttributeType = ownerClass.getField( propertyDescriptor.getName() ).getGenericType(); - } - catch ( Exception e ) { - return null; + for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ) { + if ( propertyDescriptor.getName().equals( attributeName ) ) { + if ( propertyDescriptor.getReadMethod() != null ) { + collectionAttributeType = propertyDescriptor.getReadMethod().getGenericReturnType(); + } + else if ( propertyDescriptor.getWriteMethod() != null ) { + collectionAttributeType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; + } + } } } - - if ( ParameterizedType.class.isInstance( collectionAttributeType ) ) { - final java.lang.reflect.Type[] types = ( (ParameterizedType) collectionAttributeType ).getActualTypeArguments(); - if ( types == null ) { - return null; + if ( collectionAttributeType != null ) { + if ( ParameterizedType.class.isInstance( collectionAttributeType ) ) { + final java.lang.reflect.Type[] types = ( (ParameterizedType) collectionAttributeType ).getActualTypeArguments(); + if ( types == null ) { + return; + } + else if ( types.length == 1 ) { + javaType = (Class) types[0]; + } + else if ( types.length == 2 ) { + // Map + javaType = (Class) types[1]; + } } - else if ( types.length == 1 ) { - return (Class) types[0]; - } - else if ( types.length == 2 ) { - // Map - return (Class) types[1]; + else { + javaType = (Class) collectionAttributeType; } } - return null; } + } }