Search code examples
javamethodhandle

Using MethodHandles to call a method on of class that isn't known at compile time (without reflection)


I need to call the getName() method of a Person class, without knowing the Person class as compilation time and by using a MethodHandle (instead of normal reflection).

So I'd want this code to work (this code cannot change):

MyGetterAccessor myGA = new MyGetterAccessor(Person.class, "getName", String.class)
assertEquals("Ann", myGA.call(new Person("Ann"));
assertEquals("Beth", myGA.call(new Person("Beth"));

Here's my method handle code, which must not use the word "Person" (this can can change to make this work):

public class MyGetterAccessor {

    MethodHandle mh;

    public GetterAccessor(Class entityClass, String methodName, Class returnType) {
        mh = MethodHandles.lookup().findVirtual(entityClass, methodName, MethodType.methodType(returnType));
    }

    public Object call(Object entity) {
        return mh.invokeExact(entity);
    }

}

but this fails with WrongMethodTypeException. Any suggestion how to fix that?


Solution

  • FWIW, this works, but it's slow in java 8 in my benchmarks.

    MethodHandle temp = lookup.findVirtual(getterMethod.getDeclaringClass(), getterMethod.getName(), MethodType.methodType(returnType));
    temp = temp.asType(temp.type().changeParameterType(0 , Object.class));
    getterMethodHandle = temp.asType(temp.type().changeReturnType(Object.class));