Invoke dynamics


Published on

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Invoke dynamics

  1. 1. JSR 292 Invoke Dynamics By Balamurugan S
  2. 2. My Info <ul><li>4+ years of experience in Java </li></ul><ul><li>Currently working in logica </li></ul><ul><li>Worked as a trainer for two years </li></ul><ul><li>Completed SCJP, SCWCD and SCBCD certifications </li></ul><ul><li>Passion in learning new technologies and training people </li></ul><ul><li>Joined JUG Chennai few months back </li></ul>
  3. 3. Why JSR 292 ? <ul><li>Dynamically Typed Languages </li></ul><ul><li>Performance </li></ul><ul><li>Invokedynamic instruction </li></ul><ul><li>Hotswappable classes – Interface Injection </li></ul>
  4. 4. Dynamically Typed Languages and the JVM <ul><li>What is the JVM? </li></ul><ul><li>Statically Typed Languages vs. Dynamically Typed Languages </li></ul><ul><li>JSR 223 — A First Step in Dynamic Language Support </li></ul>
  5. 5. The Problem for Dynamically Typed Languages <ul><li>Satisfying the requirements of Java Byte code </li></ul><ul><li>Byte code supports only statically typed languages </li></ul><ul><li>Byte code requirements for method invocation </li></ul><ul><ul><li>Statically typed languages – Type checking is done in compile time </li></ul></ul><ul><li>Consider the following Java code snippet: </li></ul><ul><li>String s = &quot;Hello World&quot;; </li></ul><ul><li>System.out.println(s); </li></ul><ul><li>Bytecode instructions for the above code: </li></ul><ul><li>ldc #2 </li></ul><ul><li>astore_1 </li></ul><ul><li>getstatic #3 </li></ul><ul><li>aload_1 </li></ul><ul><li>invokevirtual #4 </li></ul>
  6. 6. JSR 292 — The Next Step in Dynamic Language Support <ul><li>Introduction of new java byte code instruction “invokedynamic” and new method linkage mechanism </li></ul><ul><li>Bytecode Instructions for Method Invocation: </li></ul><ul><li>Since its inception, the Java Virtual Machine Specification has specified four bytecodes for method invocation </li></ul><ul><ul><li>invokevirtual </li></ul></ul><ul><ul><li>invokeinterface </li></ul></ul><ul><ul><li>invokestatic </li></ul></ul><ul><ul><li>Invokespecial </li></ul></ul><ul><li>Let's examine invokevirtual and invokeinterface instruction, because it's similar in format to the new “invokedynamic” instruction. </li></ul>
  7. 7. The invokevirtual and invokeinterface Instruction <ul><li>The invokevirtual instruction: </li></ul><ul><li>invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V </li></ul><ul><li>Its one byte operation code </li></ul><ul><li>The remainder of the instruction, #4, is the two-byte operand </li></ul><ul><li>The receiver class that contains the method, the method name, and the method descriptor </li></ul><ul><li>Syntax: </li></ul><ul><li>invokevirtual <method-specification> </li></ul><ul><li>The invokeinterface Instruction: </li></ul><ul><li>invokeinterface #9, 2; //InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z </li></ul><ul><li>Syntax: </li></ul><ul><li>invokeinterface <method-specification> <n> </li></ul>
  8. 8. The invokedynamic Instruction <ul><li>Syntax: </li></ul><ul><li>invokedynamic <method-specification> <n> </li></ul><ul><li>An invokeinterface bytecode instruction might look similar to the following: </li></ul><ul><li>Invokedynamic #10; //NameAndTypelessThan:(Ljava/lang/Object;Ljava/lang/Object;) </li></ul><ul><li>Dynamic language to translate a method invocation into bytecode without having to specify a target type </li></ul><ul><li>The method specification in this case is a simplified constant pool reference </li></ul><ul><li>How does the JVM find the method if the receiver type isn't supplied? </li></ul><ul><li>The answer to that question is that JSR 292 </li></ul>
  9. 9. A New Dynamic Linkage Mechanism: Method Handles <ul><li>New linkage mechanism for dynamically typed languages called Method Handles </li></ul><ul><li>java.dyn package </li></ul><ul><li>Java.lang.invoke package </li></ul><ul><li>It is callable just like a named reference to a method </li></ul><ul><li>When invokedynamic instruction is executed, it is linked first </li></ul><ul><li>When linking Method Handle is assigned to individual invokedynamic instruction </li></ul>
  10. 10. <ul><li>BootStrap Method: </li></ul><ul><li>Another part of this new linkage mechanism is the bootstrap method </li></ul><ul><li>A bootstrap method is a method handle that is called once for each invokedynamic instruction, when the instruction is linked </li></ul><ul><li>Each class that contains at least one invokedynamic instruction must also specify a bootstrap method </li></ul><ul><li>The bootstrap method, in turn, creates a call site object and chooses an appropriate target method handle </li></ul><ul><li>The call site object provides an API to get and set the target method </li></ul>
  11. 11. <ul><li>Here, for example, is a snippet of bytecode that calls a method handle: </li></ul><ul><li>getfield myMH </li></ul><ul><li>ldc #999 </li></ul><ul><li>invokevirtual #44 //Method java/dyn/MethodHandle:invoke(I)I </li></ul><ul><li>istore 5 </li></ul><ul><li>When a bootstrap method is called for an unlinked dynamic call site, it is passed the following information: </li></ul><ul><li>The java.lang.Class object for the class containing the instruction. </li></ul><ul><li>The method name represented as a String. </li></ul><ul><li>The resolved descriptor for the instruction, represented as a java.dyn.MethodType token. </li></ul><ul><li>  </li></ul>
  12. 12. Sample code snippet import java.lang.invoke.*; import java.sql.*; public class Query { static { //Bootstrap method Linkage.registerBootstrapMethod(&quot;doBootstrap&quot;); } public static ResultSet query(String queryString, Object... params) { //Performs query, return ResultSet } public static CallSite doBootstrap(Class caller, String methodName, MethodType type) { MethodHandles.Lookup lookup = MethodHandles.lookup(); //Lookup the class that has the method that we will be linking to //In this case, query is in the same class Class me = lookup.lookupClass(); //Lookup query method by matching the parameter type and return type MethodHandle rawQuery = lookup.findStatic(me, &quot;query&quot; , MethodType.methodType(ResultSet.class, String.class, Object[].class)); //Set the method name as the first parameter to query() MethodHandle cookedQuery = MethodHandles.insertArguments(rawQuery, 0, methodName); //Return a CallSite object back to the JVM to continue with the call return (new ConstantCallSite(MethodHandles.collectArguments(cookedQuery, type))); }
  13. 13. public static void main(String... args) throws Throwable { //Invoking this will result in doBootstrap() method being called ResultSet result = InvokeDynamic.<ResultSet>findByTitleAndAuthor (&quot;The Shining&quot;, &quot;Stephen King&quot;); String val = InvokeDynamic.<String>findByTitleAndAuthor (&quot;Lord of the Rings&quot;, &quot;JRR Tolkein&quot;); } } //java -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic Query
  14. 14. Future Enhancements <ul><li>Interface injection: </li></ul><ul><li>Modify classes at runtime so that they can implement new interfaces </li></ul><ul><li>Can provide a structured way to accomplish this in the JVM </li></ul>
  15. 15. Summary <ul><li>JVM has been host to a growing number of languages </li></ul><ul><li>JVM is very attractive to application developers </li></ul><ul><li>JVM delivers a lot of execution efficiency </li></ul><ul><li>Implementers of compilers for dynamically typed languages have found it difficult to meet the JVM bytecode requirements for method invocation. JSR 292 addresses that problem by providing a new bytecode, invokedynamic, and a new linkage mechanism based on method handles </li></ul><ul><li>Investigation for inclusion in JSR 292 is interface injection </li></ul>
  16. 16. Thank ‘U’ email:
  17. 17. Queries?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.