Object obj = new ArrayList<String>();
For some reason, I also have a declared type for the object:
Type type = new TypeToken<List<String>>(){}.getType();
Now, when reflecting on the object's fields and methods, I want to get
the best possible type information:
Method method = obj.getClass().getMethod("get", int.class);
Type returnType = GenericTypeReflector.getExactReturnType(method,
type);
This seems like a very natural use case to me, but gentyref doesn't
support this out of the box. The code only works if "method" is looked
up on List.class, but not if it's looked up on the object's type
(ArrayList). Is there a reason for this? Could gentyref be extended to
make this use case work?
Cheers,
Peter
Say I have a java.lang.Class representing ArrayList, and a
java.lang.reflect.ParameterizedType representing List<String>. Is
there a method in gentyref that, given these two values, will return a
ParameterizedType representing ArrayList<String>? Suppose I'm looking
for: ParameterizedType getExactSubtype(ParameterizedType type, Class<?
> subType)
Cheers,
Peter
> > Object obj = new ArrayList<String>();
> > Type type = new TypeToken<List<String>>(){}.getType();
> > Method method = obj.getClass().getMethod("get", int.class);
> > Type returnType = GenericTypeReflector.getExactReturnType(method,
> > type);
So you actually want the "get(int)" method from <type>, not from
obj.getClass(). But there is no direct way of getting a method from a Type.
So let's try to implement getMethod so that you can do:
Method method = getMethod(type, "get", int.class);
A first attempt, which works for your example, is just to take the erasure.
That'll always work if <type> is a Class or ParameterizedType like in your
example.
Method getMethod(Type type, String name, Class<?> parameterTypes) throws...{
return GenericTypeReflector.erase(type).getMethod(name, parameterTypes);
}
Something that'll work with more types is to look in all superclasses and
interfaces, and gentyref (at least the trunk) does have a method for that:
Method getMethod(Type type, String name, Class<?> parameterTypes) throws...{
for (Class<?> clazz :
GenericTypeReflector.getUpperBoundClassAndInterfaces(type)) {
try {
return clazz.getMethod("get", int.class);
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
}
}
throw ...;
}
On Wednesday 06 January 2010 01:40:35 Peter Niederwieser wrote:
> Let me put it differently:
>
> Say I have a java.lang.Class representing ArrayList, and a
> java.lang.reflect.ParameterizedType representing List<String>. Is
> there a method in gentyref that, given these two values, will return a
> ParameterizedType representing ArrayList<String>? Suppose I'm looking
> for ParameterizedType getExactSubtype(ParameterizedType type, Class<?>
> subType)
That's something different, and more difficult. Is the above solution enough,
or do you really want this?
Regards,
Wouter.
> That's something different, and more difficult. Is the above solution enough,
> or do you really want this?
Not sure why that's something different. What I want is to compensate
for erasure in cases where I know an object's static type. In pseudo-
code: unerase(dynamicType: ArrayList, staticType: List<String>) ==
ArrayList<String>.