I have one method, say getHystrixClass(Class<A<B>> clazz)
. It requires input type of Class<A<B>>
to pass in. How to do that? I have tried a few methods, but all of them failed at compilation time which are shown below with their corresponding error messages.
Put in class A
getHystrixClass(A.class)
ERROR MESSAGE : The method getHystrixClass(Class<A<B>>)
in the type IHystrixProvider<A<B>>
is not applicable for the arguments (Class<A>)
Put in class B
getHystrixClass(B.class)
ERROR MESSAGE : The method getHystrixClass(Class<A<B>>)
in the type IHystrixProvider<A<B>>
is not applicable for the arguments (Class<B>)
Put in class A<B>
getHystrixClass(A<B>.class)
ERROR MESSAGE : A
cannot be resolved to a variable
Create a dummy object and get class form it.
A<B> dummy = new A<B>();
getHystrixClass(dummy.getClass())
ERROR MESSAGE : The method getHystrixClass(Class<A<B>>)
in the type IHystrixProvider<A<B>>
is not applicable for the arguments (Class<capture#1-of ? extends A>)
Any hint? Sorry for my bad English.
Edit:
Hi, @GhostCat
The interface is look something like below
// Define by my team lead
public interface IHystrixProvider<T extends HystrixCommand, Rq> {
T getHystrixClass(Class<T> hystrixCommand);
}
/**
* @param <R> the return type
*/
public abstract class HystrixCommand<R> extends ... {
...
}
Code of HystrixCommand.java is public repository which can get from GitHub
You can pass Class<A<B>>
using a (unchecked) cast - this is necessary because there is no Class
object that represents the generic type A<B>
, only one for the raw type A
.
getHystrixClass((Class<A<B>>) (Class) A.class)
This will perform casts in the following order:
Class<A> (type of A.class) -> Class -> Class<A<B>>
Note that you first need the cast to raw Class
in order to avoid an incompatible cast
warning. Both casts also have zero runtime overhead, because the raw type ( Class
) stays the same, only the type arguments change.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.