拥有一个泛型静态函数,其中我只有泛型类型,是否可以获取其中一个泛型的类?
这是我的代码:
public static <A extends ResultError, B, A1 extends ResultError, B1> Function1<Either<A, B>,
Either<A1, B1>> transformConnectorErrorVoToCommandError(Event event) {
return new AbstractFunction1<Either<A, B>, Either<A1, B1>>() {
@Override
public Either<A1, B1> apply(Either<A, B> parameter) {
if (parameter.isRight()) {
return mapsRightSide(parameter);
} else {
return mapsLeftSide(parameter, event, ${I NEED THE CLASS OF A HERE});
}
}
Either<A1, B1> mapsRightSide(Either<A, B> parameter) {
return right(parameter.right().get());
}
Right right(Object result) {
return new Right<>(result);
}
};
}
private static <A extends ResultError, B, A1 extends ResultError, B1> Either<A1, B1> mapsLeftSide(Either<A, B> parameter,
Event event,
Class<A1> outputErrorClass) {
initLazyMapperComponent();
A sourceError = parameter.left().get();
A1 destinyError = mapper.map(sourceError, outputErrorClass);
if (destinyError != null) {
return left(destinyError);
}
ExceptionUtil.throwNotHandledError(event, sourceError);
return null;
}
我在代码中需要的部分是${I NEED THE CLASS OF A HERE}
我知道在非静态函数中这是可能的,但不知道是否可以在静态方式。
问候。
最佳答案
不,你无法获得 A
的类那里。它在运行时全部被删除。
典型的选项是获取 A
的类型来自 A
实例的值您可能在 parameter
中有无论如何。
或通过Class<A> classOfA
作为进一步的参数。
关于java - 从静态函数中的泛型获取类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46724577/