本文整理匯總了Java中java.lang.invoke.MethodHandles.Lookup.findConstructor方法的典型用法代碼示例。如果您正苦於以下問題:Java Lookup.findConstructor方法的具體用法?Java Lookup.findConstructor怎麽用?Java Lookup.findConstructor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles.Lookup
的用法示例。
在下文中一共展示了Lookup.findConstructor方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testFindConstructor
import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
void testFindConstructor(boolean positive, Lookup lookup,
Class<?> defc, Class<?>... params) throws Throwable {
countTest(positive);
MethodType type = MethodType.methodType(void.class, params);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
target = lookup.findConstructor(defc, type);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
}
if (verbosity >= 3)
System.out.println("findConstructor "+defc.getName()+".<init>/"+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(type.changeReturnType(defc), target.type());
Object[] args = randomArgs(params);
printCalled(target, defc.getSimpleName(), args);
Object obj = target.invokeWithArguments(args);
if (!(defc == Example.class && params.length < 2))
assertCalled(defc.getSimpleName()+".<init>", args);
assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
示例2: lookupConstructor
import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
static MethodHandle lookupConstructor(Lookup lookup, Class<?> refc, Class<?> ptypes) {
try {
return lookup.findConstructor(refc, MethodType.methodType(void.class, ptypes));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
示例3: findConstructor
import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
/**
* Invokes Lookup findConstructor with a method type constructored from the
* given return and parameter types.
*/
static MethodHandle findConstructor(Lookup lookup,
Class<?> clazz,
Class<?> rtype,
Class<?>... ptypes) throws Exception {
MethodType mt = MethodType.methodType(rtype, ptypes);
return lookup.findConstructor(clazz, mt);
}
示例4: findNoArgConstructorAndInvoke
import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
/**
* Produces the method handle for the no-arg constructor and invokes it
*/
Object findNoArgConstructorAndInvoke(Class<?> clazz, Lookup lookup) throws Throwable {
MethodType mt = MethodType.methodType(void.class);
MethodHandle mh = lookup.findConstructor(clazz, mt);
return mh.invoke();
}