comparison samples/Main.asm @ 1555:376a63a077ee

8144186: Update Main.asm sample to use dynalink public API Reviewed-by: hannesw, mhaupt
author sundar
date Fri, 27 Nov 2015 18:28:16 +0530
parents bfe6bd5d57bf
children
comparison
equal deleted inserted replaced
0:ab3ee72bc905 1:46dd6d43bf47
36 // 36 //
37 // java -cp <asmtools.jar> org.openjdk.asmtools.Main jasm Main.asm 37 // java -cp <asmtools.jar> org.openjdk.asmtools.Main jasm Main.asm
38 // 38 //
39 // See also: https://wiki.openjdk.java.net/display/CodeTools/asmtools 39 // See also: https://wiki.openjdk.java.net/display/CodeTools/asmtools
40 // 40 //
41 // NOTE: Uses nashorn internals and so *may* break with later nashorn! 41 // NOTE: Uses dynalink API defined by JEP-276 (http://openjdk.java.net/jeps/276)
42 42
43 super public class Main 43 super public class Main
44 version 52:0 44 version 52:0
45 { 45 {
46 46
47 private static final Field linker:"Ljdk/dynalink/DynamicLinker;";
47 48
49 /*
50 static {
51 // create dynalink linker factory
52 final DynamicLinkerFactory factory = new DynamicLinkerFactory();
53
54 // create standard linker that can handle Java POJOs
55 linker = factory.createLinker();
56 }
57 */
58 static Method "<clinit>":"()V"
59 stack 2 locals 1
60 {
61 new class jdk/dynalink/DynamicLinkerFactory;
62 dup;
63 invokespecial Method jdk/dynalink/DynamicLinkerFactory."<init>":"()V";
64 astore_0;
65 aload_0;
66 invokevirtual Method jdk/dynalink/DynamicLinkerFactory.createLinker:"()Ljdk/dynalink/DynamicLinker;";
67 putstatic Field linker:"Ljdk/dynalink/DynamicLinker;";
68 return;
69 }
70
71 /*
72 // Bootstrap method used with invokedynamic methods
73
74 public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) {
75 // use dynalink linker to perform the actual linking
76 return linker.link(
77 new SimpleRelinkableCallSite(
78 new CallSiteDescriptor(
79 MethodHandles.publicLookup(),
80 new NamedOperation(StandardOperation.GET_PROPERTY, name), type)
81 )
82 );
83 }
84
85 */
86 public static Method bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
87 stack 10 locals 3
88 {
89 getstatic Field linker:"Ljdk/dynalink/DynamicLinker;";
90 new class jdk/dynalink/support/SimpleRelinkableCallSite;
91 dup;
92 new class jdk/dynalink/CallSiteDescriptor;
93 dup;
94 invokestatic Method java/lang/invoke/MethodHandles.publicLookup:"()Ljava/lang/invoke/MethodHandles$Lookup;";
95 new class jdk/dynalink/NamedOperation;
96 dup;
97 getstatic Field jdk/dynalink/StandardOperation.GET_PROPERTY:"Ljdk/dynalink/StandardOperation;";
98 aload_1;
99 invokespecial Method jdk/dynalink/NamedOperation."<init>":"(Ljdk/dynalink/Operation;Ljava/lang/Object;)V";
100 aload_2;
101 invokespecial Method jdk/dynalink/CallSiteDescriptor."<init>":"(Ljava/lang/invoke/MethodHandles$Lookup;Ljdk/dynalink/Operation;Ljava/lang/invoke/MethodType;)V";
102 invokespecial Method jdk/dynalink/support/SimpleRelinkableCallSite."<init>":"(Ljdk/dynalink/CallSiteDescriptor;)V";
103 invokevirtual Method jdk/dynalink/DynamicLinker.link:"(Ljdk/dynalink/RelinkableCallSite;)Ljdk/dynalink/RelinkableCallSite;";
104 checkcast class java/lang/invoke/CallSite;
105 areturn;
106 }
107
108 // default constructor that does nothing!
48 public Method "<init>":"()V" 109 public Method "<init>":"()V"
49 stack 1 locals 1 110 stack 1 locals 1
50 { 111 {
51 aload_0; 112 aload_0;
52 invokespecial Method java/lang/Object."<init>":"()V"; 113 invokespecial Method java/lang/Object."<init>":"()V";
53 return; 114 return;
54 } 115 }
55 116
56 public static Method main:"([Ljava/lang/String;)V" 117 public static Method main:"([Ljava/lang/String;)V"
57 stack 2 locals 2 118 stack 2 locals 2
58 { 119 {
59 // List l = new ArrayList(); 120 // List l = new ArrayList();
60 new class java/util/ArrayList; 121 new class java/util/ArrayList;
61 dup; 122 dup;
62 invokespecial Method java/util/ArrayList."<init>":"()V"; 123 invokespecial Method java/util/ArrayList."<init>":"()V";
63 astore_1; 124 astore_1;
64 aload_1; 125 aload_1;
65 126
66 // l.add("hello"); 127 // l.add("hello");
67 ldc String "hello"; 128 ldc String "hello";
68 invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2; 129 invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2;
69 pop; 130 pop;
70 131
71 // l.add("world"); 132 // l.add("world");
72 aload_1; 133 aload_1;
73 ldc String "world"; 134 ldc String "world";
74 invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2; 135 invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2;
75 pop; 136 pop;
76 137
77 // printLength(l); 138 // printLength(l);
78 aload_1; 139 aload_1;
79 invokestatic Method printLength:"(Ljava/lang/Object;)V"; 140 invokestatic Method printLength:"(Ljava/lang/Object;)V";
80 141
81 // printLength(args); // args is argument of main method 142 // printLength(args); // args is argument of main method
82 aload_0; 143 aload_0;
83 invokestatic Method printLength:"(Ljava/lang/Object;)V"; 144 invokestatic Method printLength:"(Ljava/lang/Object;)V";
84 return; 145 return;
85 } 146 }
86 147
87 private static Method printLength:"(Ljava/lang/Object;)V" 148 private static Method printLength:"(Ljava/lang/Object;)V"
88 stack 2 locals 1 149 stack 2 locals 1
89 { 150 {
90 getstatic Field java/lang/System.out:"Ljava/io/PrintStream;"; 151 getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
91 aload_0; 152 aload_0;
92 153
93 // Using nashorn embedded dynalink linker with the following invokedynamic 154 // Using bootstrap method in this class with the following invokedynamic
94 // 'length' property on a bean - arrays, lists supported 155 // which uses dynalink linker. Dynalink's bean linker handles Java beans.
156 // 'length' property on a bean - arrays, lists supported
95 157
96 invokedynamic InvokeDynamic REF_invokeStatic:jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;":"dyn:getProp|getElem|getMethod:length":"(Ljava/lang/Object;)Ljava/lang/Object;" int 0; 158 invokedynamic InvokeDynamic REF_invokeStatic:Main.bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;":"length":"(Ljava/lang/Object;)Ljava/lang/Object;";
97 159
98 // print 'length' value 160 // print 'length' value
99 invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V"; 161 invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
100 return; 162 return;
101 } 163 }
102 164
103 } // end Class Main 165 } // end Class Main