Here’s a Java generic pattern:
public <T> T getResultData(Class<T> resultClass, other_args) {
...
return resultClass.cast(T-thing);
}
A typical call looks like:
DoubleBuffer buffer;
buffer = thing.getResultData(DoubleBuffer.class, args);
I’ve never been able to figure out how to use this pattern cleanly when the desired return type is, itself, generic. To be ‘concrete’, what if a function like this wants to return Map<String,String>? Since you can’t get a class object for a generic, of course, the only option would be to pass Map.class, and then you need a cast and an @SuppressWarning after all.
One ends up with a call like:
Map<String, String> returnedMap;
returnedMap = thing.getResultData(Map.class, some_other_args);
Now one is back to needing casts and suppressing a warning.
I suppose that one could take something from the java.lang.reflect.Type family instead of the Class, but those aren’t especially easy to concoct. That looks like:
class Dummy {
Map<String, String> field;
}
...
Type typeObject = Dummy.class.getField("field").getGenericType();
Given this, the called function could extract the base type and use that for casting or newInstance-ing, but the dummy field business sure looks ugly.
Note that functions like this are not always calling newInstance. Obviously, if they do, they don’t need to call resultClass.cast.
You cannot do that in the standard Java API. However, there are utility classes available which can get the
Typeout of a generic class. In for example Google Gson (which converts JSON to fullworthy Javabeans and vice versa) there’s aTypeTokenclass. You can use it as follows:Such a construct is exactly what you need. You can find here the source sode, you may find it useful as well. It only requires an additional
getResultData()method which can accept aType.