[jboss-dev] generic collection item type

Alexey Loubyansky alexey.loubyansky at redhat.com
Fri Aug 3 03:46:02 EDT 2007


The problem is when clazz argument is an interface and we call 
clazz.getSuperclass() which is null and which is passed as clazz 
argument to the next call.
So, either that or we shouldn't call getSuperclass() on an interface.

You could comment out the check for 
reference.isAssignableFrom(interfaceClass) and try this class

    public abstract class CollectionClass<T1 extends Object, T2 extends 
Object, T3 extends Object>
       implements Comparable<T1>, Collection<T2>, java.util.Comparator<T3>
    {
    }

Alexey

Adrian wrote:
> I've applied your patch, but I don't understand why the NPE happens?
> Why wouldn't a class implement its interface?
> 
> Basically I want to write a test for what you were seeing.
> 
> ClassInfo info = ...;
> info.getComponentType(); // Should not throw NPE! :-)
> 
> On Wed, 2007-08-01 at 11:03 +0200, Alexey Loubyansky wrote:
>> 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;
> 



More information about the jboss-development mailing list