I asked a question about the 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. Perhaps updating the original question is not suitable for this time, so I am asking another question.
Let's say I want to make one reflected method to call those marshal methods that are defined in Marshaller , for example
void marshal(Object element, ContentHandler handler) void marshal(Object element, File output) void marshal(Object element, Node node)
etc.
One of the methods I'm working on is
void marshal(Object jaxbElement, Class<?> targetType, Object target)
Implementation is simple
- Find method looks like
marshal(Ljava/lang/Object;Ljava/lang/Class;)V using Object.class and targetType - call the method with
element and target .
Thus, any unit testing code can be referenced as
marshal(element, ContentHandler.class, handler); marshal(element, File.class, new File("text.xml"));
In this case, how to define the marshal method? Is there a difference between
<T> marshal(Object element, Class<T> targetType, T target);
and
<T, S extends T> marshal(Object element, Class<T> targetType, S target)
?
Additional comments for answers
I think I need targetType for a quick and direct method that is looking for the correct method.
Without targetType I need to repeat 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 would be better, I think. :)