Invoke dynamics
Upcoming SlideShare
Loading in...5

Invoke dynamics






Total Views
Slideshare-icon Views on SlideShare
Embed Views



2 Embeds 721 694
url_unknown 27



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Invoke dynamics Invoke dynamics Presentation Transcript

    • JSR 292 Invoke Dynamics By Balamurugan S
    • My Info
      • 4+ years of experience in Java
      • Currently working in logica
      • Worked as a trainer for two years
      • Completed SCJP, SCWCD and SCBCD certifications
      • Passion in learning new technologies and training people
      • Joined JUG Chennai few months back
    • Why JSR 292 ?
      • Dynamically Typed Languages
      • Performance
      • Invokedynamic instruction
      • Hotswappable classes – Interface Injection
    • Dynamically Typed Languages and the JVM
      • What is the JVM?
      • Statically Typed Languages vs. Dynamically Typed Languages
      • JSR 223 — A First Step in Dynamic Language Support
    • The Problem for Dynamically Typed Languages
      • Satisfying the requirements of Java Byte code
      • Byte code supports only statically typed languages
      • Byte code requirements for method invocation
        • Statically typed languages – Type checking is done in compile time
      • Consider the following Java code snippet:
      • String s = "Hello World";
      • System.out.println(s);
      • Bytecode instructions for the above code:
      • ldc #2
      • astore_1
      • getstatic #3
      • aload_1
      • invokevirtual #4
    • JSR 292 — The Next Step in Dynamic Language Support
      • Introduction of new java byte code instruction “invokedynamic” and new method linkage mechanism
      • Bytecode Instructions for Method Invocation:
      • Since its inception, the Java Virtual Machine Specification has specified four bytecodes for method invocation
        • invokevirtual
        • invokeinterface
        • invokestatic
        • Invokespecial
      • Let's examine invokevirtual and invokeinterface instruction, because it's similar in format to the new “invokedynamic” instruction.
    • The invokevirtual and invokeinterface Instruction
      • The invokevirtual instruction:
      • invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
      • Its one byte operation code
      • The remainder of the instruction, #4, is the two-byte operand
      • The receiver class that contains the method, the method name, and the method descriptor
      • Syntax:
      • invokevirtual <method-specification>
      • The invokeinterface Instruction:
      • invokeinterface #9, 2; //InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      • Syntax:
      • invokeinterface <method-specification> <n>
    • The invokedynamic Instruction
      • Syntax:
      • invokedynamic <method-specification> <n>
      • An invokeinterface bytecode instruction might look similar to the following:
      • Invokedynamic #10; //NameAndTypelessThan:(Ljava/lang/Object;Ljava/lang/Object;)
      • Dynamic language to translate a method invocation into bytecode without having to specify a target type
      • The method specification in this case is a simplified constant pool reference
      • How does the JVM find the method if the receiver type isn't supplied?
      • The answer to that question is that JSR 292
    • A New Dynamic Linkage Mechanism: Method Handles
      • New linkage mechanism for dynamically typed languages called Method Handles
      • java.dyn package
      • Java.lang.invoke package
      • It is callable just like a named reference to a method
      • When invokedynamic instruction is executed, it is linked first
      • When linking Method Handle is assigned to individual invokedynamic instruction
      • BootStrap Method:
      • Another part of this new linkage mechanism is the bootstrap method
      • A bootstrap method is a method handle that is called once for each invokedynamic instruction, when the instruction is linked
      • Each class that contains at least one invokedynamic instruction must also specify a bootstrap method
      • The bootstrap method, in turn, creates a call site object and chooses an appropriate target method handle
      • The call site object provides an API to get and set the target method
      • Here, for example, is a snippet of bytecode that calls a method handle:
      • getfield myMH
      • ldc #999
      • invokevirtual #44 //Method java/dyn/MethodHandle:invoke(I)I
      • istore 5
      • When a bootstrap method is called for an unlinked dynamic call site, it is passed the following information:
      • The java.lang.Class object for the class containing the instruction.
      • The method name represented as a String.
      • The resolved descriptor for the instruction, represented as a java.dyn.MethodType token.
    • 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))); }
    • 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
    • Future Enhancements
      • Interface injection:
      • Modify classes at runtime so that they can implement new interfaces
      • Can provide a structured way to accomplish this in the JVM
    • Summary
      • JVM has been host to a growing number of languages
      • JVM is very attractive to application developers
      • JVM delivers a lot of execution efficiency
      • 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
      • Investigation for inclusion in JSR 292 is interface injection
    • Thank ‘U’ email:
    • Queries?