My goal is to develop a class that can output an object of a specified class.

public class GetMe<T> {
    public T get() {
        Object obj = generateObject();
        return (T) obj;
    }
}

Now, I know this isn't possible due to erasure. So, we can pass in a class instance and use that to cast.

public class GetMe<T> {
    public GetMe<T>(Class<T> clazz) {
        this.clazz = clazz;
    }

    public T get() {
        Object obj = generateObject();
        return clazz.cast(obj);
    }
}

This works great! As long as the class isn't parameterized. If it is, then I've got a problem.

I'm not allowed to use List<String>.class. If I pass in a ParameterizedType (which in itself is difficult to generate), there's no cast method to use.

Is there a way out of this quagmire?

Edit
Report