JJD
JJD

Reputation: 51804

How to cast List<Class<?>> to concrete type?

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

Answers (1)

Marcelo
Marcelo

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

Related Questions