Предположим, что у меня есть три метода внутри данного типа (класс/интерфейс):
public void foo(Integer integer);
public void foo(Number number);
public void foo(Object object);
Используя MethodHandle
или отражение, я хотел бы найти наиболее специфический перегруженный метод для объекта, тип которого известен только во время выполнения. то есть я хотел бы сделать JLS 15.12 во время выполнения.
Например, предположим, что в методе упомянутого выше типа есть три следующих метода:
Object object = getLong(); // runtime type is Long *just an example*
MethodHandles.lookup()
.bind(this, "foo", methodType(Void.class, object.getClass()))
.invoke(object);
Тогда я концептуально хотел бы выбрать foo(Number number)
, но выше будет вызывать исключение, поскольку API будет искать только метод foo(Long)
и ничего больше. Обратите внимание, что использование Long
здесь просто как пример. Тип объекта может быть любым на практике; String, MyBar, Integer,... и т.д. И т.д.
Есть ли что-то в API MethodHandle, которое автоматически и во время выполнения делает то же разрешение, что и компилятор после JLS 15.12?