java.lang.invoke.MethodHandleInfo Java Examples

The following examples show how to use java.lang.invoke.MethodHandleInfo. 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: InternalFunction.java    From AVM with MIT License 6 votes vote down vote up
public static InternalFunction createFunction(MethodHandles.Lookup lookup, MethodHandle target) {
    // Note that we need to convert this from a MethodHandle to a traditional reflection Method since we need to serialize it
    // and can't access the right MethodHandles.Lookup instance, later on.
    // We do that here, just to statically prove it is working.
    MethodHandleInfo info = lookup.revealDirect(target);
    Class<?> receiver = info.getDeclaringClass();
    String methodName = info.getName();
    MethodType type = info.getMethodType();
    Class<?> parameterType;

    if (info.getReferenceKind() == MethodHandleInfo.REF_invokeStatic || info.getReferenceKind() == MethodHandleInfo.REF_newInvokeSpecial) {
        parameterType = type.parameterType(0);
    } else if (info.getReferenceKind() == MethodHandleInfo.REF_invokeVirtual || info.getReferenceKind() == MethodHandleInfo.REF_invokeInterface) {
        parameterType = null;
    } else {
        throw RuntimeAssertionError.unimplemented("Unexpected MethodType " + info.getReferenceKind());
    }

    RuntimeAssertionError.assertTrue(methodName.startsWith(METHOD_PREFIX) || methodName.equals(INIT_NAME));
    
    return new InternalFunction(receiver, methodName, parameterType);
}
 
Example #2
Source File: InternalRunnable.java    From AVM with MIT License 5 votes vote down vote up
public static InternalRunnable createRunnable(MethodHandles.Lookup lookup, MethodHandle target) {
    // Note that we need to convert this from a MethodHandle to a traditional reflection Method since we need to serialize it
    // and can't access the right MethodHandles.Lookup instance, later on.
    // We do that here, just to statically prove it is working.
    MethodHandleInfo info = lookup.revealDirect(target);
    Class<?> receiver = info.getDeclaringClass();
    String methodName = info.getName();
    RuntimeAssertionError.assertTrue(methodName.startsWith(METHOD_PREFIX));
    
    return new InternalRunnable(receiver, methodName);
}
 
Example #3
Source File: VarHandleBaseTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle bind(VarHandle vh, MethodHandle mh, MethodType emt) {
    assertEquals(mh.type(), emt.insertParameterTypes(0, VarHandle.class),
                 "MethodHandle type differs from access mode type");

    MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
    assertEquals(info.getMethodType(), emt,
                 "MethodHandleInfo method type differs from access mode type");

    return mh.bindTo(vh);
}
 
Example #4
Source File: VarHandleTestReflection.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
public void methodInvocationFromMethodInfo(VarHandle.AccessMode accessMode) throws Exception {
    VarHandle v = handle();

    // Try a reflective invoke using a Method obtained from cracking
    // a MethodHandle

    MethodHandle mh = MethodHandles.lookup().unreflect(
            VarHandle.class.getMethod(accessMode.methodName(), Object[].class));
    MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);
    Method im = info.reflectAs(Method.class, MethodHandles.lookup());
    im.invoke(v, new Object[]{});
}
 
Example #5
Source File: VarHandleTestReflection.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
public void reflectAsFromVarHandleInvoker(VarHandle.AccessMode accessMode) throws Exception {
    VarHandle v = handle();

    MethodHandle mh = MethodHandles.varHandleInvoker(
            accessMode, v.accessModeType(accessMode));

    MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);

    info.reflectAs(Method.class, MethodHandles.lookup());
}
 
Example #6
Source File: VarHandleTestReflection.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "accessModesProvider", expectedExceptions = IllegalArgumentException.class)
public void reflectAsFromFindVirtual(VarHandle.AccessMode accessMode) throws Exception {
    VarHandle v = handle();

    MethodHandle mh = MethodHandles.publicLookup().findVirtual(
            VarHandle.class, accessMode.methodName(), v.accessModeType(accessMode));

    MethodHandleInfo info = MethodHandles.lookup().revealDirect(mh);

    info.reflectAs(Method.class, MethodHandles.lookup());
}
 
Example #7
Source File: ReflectionObfuscationVMT11.java    From zelixkiller with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts BoundMethodHandle$Species_L to AbstractInsnNode reference
 * 
 * @param cn
 */
private AbstractInsnNode getOriginalNode(MethodHandle mh, Lookup lookup) throws Exception {
	Field original = mh.getClass().getDeclaredField("argL0");
	original.setAccessible(true);
	MethodHandle originalHandle = (MethodHandle) original.get(mh);

	MethodHandleInfo direct = lookup.revealDirect(originalHandle);

	int refKind = direct.getReferenceKind();
	Class<?> declaringClass = direct.getDeclaringClass();
	String name = direct.getName();
	MethodType methodType = direct.getMethodType();
	int op = -1;
	if (refKind <= 4) {
		switch (refKind) {
		case 1:
			op = GETFIELD;
			break;
		case 2:
			op = GETSTATIC;
			break;
		case 3:
			op = PUTFIELD;
			break;
		case 4:
			op = PUTSTATIC;
			break;
		}
		String desc;
		if (refKind <= 2) {
			desc = methodType.toMethodDescriptorString().substring(2);
		} else {
			// method handle treats field setting as a method (returning void)
			String mds = methodType.toMethodDescriptorString();
			desc = mds.substring(1, mds.lastIndexOf(')'));
		}
		return new FieldInsnNode(op, declaringClass.getName().replace('.', '/'), name, desc);
	}
	switch (refKind) {
	case 5:
		op = INVOKEVIRTUAL;
		break;
	case 6:
		op = INVOKESTATIC;
		break;
	case 7:
	case 8:
		op = INVOKESPECIAL;
		break;
	case 9:
		op = INVOKEINTERFACE;
		break;
	}
	return new MethodInsnNode(op, declaringClass.getName().replace('.', '/'), name,
			methodType.toMethodDescriptorString());
}
 
Example #8
Source File: MethodResolver.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public MethodHandleInfo virtualInfo(Class<?> target, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
    return lookup.revealDirect(virtualHandle(target, name, type));
}
 
Example #9
Source File: MethodResolver.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public MethodHandleInfo virtualInfo(Class<?> target, Method called) throws NoSuchMethodException, IllegalAccessException {
    return virtualInfo(target, called.getName(), Methods.methodType(called));
}
 
Example #10
Source File: DebugInfo.java    From es6draft with MIT License 4 votes vote down vote up
private MethodHandleInfo info() {
    if (info == null) {
        info = MethodHandles.publicLookup().revealDirect(handle);
    }
    return info;
}