开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜