Created
July 30, 2010 16:19
-
-
Save stepheneb/500808 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[davinci]$ cat Hello.java | |
import java.dyn.*; | |
public class Hello { | |
public static void main(String... av) throws Throwable { | |
if (av.length == 0) av = new String[] { "world" }; | |
greeter(av[0] + " (from a statically linked call site)"); | |
for (String whom : av) { | |
greeter.<void>invokeExact(whom); // strongly typed direct call | |
// previous line generates invokevirtual MethodHandle.invokeExact(String)void | |
Object x = whom; | |
InvokeDynamic.hail(x); // weakly typed invokedynamic | |
// previous line generates invokedynamic hail(Object)Object | |
} | |
} | |
static void greeter(String x) { System.out.println("Hello, "+x); } | |
// intentionally pun between the method and its reified handle: | |
static MethodHandle greeter | |
= MethodHandles.lookup().findStatic(Hello.class, "greeter", | |
MethodType.methodType(void.class, String.class)); | |
// Set up a class-local bootstrap method. | |
static { Linkage.registerBootstrapMethod("bootstrapDynamic"); } | |
private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) { | |
assert(type.parameterCount() == 1 && name.equals("hail")); // in lieu of MOP | |
System.out.println("set target to adapt "+greeter); | |
return new CallSite(greeter.asType(type)); | |
} | |
} | |
// String x = InvokeDynamic.<@BootstrapMethod(MyCallSite.class) String>greet(); | |
// InvokeDynamic.<@BootstrapMethod(value=MyLinker.class, name="myBootstrap") void>greet(); | |
[davinci]$ | |
[davinci]$ java -version | |
openjdk version "1.7.0-internal-fastdebug" | |
OpenJDK Runtime Environment (build 1.7.0-internal-fastdebug-stephen_2010_07_30_11_56-b00) | |
OpenJDK Server VM (build 19.0-b03-fastdebug, mixed mode) | |
[davinci]$ | |
[davinci]$ javac Hello.java | |
Hello.java:11: warning: InvokeDynamic calls must be in scope of a @BootstrapMethod annotation (registerBootstrapMethod is deprecated) | |
InvokeDynamic.hail(x); // weakly typed invokedynamic | |
^ | |
Note: Hello.java uses or overrides a deprecated API. | |
Note: Recompile with -Xlint:deprecation for details. | |
1 warning | |
[davinci]$ | |
[davinci]$ java Hello | |
Exception in thread "main" java.lang.ClassFormatError: invokedynamic instructions not enabled in this JVM in method Hello.main([Ljava/lang/String;)V | |
at java.lang.Class.getDeclaredMethods0(Native Method) | |
at java.lang.Class.privateGetDeclaredMethods(Class.java:2440) | |
at java.lang.Class.getMethod0(Class.java:2683) | |
at java.lang.Class.getMethod(Class.java:1618) | |
at sun.launcher.LauncherHelper.signatureDiagnostic(LauncherHelper.java:210) | |
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:202) |
rose00
commented
Jul 31, 2010
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment