I asked a question about differences between
public static <T> void work(Class<T> type, T instance);
and
public static <T, S extends T> void work(Class<T> type, S instance);
I think I should explain what exactly I want to know. Updating the original question is not proper for this time, I think, so I'm asking an another question here.
Say, I want to make a single reflected method for invoking those marshal
methods defined in Marshaller such as
void marshal(Object element, ContentHandler handler)
void marshal(Object element, File output)
void marshal(Object element, Node node)
and so on.
One of method that I'm working on is
void marshal(Object jaxbElement, Class<?> targetType, Object target)
The implementation is simple
- Find a method looks like
marshal(Ljava/lang/Object;Ljava/lang/Class;)V
withObject.class
andtargetType
- invoke the method with
element
andtarget
.
So any unit testing code can invoke like
marshal(element, ContentHandler.class, handler);
marshal(element, File.class, new File("text.xml"));
In this case how should I define the marshal
method? Is there any difference between
<T> marshal(Object element, Class<T> targetType, T target);
and
<T, S extends T> marshal(Object element, Class<T> targetType, S target)
?
Further Comments per Answers
I think I need the targetType
for fast and direct method looking the right method.
Without the targetType
I have to iterate all methods like
for (Method method : Marshaller.class.getMethods()) {
// check modifiers, name, return type, and so on.
if (!method.getParameterTypes()[1].isAssignableFrom(target.getClass())) {
}
}
Adding another version for this will be better, I think. :)