本文整理汇总了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();
}