开发者

How do I declare the generic type of an instance dynamically in java

开发者 https://www.devze.com 2022-12-20 02:22 出处:网络
I am mocking an interface that doesn\'t use generics, but does take a Class type as an argument. public Object query(Class c, Filter f)

I am mocking an interface that doesn't use generics, but does take a Class type as an argument.

public Object query(Class c, Filter f)
{....}

Is there a way in my implementation to use c as the argument for a generic?

eg.

return new ArrayList<c>();

Obviously I could do a switch if I had a know set of values for c, but开发者_StackOverflow社区 that is a very ugly hack that I don't want to do.

Thanks.


You need a helper method:

 private <T> List<T> createList(Class<T> klass) {
          return new ArrayList<T>();
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消