[jboss-dev] generic collection item type
Alexey Loubyansky
alexey.loubyansky at redhat.com
Wed Aug 1 05:03:58 EDT 2007
Yes, it does work! Thanks!
I had to add a check that the interface implements the reference before
calling locateActualType because in case of Comparable<T> and so on I
would get NPE later. I checked out the trunk but it didn't build. So,
here is the diff.
Type[] interfaces = clazz.getGenericInterfaces();
for (Type intf : interfaces)
- {
- Type result;
+ {
+ Class interfaceClass;
if (intf instanceof Class)
{
- Class interfaceClass = (Class) intf;
- result = locateActualType(reference, parameter,
interfaceClass, intf);
- if (result instanceof TypeVariable)
- result = getParameter(clazz, type, (TypeVariable) result);
+ interfaceClass = (Class) intf;
}
else if (intf instanceof ParameterizedType)
{
ParameterizedType interfaceType = (ParameterizedType) intf;
- Class interfaceClass = (Class) interfaceType.getRawType();
+ interfaceClass = (Class) interfaceType.getRawType();
+ }
+ else
+ throw new IllegalStateException("Unexpected type " +
intf.getClass());
+
+ Type result = null;
+ if (reference.isAssignableFrom(interfaceClass))
+ {
result = locateActualType(reference, parameter,
interfaceClass, intf);
if (result instanceof TypeVariable)
result = getParameter(clazz, type, (TypeVariable) result);
}
- else
- throw new IllegalStateException("Unexpected type " +
intf.getClass());
+
if (result != null)
return result;
Adrian wrote:
> On Mon, 2007-07-30 at 14:39 +0200, Alexey Loubyansky wrote:
>> Adrian wrote:
>>> Perhaps we can add that to the ClassInfo model?
>> That would be excellent.
>>
>>> if (classInfo.isCollection())
>>> typeInfo = classInfo.getComponentType();
>>>
>>> and it would do all the hard work of locating the collection
>>> definition and retrieving the type.
>>>
>>> We could also do something similar for maps, i.e.
>>>
>>> if (classInfo.isMap())
>>> {
>>> keyType = classInfo.getKeyType();
>>> valueType = classInfo.getValueType();
>>> }
>> This one too!
>>
>
> I've implemented this. It was actually easier
> than I thought once I got my head around it. :-)
>
> See the bottom of IntrospectionTypeInfoFactoryImpl
>
> The basic idea is to locate the Collection interface
> in the type hierarchy
> then work your way back mapping TypeVariables
> until you reach where the real type is specified.
>
> Or failing that use the upper bounds of the
> last TypeVariable found.
>
>> Thanks,
>> Alexey
>> _______________________________________________
>> jboss-development mailing list
>> jboss-development at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/jboss-development
More information about the jboss-development
mailing list