java.lang.invoke.MethodHandles.Lookup#findStatic ( )源码实例Demo

下面列出了java.lang.invoke.MethodHandles.Lookup#findStatic ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bumblebench   文件: BootstrapMethods.java
public static CallSite gwtBootstrap(Lookup ignored, String name, MethodType type) throws Throwable {
	Lookup lookup = lookup();
	MethodHandle double_string = lookup.findStatic(BootstrapMethods.class, "dup", methodType(String.class, String.class));
	MethodHandle double_integer = lookup.findStatic(BootstrapMethods.class, "dup", methodType(String.class, Integer.class));
	MethodHandle double_object = lookup.findStatic(BootstrapMethods.class, "dup", methodType(String.class, Object.class));
	MethodHandle isInteger = lookup.findStatic(BootstrapMethods.class, "isInteger", methodType(boolean.class, Object.class));
	MethodHandle isString = lookup.findStatic(BootstrapMethods.class, "isString", methodType(boolean.class, Object.class));

	MethodHandle handle = guardWithTest(
			isString, 
			double_string.asType(methodType(String.class, Object.class)),
			double_object);
	handle = guardWithTest(
			isInteger,
			double_integer.asType(methodType(String.class, Object.class)),
			handle);
	return new MutableCallSite(handle);
}
 
源代码2 项目: armeria   文件: FlagsTest.java
@Test
void dumpOpenSslInfoDoNotThrowStackOverFlowError() throws Throwable {
    assumeThat(OpenSsl.isAvailable()).isTrue();
    System.setProperty("com.linecorp.armeria.dumpOpenSslInfo", "true");

    // There's a chance that Flags.useOpenSsl() is already called by other test cases, which means that
    // we cannot set dumpOpenSslInfo. So we use our own class loader to load the Flags class.
    final FlagsClassLoader classLoader = new FlagsClassLoader();
    final Class<?> flags = classLoader.loadClass("com.linecorp.armeria.common.Flags");
    final Lookup lookup = MethodHandles.publicLookup();
    final MethodHandle useOpenSslMethodHandle = lookup.findStatic(flags, "useOpenSsl",
                                                                  MethodType.methodType(boolean.class));
    useOpenSslMethodHandle.invoke(); // Call Flags.useOpenSsl();

    final MethodHandle dumpOpenSslInfoMethodHandle =
            lookup.findStatic(flags, "dumpOpenSslInfo", MethodType.methodType(boolean.class));
    // // Call Flags.dumpOpenSslInfo();
    assertThat(dumpOpenSslInfoMethodHandle.invoke()).isSameAs(Boolean.TRUE);
}
 
源代码3 项目: openjdk-jdk9   文件: LookupTest.java
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();
        }
    }
}
 
源代码4 项目: openjdk-jdk9   文件: ReflectiveLookupTest.java
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());
}
 
源代码5 项目: dragonwell8_jdk   文件: MethodHandlesTest.java
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);
}
 
源代码6 项目: TencentKona-8   文件: MethodHandlesTest.java
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);
}
 
源代码7 项目: jdk8u_jdk   文件: MethodHandlesTest.java
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);
}
 
源代码8 项目: beautiful_logger   文件: LoggerImpl.java
static MethodHandle createFactory(Lookup lookup, Class<?> loggerClass) {
  try {
    return lookup.findStatic(loggerClass, "create", methodType(Logger.class, MethodHandle.class));
  } catch (NoSuchMethodException | IllegalAccessException e) {
    throw new IllegalStateException(e);
  }
}
 
源代码9 项目: openjdk-jdk8u   文件: MethodHandlesTest.java
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);
}
 
源代码10 项目: openjdk-jdk8u-backup   文件: MethodHandlesTest.java
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);
}
 
源代码11 项目: Bytecoder   文件: StringConcatFactory.java
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);
    }
}
 
源代码12 项目: openjdk-jdk9   文件: StringConcatFactory.java
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);
    }
}
 
源代码13 项目: openjdk-jdk9   文件: ReflectionFrames.java
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);
    }
}
 
源代码14 项目: openjdk-jdk9   文件: CallerSensitiveTest.java
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);
}
 
源代码15 项目: openjdk-jdk9   文件: MethodHandlesTest.java
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);
}
 
源代码16 项目: jdk8u-jdk   文件: MethodHandlesTest.java
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);
}
 
源代码17 项目: hottub   文件: MethodHandlesTest.java
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);
}
 
源代码18 项目: openjdk-8-source   文件: MethodHandlesTest.java
@Test
public void testUserClassInSignature() 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);
}
 
源代码19 项目: openjdk-8   文件: MethodHandlesTest.java
@Test
public void testUserClassInSignature() 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);
}
 
源代码20 项目: openjdk-jdk9   文件: MethodHandlesTest.java
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);
    }
}