java.lang.invoke.MethodHandles Java Examples

The following examples show how to use java.lang.invoke.MethodHandles. 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: NativeCalls.java    From es6draft with MIT License 6 votes vote down vote up
private static MethodHandle forSpreadCall(MethodHandle mh, MethodType type) {
    int expectedParameters = mh.type().parameterCount();
    int actualParameters = type.parameterCount();
    if (!mh.isVarargsCollector() || !mh.type().parameterType(expectedParameters - 1).equals(Object[].class)) {
        throw new WrongMethodTypeException("Not Object[] var-args collector");
    }
    if (expectedParameters > actualParameters) {
        throw new WrongMethodTypeException("Too few arguments");
    }
    if (expectedParameters < actualParameters) {
        int fixedCount = actualParameters - expectedParameters;
        int firstFixed = expectedParameters - 1;
        List<Class<?>> fixed = type.parameterList().subList(firstFixed, firstFixed + fixedCount);
        mh = MethodHandles.collectArguments(mh, firstFixed, combineArraysMH);
        mh = MethodHandles.collectArguments(mh, firstFixed, toObjectArray(fixed));
    }
    return mh.asType(type);
}
 
Example #2
Source File: ReflectionBenchmarkUniqueAccessTest.java    From AVM with MIT License 6 votes vote down vote up
private long uniqueInstanceMethodHandleInstanceFieldWriteAccessInvokeOnly(int spins) throws Throwable {
    ReflectionTarget[] targets = new ReflectionTarget[spins];
    MethodHandle[] fields = new MethodHandle[spins];

    for (int i = 0; i < spins; i++) {
        ClassLoader loader = new URLClassLoader(new URL[]{ classpathDirectory.toURI().toURL() });
        Class<?> clazz = loader.loadClass(targetClassName);
        targets[i] = (ReflectionTarget) MethodHandles.lookup().findConstructor(clazz, MethodType.methodType(void.class)).invoke();
        fields[i] = MethodHandles.lookup().findSetter(clazz, instanceField, Object.class);
    }

    Object object = new Object();

    long start = System.nanoTime();
    for (int i = 0; i < spins; i++) {
        fields[i].invoke(targets[i], object);
    }
    long end = System.nanoTime();
    return end - start;
}
 
Example #3
Source File: LambdaReceiver.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(LambdaReceiver_A.class, "f", mt(int.class));
    MethodType X = mt(int.class, LambdaReceiver.class);
    MethodType A = mt(int.class, LambdaReceiver_A.class);
    MethodType mti = mt(IA.class);
    CallSite cs = LambdaMetafactory.metafactory(l, "m", mti,A,h,X);
    IA p = (IA)cs.dynamicInvoker().invoke();
    LambdaReceiver_A lra = new LambdaReceiver_A();
    try {
        p.m(lra);
    } catch (ClassCastException cce) {
        return;
    }
    throw new AssertionError("CCE expected");
}
 
Example #4
Source File: NashornBottomLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static MethodHandle linkMissingBeanMember(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    final String operand = NashornCallSiteDescriptor.getOperand(desc);
    switch (NashornCallSiteDescriptor.getStandardOperation(desc)) {
    case GET:
        if (NashornCallSiteDescriptor.isOptimistic(desc)) {
            return adaptThrower(MethodHandles.insertArguments(THROW_OPTIMISTIC_UNDEFINED, 0, NashornCallSiteDescriptor.getProgramPoint(desc)), desc);
        } else if (operand != null) {
            return getInvocation(EMPTY_PROP_GETTER, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_GETTER, linkerServices, desc);
    case SET:
        final boolean strict = NashornCallSiteDescriptor.isStrict(desc);
        if (strict) {
            return adaptThrower(bindOperand(THROW_STRICT_PROPERTY_SETTER, operand), desc);
        } else if (operand != null) {
            return getInvocation(EMPTY_PROP_SETTER, linkerServices, desc);
        }
        return getInvocation(EMPTY_ELEM_SETTER, linkerServices, desc);
    default:
        throw new AssertionError("unknown call type " + desc);
    }
}
 
Example #5
Source File: JDK12StringFunctions.java    From journaldev with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	String str = "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****";

	// 1. public String indent​(int n)
	System.out.println(str.indent(0));
	System.out.println(str.indent(3));
	System.out.println(str.indent(-3));

	// 2. public <R> R transform​(Function<? super String,​? extends R> f)
	String s = "Hi,Hello,Howdy";
	List<String> strList = s.transform(s1 -> {return Arrays.asList(s1.split(","));});
	System.out.println(strList);

	//Java 12 String implements Constable, ConstantDesc - hence two new methods
	// 3. public Optional<String> describeConstable()
	String so = "Hello";
	Optional<String> os = so.describeConstable();
	System.out.println(os);
	System.out.println(os.get());
	
	// 4. public String resolveConstantDesc​(MethodHandles.Lookup lookup)
	String so1 = "Hello";
	System.out.println(so1.resolveConstantDesc(MethodHandles.lookup()));
	
	
}
 
Example #6
Source File: Method.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Handle generateBootstrapMethod(Handle h) {
    String bootstrapName = "bootstrapMethod";
    MethodType bootstrapType = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);

    MethodVisitor bmv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, bootstrapName, bootstrapType.toMethodDescriptorString(), null, null);
    bmv.visitCode();

    String constCallSite = "java/lang/invoke/ConstantCallSite";
    bmv.visitTypeInsn(NEW, constCallSite);
    bmv.visitInsn(DUP);

    bmv.visitLdcInsn(h);

    bmv.visitMethodInsn(INVOKESPECIAL, constCallSite, "<init>", "(Ljava/lang/invoke/MethodHandle;)V", false);
    bmv.visitInsn(ARETURN);

    bmv.visitMaxs(0,0);
    bmv.visitEnd();

    return new Handle(H_INVOKESTATIC, ownerClassName, bootstrapName, bootstrapType.toMethodDescriptorString());
}
 
Example #7
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 #8
Source File: PermuteArgsReturnVoidTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnOnStack() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeIdentity",
            MethodType.methodType(String.class, String.class, int.class, int.class));
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example #9
Source File: VarHandleTestAccessBoolean.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@BeforeClass
public void setup() throws Exception {
    vhFinalField = MethodHandles.lookup().findVarHandle(
            VarHandleTestAccessBoolean.class, "final_v", boolean.class);

    vhField = MethodHandles.lookup().findVarHandle(
            VarHandleTestAccessBoolean.class, "v", boolean.class);

    vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
        VarHandleTestAccessBoolean.class, "static_final_v", boolean.class);

    vhStaticField = MethodHandles.lookup().findStaticVarHandle(
        VarHandleTestAccessBoolean.class, "static_v", boolean.class);

    vhArray = MethodHandles.arrayElementVarHandle(boolean[].class);
}
 
Example #10
Source File: SerializerH3Enum.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
private MethodHandle introspectConstructor()
{
  try {
    Method m = _type.getMethod("valueOf", String.class);
    
    Objects.requireNonNull(m);
    
    m.setAccessible(true);
      
    MethodHandle mh = MethodHandles.lookup().unreflect(m);
        
    mh = mh.asType(MethodType.methodType(Object.class, String.class));
      
    return mh;
  } catch (Exception e) {
    throw new H3ExceptionIn(_type.getName() + ": " + e.getMessage(), e);
  }
}
 
Example #11
Source File: ReflectionFactory.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl,
                                                               String methodName,
                                                               Class<?> streamClass) {
    if (!Serializable.class.isAssignableFrom(cl)) {
        return null;
    }

    try {
        Method meth = cl.getDeclaredMethod(methodName, streamClass);
        int mods = meth.getModifiers();
        if (meth.getReturnType() != Void.TYPE ||
                Modifier.isStatic(mods) ||
                !Modifier.isPrivate(mods)) {
            return null;
        }
        meth.setAccessible(true);
        return MethodHandles.lookup().unreflect(meth);
    } catch (NoSuchMethodException ex) {
        return null;
    } catch (IllegalAccessException ex1) {
        throw new InternalError("Error", ex1);
    }
}
 
Example #12
Source File: DefaultStaticInvokeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private MethodHandle getTestMH(Class clazz, String methodName,
        Object param, boolean isNegativeTest)
        throws Exception {
    MethodType mType = (param != null)
            ? MethodType.genericMethodType(1)
            : MethodType.methodType(String.class);
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    if (!isNegativeTest) {
        return methodName.equals("staticMethod")
                ? lookup.findStatic(clazz, methodName, mType)
                : lookup.findVirtual(clazz, methodName, mType);
    } else {
        return methodName.equals("staticMethod")
                ? lookup.findVirtual(clazz, methodName, mType)
                : lookup.findStatic(clazz, methodName, mType);
    }
}
 
Example #13
Source File: SpecialStatic.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testFindSpecial() throws Throwable {
    MethodHandles.Lookup lookup = (MethodHandles.Lookup)t3.getDeclaredMethod("getLookup").invoke(null);
    MethodHandle mh = lookup.findSpecial(t1, "m", MethodType.methodType(int.class), t3);
    int result = (int)mh.invoke(t3.newInstance());
    assertEquals(result, 1); // T1.m should be invoked.
}
 
Example #14
Source File: ExplicitCastArgumentsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
    try {
        MethodHandles.explicitCastArguments(mh, mt);
        throw new AssertionError("Expected WrongMethodTypeException is not thrown");
    } catch (WrongMethodTypeException wmte) {
        if (VERBOSE) {
            System.out.printf("Expected exception %s: %s\n",
                    wmte.getClass(), wmte.getMessage());
        }
    }
}
 
Example #15
Source File: TypeConverterFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle applyConverters(final MethodHandle handle, final int pos, final List<MethodHandle> converters) {
    if(converters.isEmpty()) {
        return handle;
    }
    final MethodHandle newHandle =
            MethodHandles.filterArguments(handle, pos, converters.toArray(new MethodHandle[converters.size()]));
    converters.clear();
    return newHandle;
}
 
Example #16
Source File: MarshalBeanToMap.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
private void introspectFields(ArrayList<MarshalField> fieldList,
                              PodImport moduleImport,
                              Class<?> sourceClass)
{
  if (sourceClass == null || Object.class.equals(sourceClass)) {
    return;
  }
  
  introspectFields(fieldList, 
                   moduleImport,
                   sourceClass.getSuperclass());
  
  Lookup lookup = MethodHandles.lookup();
  
  for (Field sourceField : sourceClass.getDeclaredFields()) {
    if (Modifier.isStatic(sourceField.getModifiers())) {
      continue;
    }
    
    try {
      sourceField.setAccessible(true);
      
      ModuleMarshal marshal;
      marshal = moduleImport.marshalArg(sourceField.getType(),
                                        Object.class);
      
      MethodHandle sourceHandle = lookup.unreflectGetter(sourceField);
      sourceHandle = sourceHandle.asType(MethodType.methodType(Object.class, Object.class));
    
      MarshalField fieldMarshal = new MarshalFieldObject(sourceField.getName(),
                                                         marshal, 
                                                         sourceHandle);
      
      fieldList.add(fieldMarshal);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 
Example #17
Source File: Selector.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void handleNullWithoutBoolean() {
    if (handle != null || args[0] != null) return;

    if (staticTargetType.isPrimitive()) {
        handle = MethodHandles.insertArguments(GROOVY_CAST_EXCEPTION, 1, staticTargetType);
        // need to call here here because we used the static target type
        // it won't be done otherwise because handle.type() == callSite.type()
        castAndSetGuards();
    } else {
        handle = MethodHandles.identity(staticSourceType);
    }
}
 
Example #18
Source File: NashornCallSiteDescriptor.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private NashornCallSiteDescriptor(final MethodHandles.Lookup lookup, final String operator, final String operand,
        final MethodType methodType, final int flags) {
    this.lookup = lookup;
    this.operator = operator;
    this.operand = operand;
    this.methodType = methodType;
    this.flags = flags;
}
 
Example #19
Source File: Test8009222.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    MethodHandles.lookup()
            .findStaticGetter(Intf.class, "i", int.class)
            .getClass(); // null check

    System.out.println("TEST PASSED");
}
 
Example #20
Source File: MethodHandleFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public MethodHandle setter(final MethodHandles.Lookup explicitLookup, final Class<?> clazz, final String name, final Class<?> type) {
    try {
        final MethodHandle mh = explicitLookup.findSetter(clazz, name, type);
        return debug(mh, "setter", explicitLookup, clazz, name, type);
    } catch (final NoSuchFieldException | IllegalAccessException e) {
        throw new LookupException(e);
    }
}
 
Example #21
Source File: VariableHandlesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
    VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
      .lookup()
      .in(VariableHandlesUnitTest.class)
      .findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);

    assertEquals(1, PUBLIC_TEST_VARIABLE.coordinateTypes().size());
    assertEquals(VariableHandlesUnitTest.class, PUBLIC_TEST_VARIABLE.coordinateTypes().get(0));
}
 
Example #22
Source File: ValueConversions.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle unbox(Wrapper wrap, int kind) {
    // kind 0 -> strongly typed with NPE
    // kind 1 -> strongly typed but zero for null,
    // kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
    // kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
    WrapperCache cache = UNBOX_CONVERSIONS[kind];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    switch (wrap) {
        case OBJECT:
        case VOID:
            throw new IllegalArgumentException("unbox "+wrap);
    }
    // look up the method
    String name = "unbox" + wrap.wrapperSimpleName();
    MethodType type = unboxType(wrap, kind);
    try {
        mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
    } catch (ReflectiveOperationException ex) {
        mh = null;
    }
    if (mh != null) {
        if (kind > 0) {
            boolean cast = (kind != 2);
            mh = MethodHandles.insertArguments(mh, 1, cast);
        }
        if (kind == 1) {  // casting but exact (null -> zero)
            mh = mh.asType(unboxType(wrap, 0));
        }
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find unbox adapter for " + wrap
            + (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
}
 
Example #23
Source File: NashornCallSiteDescriptor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static NashornCallSiteDescriptor get(final MethodHandles.Lookup lookup, final String operator, final String operand, final MethodType methodType, final int flags) {
    final NashornCallSiteDescriptor csd = new NashornCallSiteDescriptor(lookup, operator, operand, methodType, flags);
    // Many of these call site descriptors are identical (e.g. every getter for a property color will be
    // "dyn:getProp:color(Object)Object", so it makes sense canonicalizing them.
    final ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor> classCanonicals = canonicals.get(lookup.lookupClass());
    final NashornCallSiteDescriptor canonical = classCanonicals.putIfAbsent(csd, csd);
    return canonical != null ? canonical : csd;
}
 
Example #24
Source File: T8032697.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    l = MethodHandles.lookup();
    h = l.findVirtual(T8032697_A.class, "f", mt(int.class));
    if (mf(mt(I.class, T8032697.class), mt(int.class))) throw new AssertionError("Error: Should work");
    if (mf(mt(IA.class), mt(int.class, T8032697.class))) throw new AssertionError("Error: Should work");
    if (!mf(mt(I.class, T8032697_A.class), mt(int.class))) throw new AssertionError("Error: Should fail");
    if (!mf(mt(IA.class), mt(int.class, T8032697_A.class))) throw new AssertionError("Error: Should fail");
}
 
Example #25
Source File: MethodHandleFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public MethodHandle findSpecial(final MethodHandles.Lookup explicitLookup, final Class<?> clazz, final String name, final MethodType type, final Class<?> thisClass) {
    try {
        final MethodHandle mh = explicitLookup.findSpecial(clazz, name, type, thisClass);
        return debug(mh, "findSpecial", explicitLookup, clazz, name, type);
    } catch (final NoSuchMethodException | IllegalAccessException e) {
        throw new LookupException(e);
    }
}
 
Example #26
Source File: GuardedInvocation.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
 * @param guardFallback the fallback method handle in case guard returns false.
 * @param catchFallback the fallback method in case the exception handler triggers
 * @return a composite method handle.
 */
public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
    final MethodHandle guarded =
            guard == null ?
                    invocation :
                    MethodHandles.guardWithTest(
                            guard,
                            invocation,
                            guardFallback);

    final MethodHandle catchGuarded =
            exception == null ?
                    guarded :
                    MH.catchException(
                            guarded,
                            exception,
                            MethodHandles.dropArguments(
                                catchFallback,
                                0,
                                exception));

    if (switchPoints == null) {
        return catchGuarded;
    }

    MethodHandle spGuarded = catchGuarded;
    for (final SwitchPoint sp : switchPoints) {
        spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
    }

    return spGuarded;
}
 
Example #27
Source File: RpcMethod.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
private RpcMethod(INSTANCE instance, Method method, String[] parameterNames,String methodName,
                  boolean returnTypeJdk9PublisherFlag, boolean returnTypeReactivePublisherFlag,
                  boolean returnRxjava3ObservableFlag, boolean returnRxjava3FlowableFlag) {
    this.instance = instance;
    this.method = method;
    this.methodName = methodName;
    this.parameterNames = parameterNames;
    this.returnTypeJdk9PublisherFlag = returnTypeJdk9PublisherFlag;
    this.returnTypeReactivePublisherFlag = returnTypeReactivePublisherFlag;
    this.returnRxjava3ObservableFlag = returnRxjava3ObservableFlag;
    this.returnRxjava3FlowableFlag = returnRxjava3FlowableFlag;
    this.returnCompletableFutureFlag = CompletableFuture.class.isAssignableFrom(method.getReturnType());
    this.returnCompletionStageFlag = CompletionStage.class.isAssignableFrom(method.getReturnType());
    this.returnFutureFlag = Future.class.isAssignableFrom(method.getReturnType());
    this.parameterTypes = method.getParameterTypes();
    if(returnTypeJdk9PublisherFlag || returnTypeReactivePublisherFlag
            || returnRxjava3ObservableFlag || returnRxjava3FlowableFlag
            || returnCompletableFutureFlag || returnCompletionStageFlag || returnFutureFlag){
        this.genericReturnType = getParameterizedType(method);
    }else {
        this.genericReturnType = method.getGenericReturnType();
    }
    this.innerMethodFlag = RpcServerInstance.isRpcInnerClass(method.getDeclaringClass());
    this.parameterTypeDescriptorName = Stream.of(parameterTypes)
            .map(Class::getSimpleName)
            .collect(Collectors.joining(","));
    this.methodDescriptorName = getMethodDescriptorName(method);
    this.parameterCount = method.getParameterCount();
    MethodHandle methodHandle;
    try {
        MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
        MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
        methodHandle = publicLookup.findVirtual(method.getDeclaringClass(), method.getName(), methodType);
    } catch (NoSuchMethodException | IllegalAccessException e) {
        methodHandle = null;
    }
    this.methodHandle = methodHandle;
}
 
Example #28
Source File: CatchExceptionTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void runTest() {
    Helper.clear();

    Object[] args = Helper.randomArgs(
            argsCount, thrower.type().parameterArray());
    Object arg0 = Helper.MISSING_ARG;
    Object arg1 = testCase.thrown;
    if (argsCount > 0) {
        arg0 = args[0];
    }
    if (argsCount > 1) {
        args[1] = arg1;
    }
    Asserts.assertEQ(nargs, thrower.type().parameterCount());
    if (argsCount < nargs) {
        Object[] appendArgs = {arg0, arg1};
        appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
        thrower = MethodHandles.insertArguments(
                thrower, argsCount, appendArgs);
    }
    Asserts.assertEQ(argsCount, thrower.type().parameterCount());

    MethodHandle target = MethodHandles.catchException(
            testCase.filter(thrower), testCase.throwableClass,
            testCase.filter(catcher));

    Asserts.assertEQ(thrower.type(), target.type());
    Asserts.assertEQ(argsCount, target.type().parameterCount());

    Object returned;
    try {
        returned = target.invokeWithArguments(args);
    } catch (Throwable ex) {
        testCase.assertCatch(ex);
        returned = ex;
    }

    testCase.assertReturn(returned, arg0, arg1, dropped, args);
}
 
Example #29
Source File: Selector.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Set MOP based constructor invocation path.
 */
@Override
public void setMetaClassCallHandleIfNeeded(boolean standardMetaClass) {
    if (handle != null) return;
    useMetaClass = true;
    if (LOG_ENABLED) LOG.info("set meta class invocation path");
    handle = MOP_INVOKE_CONSTRUCTOR.bindTo(mc);
    handle = handle.asCollector(Object[].class, targetType.parameterCount() - 1);
    handle = MethodHandles.dropArguments(handle, 0, Class.class);
    if (LOG_ENABLED) LOG.info("create collector for arguments");
}
 
Example #30
Source File: MethodHandleFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public MethodHandle findStatic(final MethodHandles.Lookup explicitLookup, final Class<?> clazz, final String name, final MethodType type) {
    try {
        return explicitLookup.findStatic(clazz, name, type);
    } catch (final NoSuchMethodException | IllegalAccessException e) {
        throw new LookupException(e);
    }
}