LambdaMetafactory的Java访问bean方法 [英] Java access bean methods with LambdaMetafactory

查看:106
本文介绍了LambdaMetafactory的Java访问bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与该主题中的明确使用LambdaMetafactory
密切相关,提供了一些非常好的例子来使用LambdaMetafactory来访问类的静态方法;但是,我想知道访问现有bean实例的非静态字段的等效代码是什么。似乎很难找到一个例子,我执行的每一次尝试都以非工作代码结束。

my question is strongly related to Explicit use of LambdaMetafactory in that thread, some very good examples are provided to use the LambdaMetafactory to access a static method of a class; however, I wonder what is the equivalent code to access a non static field of an existing bean instance. It seems really hard to find an example and every attempt I performed ended up in non working code.

这是bean代码:

class SimpleBean {
    private Object obj= "myCustomObject";
    private static Object STATIC_OBJECT = "myCustomStaticObject";
    public Object getObj() {
        return obj;
    }
    public void setObj(final Object obj) {
        this.obj = obj;
    }
    public static Object getStaticObj() {
        return STATIC_OBJECT;
    }
    public static void setStaticObj(final Object obj) {
        STATIC_OBJECT = obj;
    }
}

这是一个成功访问静态方法的工作单元测试getStaticObj():

Here a working unit test that successfully access the static method "getStaticObj()":

    @Test
public void accessStaticMethod() throws Throwable
{
    MethodHandles.Lookup caller = MethodHandles.lookup();
    Method reflected = SimpleBean.class.getDeclaredMethod("getStaticObj");
    MethodHandle methodHandle = caller.unreflect(reflected);
    CallSite site = LambdaMetafactory.metafactory(caller,
            "get",
            MethodType.methodType(Supplier.class),
            MethodType.methodType(Object.class),
            methodHandle,
            MethodType.methodType(Object.class));
    MethodHandle factory = site.getTarget();
    Supplier r = (Supplier) factory.invoke();
    assertEquals( "myCustomStaticObject", r.get());
}

现在我尝试访问非静态getObj()方法失败了:

Now here my failing attempts to access the non static "getObj()" method:

    @Test
public void accessNonStaticMethodTestOne() throws Throwable
{
    SimpleBean simpleBeanInstance = new SimpleBean();

    MethodHandles.Lookup caller = MethodHandles.lookup();
    MethodHandle methodHandle = caller.bind(simpleBeanInstance, "getObj", MethodType.methodType(Object.class));
    assertEquals("myCustomObject", methodHandle.invoke());

    // This test fails here with exception:
    // java.lang.IllegalArgumentException: not a direct method handle
    CallSite site = LambdaMetafactory.metafactory(caller,
            "get",
            MethodType.methodType(Supplier.class),
            MethodType.methodType(Object.class),
            methodHandle,
            MethodType.methodType(Object.class));

    MethodHandle factory = site.getTarget();
    Supplier r = (Supplier) factory.invoke();
    assertEquals( "myCustomObject", r.get());

}

@Test
public void accessNonStaticMethodTwo() throws Throwable
{

    SimpleBean simpleBeanInstance = new SimpleBean();

    MethodHandles.Lookup caller = MethodHandles.lookup();

    Method reflected = SimpleBean.class.getDeclaredMethod("getObj");
    MethodHandle methodHandle = caller.unreflect(reflected);

    // This test fails here with exception:
    // java.lang.invoke.LambdaConversionException: Incorrect number of parameters
    CallSite site = LambdaMetafactory.metafactory(caller,
            "get",
            MethodType.methodType(Supplier.class),
            MethodType.methodType(Object.class),
            methodHandle,
            MethodType.methodType(Object.class));

    MethodHandle factory = site.getTarget();
    factory = factory.bindTo(simpleBeanInstance);
    Supplier r = (Supplier) factory.invoke();
    assertEquals( "myCustomObject", r.get());

}


@Test
public void accessNonStaticMethodThree() throws Throwable
{

    SimpleBean simpleBeanInstance = new SimpleBean();

    MethodHandles.Lookup caller = MethodHandles.lookup();

    Method reflected = SimpleBean.class.getDeclaredMethod("getObj");
    MethodHandle methodHandle = caller.unreflect(reflected);

    CallSite site = LambdaMetafactory.metafactory(caller,
            "get",
            MethodType.methodType(Supplier.class),
            MethodType.methodType(Object.class, SimpleBean.class),
            methodHandle,
            MethodType.methodType(Object.class, SimpleBean.class));

    MethodHandle factory = site.getTarget();

    //This test fails here with exception:
    // java.lang.IllegalArgumentException: no leading reference parameter: spike.LambdaBeanAccessAtRuntimeTest$SimpleBean@4459eb14
    factory = factory.bindTo(simpleBeanInstance);
    Supplier r = (Supplier) factory.invoke();
    assertEquals( "myCustomObject", r.get());

}

每次尝试都有不同的负面结果,我真的希望有人是帮助我让至少一个测试工作正常。

Every attempt has a different negative result, I really hope someone is abe to help me to have at least one test working fine.

推荐答案

如果你想将值绑定到你的lamba,你必须将这些参数包含在invokedtype签名中:

If you want to bind values to your lamba, you have to include these parameters to the invokedtype signature:

SimpleBean simpleBeanInstance = new SimpleBean();

MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType getter=MethodType.methodType(Object.class);
MethodHandle target=caller.findVirtual(SimpleBean.class, "getObj", getter);
CallSite site = LambdaMetafactory.metafactory(caller,
    "get", // include types of the values to bind:
    MethodType.methodType(Supplier.class, SimpleBean.class),
    getter, target, getter);

MethodHandle factory = site.getTarget();
factory = factory.bindTo(simpleBeanInstance);
Supplier r = (Supplier) factory.invoke();
assertEquals( "myCustomObject", r.get());






您可以使用<而不是绑定值code>函数以bean作为参数:


Instead of binding a value you may use a Function which takes the bean as argument:

SimpleBean simpleBeanInstance = new SimpleBean();

MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType getter=MethodType.methodType(Object.class);
MethodHandle target=caller.findVirtual(SimpleBean.class, "getObj", getter);
MethodType func=target.type();
CallSite site = LambdaMetafactory.metafactory(caller,
    "apply",
    MethodType.methodType(Function.class),
    func.generic(), target, func);

MethodHandle factory = site.getTarget();
Function r = (Function)factory.invoke();
assertEquals( "myCustomObject", r.apply(simpleBeanInstance));

这篇关于LambdaMetafactory的Java访问bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆