本文整理汇总了Java中java.lang.invoke.MethodHandles.Lookup.findStatic方法的典型用法代码示例。如果您正苦于以下问题:Java Lookup.findStatic方法的具体用法?Java Lookup.findStatic怎么用?Java Lookup.findStatic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles.Lookup
的用法示例。
在下文中一共展示了Lookup.findStatic方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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());
Method lookupMethod = MethodHandles.class.getMethod("lookup");
System.out.println("reflection method: " + lookupMethod);
if (!lookupMethod.getName().equals("lookup")) {
throw new RuntimeException("Unexpected name: " + lookupMethod.getName());
}
// Get a full power Lookup reflectively.
Lookup lookup2 = (Lookup) lookupMethod.invoke(null);
assertEquals(lookup1.lookupClass(), lookup2.lookupClass());
assertEquals(lookup1.lookupModes(), lookup2.lookupModes());
MethodHandle mh2 = lookup2.findStatic(lookup2.lookupClass(),
"foo",
methodType(String.class));
assertEquals((String) mh2.invokeExact(), foo());
}
示例3: 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);
}
示例4: lookupStatic
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
static MethodHandle lookupStatic(Lookup lookup, Class<?> refc, String name, Class<?> rtype, Class<?>... ptypes) {
try {
return lookup.findStatic(refc, name, MethodType.methodType(rtype, ptypes));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
示例5: handle
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
public static StackInspector handle(How how) throws Exception {
Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findStatic(Caller.class, "create",
MethodType.methodType(StackInspector.class, How.class));
try {
return (StackInspector) mh.invoke(how);
} catch (Error | Exception x) {
throw x;
} catch(Throwable t) {
throw new AssertionError(t);
}
}
示例6: invokeMethodHandle
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
void invokeMethodHandle(Lookup lookup) throws Throwable {
MethodHandle mh1 = lookup.findStatic(java.util.CSM.class, CSM_CALLER_METHOD,
MethodType.methodType(Class.class));
Class<?> c = (Class<?>)mh1.invokeExact();
MethodHandle mh2 = lookup.findStatic(java.util.CSM.class, NON_CSM_CALLER_METHOD,
MethodType.methodType(Result.class));
Result result = (Result)mh2.invokeExact();
checkNonCSMCaller(CallerSensitiveTest.class, result);
}
示例7: initHello
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
private static MethodHandle initHello(Lookup lookup) throws NoSuchMethodException, IllegalAccessException {
return lookup.findStatic(lookup.lookupClass(), "hello", methodType(String.class, String.class));
}
示例8: init
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
private static MethodHandle init(Lookup lookup) throws NoSuchMethodException, IllegalAccessException {
return lookup.findStatic(lookup.lookupClass(), "incr", methodType(int.class, int.class));
}
示例9: testGenericLoopCombinator0
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
public void testGenericLoopCombinator0() throws Throwable {
if (CAN_SKIP_WORKING) return;
startTest("loop");
// Test as follows:
// * Have an increasing number of loop-local state. Local state type diversity grows with the number.
// * Initializers set the starting value of loop-local state from the corresponding loop argument.
// * For each local state element, there is a predicate - for all state combinations, exercise all predicates.
// * Steps modify each local state element in each iteration.
// * Finalizers group all local state elements into a resulting array. Verify end values.
// * Exercise both pre- and post-checked loops.
// Local state types, start values, predicates, and steps:
// * int a, 0, a < 7, a = a + 1
// * double b, 7.0, b > 0.5, b = b / 2.0
// * String c, "start", c.length <= 9, c = c + a
final Class<?>[] argTypes = new Class<?>[] {int.class, double.class, String.class};
final Object[][] args = new Object[][] {
new Object[]{0 },
new Object[]{0, 7.0 },
new Object[]{0, 7.0, "start"}
};
// These are the expected final state tuples for argument type tuple / predicate combinations, for pre- and
// post-checked loops:
final Object[][] preCheckedResults = new Object[][] {
new Object[]{7 }, // (int) / int
new Object[]{7, 0.0546875 }, // (int,double) / int
new Object[]{5, 0.4375 }, // (int,double) / double
new Object[]{7, 0.0546875, "start1234567"}, // (int,double,String) / int
new Object[]{5, 0.4375, "start1234" }, // (int,double,String) / double
new Object[]{6, 0.109375, "start12345" } // (int,double,String) / String
};
final Object[][] postCheckedResults = new Object[][] {
new Object[]{7 }, // (int) / int
new Object[]{7, 0.109375 }, // (int,double) / int
new Object[]{4, 0.4375 }, // (int,double) / double
new Object[]{7, 0.109375, "start123456"}, // (int,double,String) / int
new Object[]{4, 0.4375, "start123" }, // (int,double,String) / double
new Object[]{5, 0.21875, "start12345" } // (int,double,String) / String
};
final Lookup l = MethodHandles.lookup();
final Class<?> MHT = MethodHandlesTest.class;
final Class<?> B = boolean.class;
final Class<?> I = int.class;
final Class<?> D = double.class;
final Class<?> S = String.class;
final MethodHandle hip = l.findStatic(MHT, "loopIntPred", methodType(B, I));
final MethodHandle hdp = l.findStatic(MHT, "loopDoublePred", methodType(B, I, D));
final MethodHandle hsp = l.findStatic(MHT, "loopStringPred", methodType(B, I, D, S));
final MethodHandle his = l.findStatic(MHT, "loopIntStep", methodType(I, I));
final MethodHandle hds = l.findStatic(MHT, "loopDoubleStep", methodType(D, I, D));
final MethodHandle hss = l.findStatic(MHT, "loopStringStep", methodType(S, I, D, S));
final MethodHandle[] preds = new MethodHandle[] {hip, hdp, hsp};
final MethodHandle[] steps = new MethodHandle[] {his, hds, hss};
for (int nargs = 1, useResultsStart = 0; nargs <= argTypes.length; useResultsStart += nargs++) {
Class<?>[] useArgTypes = Arrays.copyOf(argTypes, nargs, Class[].class);
MethodHandle[] usePreds = Arrays.copyOf(preds, nargs, MethodHandle[].class);
MethodHandle[] useSteps = Arrays.copyOf(steps, nargs, MethodHandle[].class);
Object[] useArgs = args[nargs - 1];
Object[][] usePreCheckedResults = new Object[nargs][];
Object[][] usePostCheckedResults = new Object[nargs][];
System.arraycopy(preCheckedResults, useResultsStart, usePreCheckedResults, 0, nargs);
System.arraycopy(postCheckedResults, useResultsStart, usePostCheckedResults, 0, nargs);
testGenericLoopCombinator(nargs, useArgTypes, usePreds, useSteps, useArgs, usePreCheckedResults,
usePostCheckedResults);
}
}