Few questions about invokeDynamic

assembling signals assembling.signals at yandex.ru
Sat Nov 6 09:36:54 PDT 2010


Remi, thank you a lot for such a quick reply!

I tried to do what you suggested, but it keeps failing.

Note, that I don't use some customized build, but instead, as also mentionen in previous mail, JDK7 b116.
Could you please try the code by yourself? It's a single class.

Thanks again!



= = = = = = = = = =
CODE
= = = = = = = = = =

import java.dyn.*;

public final class _InvokeDynamic {

	private final static MethodHandle methodHandle;

	public static int methodA() {
		System.out.println("!@#! MAGIC !@#!");
		return 0;
	}

	static {
		Linkage.registerBootstrapMethod("bootstrap");
		methodHandle = MethodHandles.lookup().findStatic(_InvokeDynamic.class,
				"methodA", MethodType.methodType(int.class));
	}

	private static CallSite bootstrap(Class<?> declaring, String name, MethodType type) {
		System.out.println("declaring class: " + declaring + ", name: " + name
				+ ", method type:" + type);
		CallSite cs = new CallSite();
		cs.setTarget(methodHandle);
		return cs;
	}

	public static void main(String[] ars)
			throws Throwable {
		try {
			System.out.println("\n\nATTEMPT 1");
			InvokeDynamic.<int>anything();
		} catch (Throwable t) {
			t.printStackTrace(System.out);
		}

		try {
			System.out.println("\n\nATTEMPT 2");
			methodHandle.invokeExact(); // warning: unchecked generic array creation
		} catch (Throwable t) {
			t.printStackTrace(System.out);
		}

		try {
			System.out.println("\n\nATTEMPT 3");
			(int) methodHandle.invokeExact(); // error: not a statement
		} catch (Throwable t) {
			t.printStackTrace(System.out);
		}
	}

}

= = = = = = = = = =
OUTPUT
= = = = = = = = = =

ATTEMPT 1
declaring class: class _InvokeDynamic, name: anything, method type:()int
!@#! MAGIC !@#!


ATTEMPT 2
java.dyn.WrongMethodTypeException: ()I cannot be called as ([Ljava/lang/Object;)Ljava/lang/Object;
        at _InvokeDynamic.main(_InvokeDynamic.java:40)


ATTEMPT 3
java.lang.RuntimeException: Uncompilable source code - not a statement
        at _InvokeDynamic.main(_InvokeDynamic.java:47)
BUILD SUCCESSFUL (total time: 0 seconds)


More information about the mlvm-dev mailing list