Java Code Examples for java.lang.invoke.MethodHandleInfo#getMethodType()

The following examples show how to use java.lang.invoke.MethodHandleInfo#getMethodType() . 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: 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());
}