Interfaces are injected at 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 toClass#getInterfaces
, although I'm sure someone smart will be able to solve this without having to know about all injectable interfaces beforehand).
What can this be used for?
As a language implementer on the Java platform I think interface injection would be a blessing. In fact I think it is the one thing that would simplify the implementation of languages on the JVM the most. Any language probably has a base interface (as Java has
java.lang.Object
and Jython has org.python.core.PyObject
), let's be unbiased and call it "MyLangObject
" for the sake of the continued discussion. There are two things that make the Java platform great:- There are a lot of really good toolkits an libraries implemented for the Java platform.
- There are a lot of great languages for the Java platform in which even more great libraries and toolkits will be developed.
MyLangObject
was an injectable interface all you would need to do to be able to integrate with any object from another language would be to just interact with it through the MyLangObject
interface, and the injection mechanism would take care of the rest.The injection mechanism could even be used with the classes within your language. Instead of having a base class supplying the default implementation of the methods of
MyLangObject
you could let the injection method return the default implementation for your methods.Or why not use interface injection to support function invocation with different argument counts. Each function in your language would implement a set of
call
methods, one for each argument count it can be invoked with. Your language would then have a set of injectable Callable
interfaces one for each argument count that any function in your language can be invoked with, each with only one call
method, with the appropriate number of arguments. These interfaces could be generated at runtime if your language supports runtime code generation. The default implementation of the call method in each Callable
interface would of course raise an exception, since the function obviously doesn't support that argument count if it doesn't implement the appropriate method.Interface injection really does provide a huge set of opportunities.
How could interface injection implement a Meta Object Protocol?
There is a great project initialized by Attila Szegedi of the JVM-languages comminuty to create a common meta object protocol (MOP) for all languages on the JVM. With interface injection this would be a simple task.
- Let all objects in your language (that supports the MOP) implement the (probably not injectable) interface
java.dyn.SupportsMetaObjectProtocol
. An interface with only one method:java.dyn.MetaObjectProtocol getMetaObjectProtocol();
This would return the implementation of thejava.dyn.MetaObjectProtocol
interface for your particular language. - The
java.dyn.MetaObjectProtocol
contains methods for getting method handles for each dynamic language construct that the community have agreed to be a good common construct, such as getters and setters for subscript operations. These method handles would come from the actual implementation of them for your particular language, and would therefore benefit from every imaginable optimization you have cooked up for your language. - When the main interface of my language is being injected into a class from your language it finds that your class implements
java.dyn.SupportsMetaObjectProtocol
and uses that to get the method handles for all dynamic language constructs supported by my language, rebinding them them to the method names used in my language.
To sum things up: I can't wait until the JVM supports interface injection.
Edit: this post has also been re-posted on Javalobby.
2 comments:
No way should this be done at runtime. Then you end up in the monkey-patching ghetto with all the other "meta-programmable" languages.
Instead, you want to add something like C#'s extension methods, then on top of that allow post-hoc declarations that a class implements an interface (once it has the right native and extension methods available to meet the specification of that interface).
Greg: it sounds like the system you suggested could be made to do many of the things I want to see from interface injection.
The main difference with extension methods is of course that they are linked to a specific target class.
The extension methods would then probably be applied to their respective target classes at class load time (when loading the class that defines them), which would make it possible to generate the them at run time, which would suite the needs of the emerging set of languages on the Java platform.
So I cannot say that I see much difference in the two suggestions, except that the approach of using method handles for interface injection might be a bit easier to automatically adapt to unforeseen classes.
Post a Comment