我已经将我的Clojure应用程序从jdk8迁移到jdk11 (Zulu JRE11),它开始无法调用接口上的default method,该接口的实现是机器生成的(看起来有点像#object[com.sun.proxy.$Proxy110 0x28466aa5 nil])。
我知道
(.someDefaultMethod iinterface-impl)并获取
java.lang.IllegalAccessException: access to public member failed:
my.IInterface.someDefaultMethod[Ljava.lang.Object;@172aedbe/invokeSpecial, from my.IInterface/2 (unnamed module @627551fb)
at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:942)
at java.base/java.lang.invoke.MethodHandles$Lookup.checkAccess(MethodHandles.java:2206)
at java.base/java.lang.invoke.MethodHandles$Lookup.checkMethod(MethodHandles.java:2146)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:2290)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodNoSecurityManager(MethodHandles.java:2283)
at java.base/java.lang.invoke.MethodHandles$Lookup.unreflectSpecial(MethodHandles.java:1798)
at my.SomeService.invoke(SomeService.java:305)有什么解决方案吗?谢谢!
发布于 2020-01-27 00:24:07
事实证明,这个错误并不是由于Clojure造成的,而是由于调用默认方法的方式在代理处理程序的invoke中编写的
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Proxy: calling " + method);
// Default methods are public non-abstract instance methods
// declared in an interface.
if (((method.getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) ==
Modifier.PUBLIC) && method.getDeclaringClass().isInterface()) {
// see https://rmannibucau.wordpress.com/2014/03/27/java-8-default-interface-methods-and-jdk-dynamic-proxies/
final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
final Class<?> declaringClass = method.getDeclaringClass();
return constructor.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
.unreflectSpecial(method, declaringClass)
.bindTo(proxy)
.invokeWithArguments(args);
}
return null;
}https://stackoverflow.com/questions/59897831
复制相似问题