[英]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. 我在模拟一个不使用泛型的接口,但确实将Class
类型作为参数。
public Object query(Class c, Filter f)
{....}
Is there a way in my implementation to use c
as the argument for a generic? 我的实现中是否可以使用c
作为泛型的参数?
eg. 例如。
return new ArrayList<c>();
Obviously I could do a switch
if I had a know set of values for c
, but that is a very ugly hack that I don't want to do. 显然,如果我知道c
一组值,就可以进行switch
,但这是我不想做的非常丑陋的修改。
Thanks. 谢谢。
You need a helper method: 您需要一个辅助方法:
private <T> List<T> createList(Class<T> klass) {
return new ArrayList<T>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.