I’m looking for a Java library for converting values that supports generic types. That is, if I have Foo<T>, then the library should support converting a String to Foo<String> or Foo<Int>.
Of course given a class, one cannot tell its generic component. My aim is to populate a bean with string values. So the bean’s properties can be examined for the generic type information.
Here is a skeleton of a solution:
Note the source type is a
Class, but the target is aType. If it is aClass, then code can be delegated to one of the existing libraries. IftargetTypeis aParameterizedType, then you can get the type arguments (classes) from it, then the standard libraries can be used to convert to them.To use:
Type targetType = field.getGenericType()Type targetType = method.getGenericParameterTypes()[0]