我有以下界面:
public interface Foo<T> { }
…以及它的实现:
@Component
public class FooImpl<T> implements Foo<T> { }
如何检索 Class<T>
从内部 FooImpl
?
重要提示: FooImpl
不能抽象( ParameterizedType::getActualTypeArguments
不起作用)。
我尝试过:
@Component
public class FooImpl<T> implements Foo<T> {
@PostConstruct
private void postConstruction() {
ResolvableType r1 = ResolvableType.forClass(getClass());
ResolvableType r2 = ResolvableType.forClass(getClass(), Foo.class);
ResolvableType r3 = ResolvableType.forClass(Foo.class, getClass());
ResolvableType r4 = ResolvableType.forClass(getClass(), FooImpl.class);
ResolvableType r5 = ResolvableType.forClass(FooImpl.class, getClass());
Class<T> c11 = (Class<T>) r1.getGeneric().resolve();
Class<T> c12 = (Class<T>) r1.getGeneric(0).resolve();
Class<T> c13 = (Class<T>) r1.getGeneric().toClass();
Class<T> c14 = (Class<T>) r1.getGeneric(0).toClass();
Class<T> c21 = (Class<T>) r2.getGeneric().resolve();
Class<T> c22 = (Class<T>) r2.getGeneric(0).resolve();
Class<T> c23 = (Class<T>) r2.getGeneric().toClass();
Class<T> c24 = (Class<T>) r2.getGeneric(0).toClass();
Class<T> c31 = (Class<T>) r3.getGeneric().resolve();
Class<T> c32 = (Class<T>) r3.getGeneric(0).resolve();
Class<T> c33 = (Class<T>) r3.getGeneric().toClass();
Class<T> c34 = (Class<T>) r3.getGeneric(0).toClass();
Class<T> c41 = (Class<T>) r4.getGeneric().resolve();
Class<T> c42 = (Class<T>) r4.getGeneric(0).resolve();
Class<T> c43 = (Class<T>) r4.getGeneric().toClass();
Class<T> c44 = (Class<T>) r4.getGeneric(0).toClass();
Class<T> c51 = (Class<T>) r5.getGeneric().resolve();
Class<T> c52 = (Class<T>) r5.getGeneric(0).resolve();
Class<T> c53 = (Class<T>) r5.getGeneric().toClass();
Class<T> c54 = (Class<T>) r5.getGeneric(0).toClass();
Class<T> c61 = (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(), MongoRepositoryExtension.class);
Class<T> c62 = (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(), MongoRepositoryExtensionImpl.class);
Class<T> c63 = (Class<T>) GenericTypeResolver.resolveTypeArgument(MongoRepositoryExtension.class, getClass());
Class<T> c64 = (Class<T>) GenericTypeResolver.resolveTypeArgument(MongoRepositoryExtensionImpl.class, getClass());
}
}
暂无答案!
目前还没有任何答案,快来回答吧!