本文整理匯總了Java中java.lang.invoke.MethodHandles.Lookup.findSpecial方法的典型用法代碼示例。如果您正苦於以下問題:Java Lookup.findSpecial方法的具體用法?Java Lookup.findSpecial怎麽用?Java Lookup.findSpecial使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles.Lookup
的用法示例。
在下文中一共展示了Lookup.findSpecial方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testFindSpecial
import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
countTest(positive);
String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
MethodType type = MethodType.methodType(ret, params);
Lookup specialLookup = maybeMoveIn(lookup, specialCaller);
boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
(specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
if (verbosity >= 5) System.out.println(" lookup => "+specialLookup);
target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertExceptionClass(
(!specialAccessOK) // this check should happen first
? IllegalAccessException.class
: (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
? NoSuchMethodException.class
: IllegalAccessException.class,
noAccess);
if (verbosity >= 5) ex.printStackTrace(System.out);
}
if (verbosity >= 3)
System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+type+" => "+target
+(target == null ? "" : target.type())
+(noAccess == null ? "" : " !! "+noAccess));
if (positive && noAccess != null) throw noAccess;
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
if (!positive) return; // negative test failed as expected
assertEquals(specialCaller, target.type().parameterType(0));
assertEquals(type, target.type().dropParameterTypes(0,1));
Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
assertNameStringContains(target, methodName);
Object[] args = randomArgs(paramsWithSelf);
printCalled(target, name, args);
target.invokeWithArguments(args);
assertCalled(name, args);
}