I hava a class following:
MyClass implements Serializable, Iterable<MyAnotherClass> {
snip
}
I made following codes to get "MyAnotherClass" class from an instance of MyClass.
These work well but I feel it's not cool because I'm using "instanceof" operator even getting "Type" of interfaces.
Does anyone have better idea without using instanceof operator?
Object instance = (Object)new MyClass();
Class<?> iteratorParamClass = null;
Type[] interfaceTypes = instance.getClass().getGenericInterfaces();
for (Type type : interfaceTypes) {
if (type instanceof ParameterizedType) { // ★This is not cool.
Type rawType = ((ParameterizedType) type).getRawType();
if (Iterable.class == (Class<?>) rawType) {
iteratorParamClass = (Class<?>) (((ParameterizedType) type).getActualTypeArguments())[0];
}
}
}
I am happy if the Type interface had a method like "Class<?> getConcreteClass()".