Suppose I have an interface like this;
public interface Validator<T> {
Class<T> type();
}
If I want to implement this interface with an object, there is not any problem;
public OrderValidator implements Validator<Order>{
Class<Order> type(){
return Order.class;
}
}
But I can't implement this interface if I pass a Collection as generic type like this;
public CollectionValidator implements Validator<Collection<Item>>{
Class<Collection<Item>> type(){
//how can I implement this method to return type of Collection<Item> ?
}
}
How can I implement type()
method to return type of Collection< Item>
?
Collection< Item>.class
does not work.
If you just want to make it compile then try:
public interface Validator<T> {
Class<T> type();
}
public static class CollectionValidator<Item> implements Validator<Collection<Item>>{
@SuppressWarnings("unchecked")
public Class<Collection<Item>> type() {
return (Class<Collection<Item>>) (Class<?>) Collection.class;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments