I have a Java method with the following signature:
public <T> List<HtmlOptionsComposite> convertToHtmlOptionsCompositeList (List<? extends T> objects, Class<T> clazz, String getValueMethodName, String getDescriptionMethodName, String getDiscontinuedMethodName) { ... }
The clazz parameter is required by the method to use reflection (clazz.getMethod(...)) on each of the given objects, but do I really need to pass it in as a parameter to the method? Is there any way I can refactor the code to work out what clazz should be automatically?
You could call
getClass()on the first object in the list. That would give you aClass<? extends T>rather thanClass<T>though – and in particular that means may will be some methods which are given which shouldn’t be (because they’re not methods onTitself).