HHH-6561 plural attribute java type detecition, not perfect, but works

This commit is contained in:
Strong Liu 2012-02-07 17:13:34 -06:00
parent d73420424e
commit 2342318514
1 changed files with 29 additions and 35 deletions

View File

@ -1082,7 +1082,6 @@ public class Binder {
private class PluralAttributeJavaTypeDeterminerDelegate implements BeanInfoHelper.BeanInfoDelegate { private class PluralAttributeJavaTypeDeterminerDelegate implements BeanInfoHelper.BeanInfoDelegate {
private final Class<?> ownerClass; private final Class<?> ownerClass;
private final String attributeName; private final String attributeName;
private Class<?> javaType = null; private Class<?> javaType = null;
private PluralAttributeJavaTypeDeterminerDelegate(Class<?> ownerClass, String attributeName) { private PluralAttributeJavaTypeDeterminerDelegate(Class<?> ownerClass, String attributeName) {
@ -1092,48 +1091,43 @@ public class Binder {
@Override @Override
public void processBeanInfo(BeanInfo beanInfo) throws Exception { public void processBeanInfo(BeanInfo beanInfo) throws Exception {
for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ) { java.lang.reflect.Type collectionAttributeType = null;
if ( propertyDescriptor.getName().equals( attributeName ) ) { if ( beanInfo.getPropertyDescriptors() == null || beanInfo.getPropertyDescriptors().length == 0 ) {
javaType = extractCollectionComponentType( beanInfo, propertyDescriptor ); // we need to look for the field and look at it...
break; collectionAttributeType = ownerClass.getField( attributeName ).getGenericType();
}
}
}
@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];
} }
else { else {
// we need to look for the field and look at it... for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() ) {
try { if ( propertyDescriptor.getName().equals( attributeName ) ) {
collectionAttributeType = ownerClass.getField( propertyDescriptor.getName() ).getGenericType(); if ( propertyDescriptor.getReadMethod() != null ) {
} collectionAttributeType = propertyDescriptor.getReadMethod().getGenericReturnType();
catch ( Exception e ) { }
return null; else if ( propertyDescriptor.getWriteMethod() != null ) {
collectionAttributeType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];
}
}
} }
} }
if ( collectionAttributeType != null ) {
if ( ParameterizedType.class.isInstance( collectionAttributeType ) ) { if ( ParameterizedType.class.isInstance( collectionAttributeType ) ) {
final java.lang.reflect.Type[] types = ( (ParameterizedType) collectionAttributeType ).getActualTypeArguments(); final java.lang.reflect.Type[] types = ( (ParameterizedType) collectionAttributeType ).getActualTypeArguments();
if ( types == null ) { if ( types == null ) {
return null; return;
}
else if ( types.length == 1 ) {
javaType = (Class<?>) types[0];
}
else if ( types.length == 2 ) {
// Map<K,V>
javaType = (Class<?>) types[1];
}
} }
else if ( types.length == 1 ) { else {
return (Class<?>) types[0]; javaType = (Class<?>) collectionAttributeType;
}
else if ( types.length == 2 ) {
// Map<K,V>
return (Class<?>) types[1];
} }
} }
return null;
} }
} }
} }