[jboss-dev] generic collection item type
Adrian
abrock at redhat.com
Mon Jul 30 09:37:51 EDT 2007
On Mon, 2007-07-30 at 14:39 +0200, Alexey Loubyansky wrote:
> 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.
That's a very good question. If the class implements
Collection<Whatever> directly, you can get at this
information.
It's just:
for (ClassInfo intf : myClass.getInterfaces())
{
if (intf.getName().equals(Collection.class.getName())
return intf.getActualTypeArguments()[0];
}
But if it is on a superclass, there is no way to get
at this information.
In the main java reflection api:
Class.getGenericSuperClass() returns a ParameterizedType
but from that ParameterizedType there is no route
to get the generic interfaces.
e.g.
public class MyClass extends AbstractCollection<Whatever>
then there's no way to get at the ParameterizedType
Collection<Whatever> that AbstractCollection<Whataver> implements.
What would really be useful would be if there was a
Class.getAllGenericInterfaces();
but that doesn't exist.
--
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Adrian Brock
Chief Scientist
JBoss, a division of Red Hat
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
More information about the jboss-development
mailing list