简体   繁体   English

如何使用MethodHandles在实现Java 8接口的类上调用方法?

[英]How to use MethodHandles to call a method on the class that implements an interface in Java 8?

public interface Foo {
    int add(int x, int y);
}

public class Bar implements Foo {
    int add(int x, int y) {
        return x+y;
    }
}

Now Imagine I have Foo.class and Bar.class in /usr/local 现在想象一下我在/usr/localFoo.classBar.class

Below is the class that loads both Bar and Foo and want want to call the add method dynamically. 下面是同时加载Bar和Foo并希望动态调用add方法的类。

import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.URL;
import java.net.URLClassLoader;

public class Test {

    public static void main(String... args) throws Throwable {
        File classFile = new File("/usr/local");
        ClassLoader classLoader = new URLClassLoader(new URL[]{classFile.toURI().toURL()});
        Class fooClass = classLoader.loadClass("Foo");
        MethodHandle mh = MethodHandles
                            .lookup()
                            .findVirtual(fooClass, "add", MethodType.methodType(int.class, int.class, int.class));
        System.out.println((int)mh.invoke(2, 3));
    }
}

Below is the error I get 以下是我得到的错误

Exception in thread "main" java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(Foo,int,int)int to (int,int)int

Questions: 问题:

  1. How to use MethodHandles to invoke a method of a class that implements an interface? 如何使用MethodHandles调用实现接口的类的方法?
  2. Is there a way to avoid crazy casting in (int)mh.invoke(2, 3) ? 有没有办法避免在(int)mh.invoke(2, 3)疯狂转换?

You forgot to include one more argument - an instance, on which the method will be invoked: 您忘记了再添加一个参数-一个实例,在该实例上将调用该方法:

Class<?> barClass = classLoader.loadClass("Bar");
Object barInstance = barClass.getConstructor().newInstance();
System.out.println(mh.invoke(barInstance, 2, 3));

Also, the return type of the MethodHandle.invoke is Object , thus you can't avoid cast in this case. 另外, MethodHandle.invoke的返回类型为Object ,因此在这种情况下您无法避免MethodHandle.invoke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过接口从实现接口的类调用方法? - How to call a method from a class which implements an interface, through the interface? Java:如何调用在接口中实现另一个方法的方法? - Java: How do I call a method that implements another method in an interface? 仅在类实现后才如何使用接口的方法? - How to use an Interface’s method only if the class implements it? 从实现接口的类中调用方法(java) - calling a method from a class that implements an interface (java) 如何在该接口类型的对象上调用实现接口的类中的方法? - How can I call a method which is in a class which implements an Interface, on an object of that interface's type? 如何在实现接口的类中重写方法 - How to override method in class that implements interface 如何在实现接口的类中实现静态方法? - how to implement a static method in a class that implements an interface? 如果对象实现该接口,该如何调用接口方法? - How do I call an interface method if an object implements that interface? 使用Java泛型来接受和接口并返回实现该接口的类 - Use Java generics to accept and Interface and return a Class that implements that Interface 如何在实现接口但不扩展另一个类的Java类中引用super方法? - How can I reference a super method in a Java class that implements an interface but does not extend another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM