Kevin J. Jones

Dynamic Dispatch in Java

Posted in Dynamic Languages, Java, invokedynamic by abahtowt on January 29, 2007

I thought it would be useful to create a way to indicate that a method dispatch should be performed dynamically in Java so it’s easy to test my experimental invokedynamic implementation. To do this I have added little bit of extra syntax to the Eclipse JDT Core Java compiler as follows,

int a;
String b;

// Dynamically invoke someMethod on this, expecting it to have an int
// return type and taking arguments of a int & String
someMethod`<int>(a,b) 

// Dynamically invoke someMethod on object aObject, expecting it to have a
//String return type and taking arguments of a int & String
aObject.someMethod`<String>(a,b);

The `<type> syntax is the indicator that you want to perform dynamic dispatch and are expecting a return type of type. The invokedynamic instruction also encodes the argument types but the compiler can infer these from the arguments actually supplied. The second form covers all Primary expressions, I have just given the most obvious example here.

I  have not supplied any syntax for making super or static dispatches as the semantics of these are undefined at the moment. The syntax also does not support the generics dispatch syntax but this is just because I wanted to avoid dealing with that yet. I suspect I will try an address these three issue in the next version. You will also note that I did not make a distinction between primitive and object types as some of the earlier work has done. I thought this distinction was pretty arbitrary from a programmers perspective so have assumed that run-time auto-boxing will be employed on primitives if its needed.

This syntax is really just to aid experimentation and not a genuine suggestion for how invokedynamic may be exposed in Java (assuming it is at all).

I am hoping to be able to release the patch that implements this in the next few days. 

Leave a Reply