我怎样才能做到这一点?
public class GenericClass<T>
{
public Type getMyType()
{
//How do I return the type of T?
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试的所有东西总是返回类型Object而不是使用的特定类型.
这是我的代码:ExecutorImp扩展了AbstractExecutor,它提取了与其实现者相同的执行逻辑(ExecutorImp就是一例),当调用ExecutorImp的execute()方法时,它将调用其超类型中的方法,但是超类型(AbstractExcutor) )应该知道另一个绑定到实现者的类(在示例中,它是User类):
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
abstract class AbstractExecutor<E> {
public void execute() throws Exception {
ArrayList<E> list = new ArrayList<E>();
// here I want to get the real type of 'E'
Class cl = this.getClass().getTypeParameters()[0].getGenericDeclaration().getClass();
Object o = cl.getConstructor(String.class).newInstance("Gate");
list.add((E) o);
System.out.println(format(list));
}
public abstract String format(ArrayList<E> list);
public abstract String getType();
}
public class ExectorImp<E> extends AbstractExecutor<User> {
@Override
public String getType() {
return "user";
}
@Override
public String format(ArrayList<User> list) {
StringBuffer sb = new StringBuffer();
for …Run Code Online (Sandbox Code Playgroud) 还有其他相关问题,例如6624113,3403909,4516891但我的问题是更简单,更具体.
我想在运行时知道我的类参数化了什么类型 - 我想要一个类型参数类型的Class对象.由于类型擦除,表达式T.class不起作用,并且没有像typeof(T)C#中那样的函数来获取它.
但是,通过ParameterizedType和相关类可以获得一些"超级反射" ,这几乎可以解决所有问题.
import java.lang.reflect.ParameterizedType;
public class MyClass<T> {
public static void main( String[] args ) {
new MyClass<Integer>().printTypeParam();
}
public void printTypeParam() {
class DummyT extends MyClass<T> {}
class DummyString extends MyClass<String> {}
ParameterizedType ptT =
((ParameterizedType) DummyT.class.getGenericSuperclass() );
ParameterizedType ptString =
((ParameterizedType) DummyString.class.getGenericSuperclass() );
System.out.println( "DummyT: " + ptT + " " + ptT.getActualTypeArguments()[0] );
System.out.println( "DummyString: " + ptString + " " …Run Code Online (Sandbox Code Playgroud) 我为一些Spring MVC控制器编写了JUnit测试.JUnit测试的初始化对于我的所有Controllers测试都是通用的,所以我想创建一个执行此初始化的抽象类.
因此,我创建了以下代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring/applicationContext-test.xml", "classpath*:spring/spring-mvc-test.xml" })
@Transactional
public abstract class AbstractSpringMVCControllerTest<T> {
@Autowired
protected ApplicationContext applicationContext;
protected MockHttpServletRequest request;
protected MockHttpServletResponse response;
protected HandlerAdapter handlerAdapter;
protected T controller;
@SuppressWarnings("unchecked")
@Before
public void initContext() throws SecurityException, NoSuchFieldException {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = applicationContext.getBean(AnnotationMethodHandlerAdapter.class);
// Does not work, the problem is here...
controller = applicationContext.getBean(T);
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是创建,为每个控制器我想测试一个扩展我的JUnit测试类AbstractSpringMVCControllerTest.extends声明中给出的类型是Controller的类.
例如,如果我想测试我的AccountController,我会创建这样的AccountControllerTest类:
public class AccountControllerTest …Run Code Online (Sandbox Code Playgroud)