本文整理汇总了Java中java.lang.invoke.MethodHandles.Lookup.findVirtual方法的典型用法代码示例。如果您正苦于以下问题:Java Lookup.findVirtual方法的具体用法?Java Lookup.findVirtual怎么用?Java Lookup.findVirtual使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles.Lookup
的用法示例。
在下文中一共展示了Lookup.findVirtual方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
public static void main(String... args) throws Throwable {
// Get a full power lookup
Lookup lookup1 = MethodHandles.lookup();
MethodHandle mh1 = lookup1.findStatic(lookup1.lookupClass(),
"foo",
methodType(String.class));
assertEquals((String) mh1.invokeExact(), foo());
// access protected member
MethodHandle mh2 = lookup1.findVirtual(java.lang.ClassLoader.class,
"getPackage",
methodType(Package.class, String.class));
ClassLoader loader = ClassLoader.getPlatformClassLoader();
Package pkg = (Package)mh2.invokeExact(loader, "java.lang");
assertEquals(pkg.getName(), "java.lang");
// MethodHandles.lookup will fail if it's called reflectively
try {
MethodHandles.class.getMethod("lookup").invoke(null);
} catch (InvocationTargetException e) {
if (!(e.getCause() instanceof IllegalArgumentException)) {
throw e.getCause();
}
}
}
示例2: testUserClassInSignature0
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
public void testUserClassInSignature0() throws Throwable {
if (CAN_SKIP_WORKING) return;
startTest("testUserClassInSignature");
Lookup lookup = MethodHandles.lookup();
String name; MethodType mt; MethodHandle mh;
Object[] args;
// Try a static method.
name = "userMethod";
mt = MethodType.methodType(Example.class, Object.class, String.class, int.class);
mh = lookup.findStatic(lookup.lookupClass(), name, mt);
assertEquals(mt, mh.type());
assertEquals(Example.class, mh.type().returnType());
args = randomArgs(mh.type().parameterArray());
mh.invokeWithArguments(args);
assertCalled(name, args);
// Try a virtual method.
name = "v2";
mt = MethodType.methodType(Object.class, Object.class, int.class);
mh = lookup.findVirtual(Example.class, name, mt);
assertEquals(mt, mh.type().dropParameterTypes(0,1));
assertTrue(mh.type().parameterList().contains(Example.class));
args = randomArgs(mh.type().parameterArray());
mh.invokeWithArguments(args);
assertCalled(name, args);
}
示例3: lookupVirtual
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
static MethodHandle lookupVirtual(Lookup lookup, Class<?> refc, String name, Class<?> rtype, Class<?>... ptypes) {
try {
return lookup.findVirtual(refc, name, MethodType.methodType(rtype, ptypes));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
示例4: handleCaller
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
public static Class<?> handleCaller() throws Exception {
Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findVirtual(StackWalker.class, "getCallerClass",
MethodType.methodType(Class.class));
try {
return (Class<?>) mh.invoke(walker.get());
} catch (Error | Exception x) {
throw x;
} catch(Throwable t) {
throw new AssertionError(t);
}
}