mardi 9 mars 2021

Java Reflection Casting Method ReturnType

To give some background, I am creating a small dependency injector and running into problems casting method invocations back into their return types. A minimal example is:

public class MinimalExample {
    public static <T> void invokeMethod(Class<T> aClass) throws ReflectiveOperationException {
        Optional<Method> myOptMethod = resolveMethod(aClass);
        if (myOptMethod.isPresent()) {
            Method myMethod = myOptMethod.get();
            Object myInstance = myMethod.invoke(myMethod);
            doSomething(myMethod.getReturnType(), myMethod.getReturnType().cast(myInstance));
        }
    }

    private static <T> Optional<Method> resolveMethod(Class<T> aClass) {
        return Stream.of(aClass.getMethods())
                .filter(aMethod -> Modifier.isStatic(aMethod.getModifiers()))
                .filter(aMethod -> aMethod.getParameterCount() == 0)
                .findAny();
    }

    private static <U> void doSomething(Class<U> aClass, U anInstance) {
        // E.g. Map aClass to anInstance.
    }
}

The problem here, is that doSomething needs to be called with Class<U>, U, but it is currently being called with Class<capture of ?>, capture of ? due to the invoke method's wildcard return type.

I could change doSomething to doSomething(Class<?> aClass, Object anInstance) but then I lose the type safety and this is not necessarily the only place that method is called.

My question is: Why can the compiler not infer they have the same underlying type, U, given the explicit cast?





Aucun commentaire:

Enregistrer un commentaire