本文整理匯總了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);
}
}