Tobias has posted 1 posts at DZone. View Full User Profile

My JVM Wishlist (Part 1): Interface injection

07.16.2008
| 13659 views |
  • submit to reddit
If you've been following what's going on in the JVM-languages community, you have probably stumbled across John Rose's blog. One of his entries was about interface injection. In short, interface injection is the ability to at runtime add an interface to a class that was not precompiled as implementing that interface.

Interfaces are injected in one of 3 situations:


  • When a method of the interface is invoked on an object.

  • When an instanceof check for the interface is performed on an object.

  • When the interface is queried for via reflection (I can see this working with Class#isAssignableFrom, but I have my doubts when it comes to Class#getInterfaces, although I'm sure someone smart will be able to solve this without having to know about all injectable interfaces beforehand).

When any of these occur, a class can either have the interface implemented already (in the regular way) or a special static injection method on the interface class is invoked. It is up to this injection method to determine if the given class can implement the interface or not. If it determines that the class can implement the interface any missing methods have to be supplied at that time. John suggests that these methods are to be supplied as method handles.

Since method handles, according to the EDR of the InvokeDynamic proposal (JSR 292), can be curried this would make it possible to attach an extra state object to the returned method handle, or to return a different implementation of the interface depending on the class they are injected to. Once an injection method of a specific interfaced has been invoked for a specific class, the injection method will never be invoked for that class again, meaning that once a class has been found to not implement an interface, that information will be final, and once an implementation of an interface has been supplied, this implementation can never be changed. This is important since it will allow the VM to perform all optimizations, such as inlining, as before.

Published at DZone with permission of its author, Tobias Ivarsson.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)