如何反射性地调用Java 8默认方法

问题描述 投票:12回答:4

鉴于这个简单的“Hello World”是Java 8接口,如何通过反射调用其hello()方法?

public interface Hello {
    default String hello() {
        return "Hello";
    }
}
java reflection interface java-8
4个回答
15
投票

您可以使用MethodHandles

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ReflectiveDefaultMethodCallExample {

    static interface Hello {
        default String hello() {
            return "Hello";
        }
    }

    public static void main(String[] args) throws Throwable{

        Hello target =
                //new Hello(){};
                (Hello)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{Hello.class}, (Object proxy, Method method, Object[] arguments) -> null);
        Method method = Hello.class.getMethod("hello");

        Object result = MethodHandles.lookup()
                                     .in(method.getDeclaringClass())
                                     .unreflectSpecial(method,method.getDeclaringClass())
                                     .bindTo(target)
                                     .invokeWithArguments();
        System.out.println(result); //Hello
    }
}

9
投票

不幸的是,似乎没有一个理想的解决方案适用于所有JDK 8,9,10,它们的行为不同。 I've run into issues when fixing an issue in jOORI've also blogged about the correct solution here in detail

This approach works in Java 8

在Java 8中,理想的方法是使用从Lookup访问包私有构造函数的hack:

import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Constructor;
import java.lang.reflect.Proxy;

interface Duck {
    default void quack() {
        System.out.println("Quack");
    }
}

public class ProxyDemo {
    public static void main(String[] a) {
        Duck duck = (Duck) Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(),
            new Class[] { Duck.class },
            (proxy, method, args) -> {
                Constructor<Lookup> constructor = Lookup.class
                    .getDeclaredConstructor(Class.class);
                constructor.setAccessible(true);
                constructor.newInstance(Duck.class)
                    .in(Duck.class)
                    .unreflectSpecial(method, Duck.class)
                    .bindTo(proxy)
                    .invokeWithArguments();
                return null;
            }
        );

        duck.quack();
    }
}

这是唯一适用于私有访问和私有不可访问接口的方法。但是,上述方法对JDK内部进行非法反射访问,这将在将来的JDK版本中不再起作用,或者在JVM上指定--illegal-access=deny

This approach works on Java 9 and 10, but not 8

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Proxy;

interface Duck {
    default void quack() {
        System.out.println("Quack");
    }
}

public class ProxyDemo {
    public static void main(String[] a) {
        Duck duck = (Duck) Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(),
            new Class[] { Duck.class },
            (proxy, method, args) -> {
                MethodHandles.lookup()
                    .findSpecial( 
                         Duck.class, 
                         "quack",  
                         MethodType.methodType(void.class, new Class[0]),  
                         Duck.class)
                    .bindTo(proxy)
                    .invokeWithArguments();
                return null;
            }
        );

        duck.quack();
    }
}

Solution

只需实现上述两种解决方案,并检查您的代码是在JDK 8上运行还是在以后的JDK上运行,您就可以了。直到你不是:)


5
投票

您无法直接调用它,因为您需要实现类的实例。为此,您需要一个实现类。 default方法不是static方法,也不能创建接口的实例。

所以,假设你有一个实现类:

class HelloImpl implements Hello {  }

您可以像这样调用方法:

Class<HelloImpl> clazz = HelloImpl.class;
Method method = clazz.getMethod("hello");
System.out.println(method.invoke(new HelloImpl()));  // Prints "Hello"

3
投票

我有found a solution从上面的接口创建实例反射性地使用来自sun.misc.ProxyGenerator的代码,该代码通过汇编字节码来定义类HelloImpl。现在我可以写:

Class<?> clazz = Class.forName("Hello");
Object instance;

if (clazz.isInterface()) {
    instance = new InterfaceInstance(clazz).defineClass().newInstance();
} else {
    instance = clazz.newInstance();
}

return clazz.getMethod("hello").invoke(instance);

......但那很难看。

© www.soinside.com 2019 - 2024. All rights reserved.