Question

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>?

Was it helpful?

Solution

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.
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top