[jboss-dev] generic collection item type

Alexey Loubyansky alexey.loubyansky at redhat.com
Mon Jul 30 08:39:43 EDT 2007


Adrian wrote:
> On Mon, 2007-07-30 at 10:35 +0200, Alexey Loubyansky wrote:
>> This approach assumes that the component type can be determined with
>>
>> TypeInfo[] types = typeInfo.getActualTypeArguments();
>> if (types != null)
>>     elementType = types[0];
>>
>> This won't work for this class
>> https://svn.jboss.org/repos/common/jbossxb-builder/trunk/src/test/java/org/jboss/ejb/metadata/jboss/JBossEnterpriseBeansMetaData.java
>>
>> for which its super class should be checked for type arguments and the 
>> component type is the second argument.
>>
> 
> Well that's a more complicated use case. 
> I don't see why you can't make it work. 
> You've just got to locate where it the type hierarchy
> and what paremeter the collection is.

That's my problem. How can I find out using the API which parameter is 
passed to the Collection

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

Besides that I have to keep track of arguments from sub- to super 
classes. And Collection interface can be extended implemented on 
different levels with different type parameters (but those should belong 
to the same type hierarchy).
But the main problem I think is the first one above.

>> I guess I'll need to specify the component type with an annotation. But 
>> it would be nice to have something like Class.getComponentType() to work 
>> for generic collections too.
> 
> 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!

Thanks,
Alexey



More information about the jboss-development mailing list