Reputation: 1200
I had custom converter for dozer framework
constom converter called for generic type list source and generic type list destination
public class ListCommonCustomConverter implements ConfigurableCustomConverter{
public Object convert(Object destination, Object source, Class<?> destinationClass, Class<?> sourceClass) {
log.info("Inside CommonCustomConverter :: convert");
// Need to fine generic type of list
return null;
}
}
Eg: if List<Person>
passed as destination for converter i need to get Person class from destination object inside converter method.
Thank in Advance.
Upvotes: 3
Views: 1847
Reputation: 16476
You could get type info from a generic type by calling getClass().getGenericInterfaces()
if your class was declared like:
public class ListCommonCustomConverter implements ConfigurableCustomConverter<Person>
But since this converter is not parameterized, you could do a trick:
public interface CustomConverterWithTypeInfo<T> extends ConfigurableCustomConverter{
}
public class ListCommonCustomConverter implements CustomConverterWithTypeInfo<Person>{
public Object convert(Object destination, Object source, Class<?> destinationClass, Class<?> sourceClass) {
Class<?> myClass = (Class)((ParameterizedType)getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0];
//...
}
}
Or you could simply do:
List<?> list = (List)destination;
if(!list.isEmpty()){
Class<?> myClass = list.get(0).getClass();
}
Upvotes: 1