Java Code Examples for java.lang.invoke.MethodHandles.Lookup#findStatic()

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#findStatic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BootstrapMethods.java    From bumblebench with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: FlagsTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: ReflectiveLookupTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 4
Source File: LookupTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 5
Source File: ReflectionFrames.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: MethodHandlesTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: MethodHandlesTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: MethodHandlesTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: MethodHandlesTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: MethodHandlesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: CallerSensitiveTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: MethodHandlesTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: StringConcatFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: StringConcatFactory.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: MethodHandlesTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: MethodHandlesTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: LoggerImpl.java    From beautiful_logger with MIT License 5 votes vote down vote up
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);
  }
}
 
Example 18
Source File: MethodHandlesTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 19
Source File: MethodHandlesTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 20
Source File: MethodHandlesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
    }
}