3

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.

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.