Reputation: 51804
Given the following code snipppet:
public class ContentProvider {
public static List<Class<?>> getProducts() {
return getContent(42, Product.class);
}
private static List<Class<?>> getContent(int id, Class<?> contentType) {
// Generic content retrieval.
}
}
How can cast the return value of getContent
so that getProducts()
returns List<Product>
?
Upvotes: 1
Views: 865
Reputation: 11308
You want to use a generic method for this:
public class ContentProvider {
public static List<Product> getProducts() {
return getContent(42, Product.class);
}
private static <T> List<T> getContent(int id, Class<T> contentType) {
// Generic content retrieval.
}
}
Upvotes: 1