如何使用MethodHandle来调用私有实例方法? [英] How to use a MethodHandle to invoke a private instance method?
问题描述
我有这个课程:
I have this class:
package com.gmail.inverseconduit.jchatexchange.assets;
import jchatexchange.util.User;
public class UserFactory implements jchatexchange.factory.Factory {
private boolean cnu(User user) {
[implementation dependant on #cnuz(User)]
}
private int cnuz(User user) {
[my implementation]
}
@Override
public User createNewUser(int id) {
[my implementation]
}
// Other methods
}
我想获得另一个类中三种方法中每种方法的 MethodHandle
,所以我使用了:
I want to obtain a MethodHandle
of each of the three methods in another class, so I used:
class Baz {
public static void main(String..$) {
Lookup myLookup = MethodHandles.lookup();
UserFactory foo = new UserFactory(); // test
try {
MethodType signature = MethodType.methodType(boolean.class, User.class);
MethodHandle pmh = lookup.findVirtual(UserFactory.class, "cnu", signature);
// ??? what now? I can't find any method in Java API that does what I need -
// to actually invoke a private method with this.
// I would have expected this to work:
boolean cnuResult = pmh.bindTo(foo).invoke();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我需要 Lookup.IMPL_LOOKUP
,但没有允许它的公共方式。我应该使用什么来使其工作?
附加说明: MethodHandle
s在Java 6中添加,但我使用的是Java 8,因此任何与Java 8相关的技巧都将受到赞赏。
I need Lookup.IMPL_LOOKUP
, but there is no public way that allows it. What should I use in order for this to work?
Additional note: MethodHandle
s are added in Java 6, but I'm using Java 8, so any tricks related to Java 8 will be appreciated as well.
推荐答案
){
Lookup myLookup = MethodHandles.lookup();
UserFactory foo = new UserFactory(); // test
尝试 {
MethodType signature = MethodType.methodType( boolean 。 class ,User。 class );
MethodHandle pmh = lookup.findVirtual(UserFactory。 class , cnu,签名);
// ???现在怎么办?我在Java API中找不到我需要的任何方法 -
// 用它来实际调用私有方法。
// 我会有期望这个工作:
boolean cnuResult = pmh.bindTo(foo).invoke();
} catch (例外e){
e.printStackTrace();
}
}
}
) { Lookup myLookup = MethodHandles.lookup(); UserFactory foo = new UserFactory(); // test try { MethodType signature = MethodType.methodType(boolean.class, User.class); MethodHandle pmh = lookup.findVirtual(UserFactory.class, "cnu", signature); // ??? what now? I can't find any method in Java API that does what I need - // to actually invoke a private method with this. // I would have expected this to work: boolean cnuResult = pmh.bindTo(foo).invoke(); } catch (Exception e) { e.printStackTrace(); } } }
我需要 Lookup.IMPL_LOOKUP
,但没有允许它的公共方式。我应该使用什么来使其工作?
附加说明: MethodHandle
s在Java 6中添加,但我使用的是Java 8,因此任何与Java 8相关的技巧都将受到赞赏。
I need Lookup.IMPL_LOOKUP
, but there is no public way that allows it. What should I use in order for this to work?
Additional note: MethodHandle
s are added in Java 6, but I'm using Java 8, so any tricks related to Java 8 will be appreciated as well.
您可以使用Lookup.unreflect [<一个href =http://docs.oracle.com/javase/8/docs/api/java/lang/invoke/MethodHandles.Lookup.html#unreflect-java.lang.reflect.Method-\"target =_ blanktitle =新窗口> ^ ]:
You can use Lookup.unreflect[^]:
public static void main(String..
){
Lookup myLookup = MethodHandles.lookup();
UserFactory foo = new UserFactory(); // test
用户baz = new User();
尝试 {
方法m = UserFactory。 class .getDeclaredMethod ( cnu,User。 class );
m.setAccessible(true);
MethodHandle pmh = myLookup.unreflect(m);
boolean b =( boolean )pmh.invoke(foo,baz);
} catch (例外e){
e.printStackTrace();
} catch (Throwable e){
e.printStackTrace();
}
}
) { Lookup myLookup = MethodHandles.lookup(); UserFactory foo = new UserFactory(); // test User baz = new User(); try { Method m = UserFactory.class.getDeclaredMethod("cnu", User.class); m.setAccessible(true); MethodHandle pmh = myLookup.unreflect(m); boolean b = (boolean)pmh.invoke(foo, baz); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } }
这篇关于如何使用MethodHandle来调用私有实例方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!