Search code examples
javagenericstype-conversionparameterized

Parameterizing a parameterized method's return type


I have been trying to get something like this working:

private static <V, K> V<KnownType> from(K oldObject, Class<? extends V<KnownType>> newClass) {
    ...
}

However, this method signature gets a compiler error:

The type V is not generic; it cannot be parameterized with arguments <KnownType>

Basically, I want a method that converts K to V<KnownType>. Is this possible?

Also, some additional context in case somebody writes code for this -- all V's have a single-argument constructor that take in a K.

Thanks in advance.

Edit -- The input class is a subclass of V. (? extends V...)


Solution

  • Assuming:

    public interface SomeType<E>
    

    Try this:

    private static <V extends SomeType<KnownType>, K> V from(K oldObject, Class<V> newClass) {