0

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?

4

2 回答 2

0

Maybe

public static <T,R> setValue(T setTo, BiFunction<T,String,R> setter, String value) {
    return setter.apply(setTo, value);
}

assertEquals("Ann", setValue(new Person(), Person::set, "Ann"));

Or is that not reflective enough?

于 2017-12-21T13:42:39.537 回答
0

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));
于 2018-01-10T11:12:01.263 回答