Hey hey, I've been checking Kotlin for a while and so far, I like it pretty much, although I didn't experiment thouroughly with it. Anyway, one of the things I really, really miss in Java, is some kind of 'intercept a method call'. There is a way, like using proxies, but these tend to pollute your code, and JVM optimization is pretty hard, inlining is not always possible (due to big) and you get huge stack traces with all the proxy calls. There is an invokedynamic-instruction on the JVM (and callable in Java7 and later), which can be used to overcome the proxy-issues. What I wonder is: does Kotlin provide (or are there plans to provide) any kind of 'interceptable' functions, meaning, if a method is defined as interceptable, is it possible to add some behaviour before or after the method is called (without using reflection etc, which causes overhead and so on), like http://niklasschlimm.blogspot.be/2012/02/java-7-complete-invokedynamic-example.html? I remember I saw Remi Forax on Devoxx Belgium last year, and he showed some Java-code intercepting calls using the 'invokedynamic'-instruction in the compiled code. It looked more or less like this (although I can't remember fully):
public class SomeService { @SomeAnnotation private interceptable Field field;
}
…
HashMap<Class<?>, Object> aspects = …
aspects.put(Field.class, new FieldImplementation());
Bootstrap.addAdvice(new Advice(AnnotatedElement ae, MethodHandle mh) {
if (!ae.isAnnotationPresent(SomeAnnotation.class)) { return mh; }
Class<?> type = mh.type().returnType();
return MethodHandles.dropArguments(
MethodHandles.constant(type, aspects.get(type)), 0, mh.type().parameterType(0)));
})
…
or even something like - I certainly remember the ‘public’-keyword on a field :
public class PoJo {
public interceptable String field
}
…
Bootstrap.addAdvice(new Advice(AnnotatedElement ae, MethodHandle mh) {
if (ae instanceof String) {
Methodhandle getter;
if (mh.type().parameterCount() == 0 && getter=getGetter(Field(ae)) != null) {
return getter;
}
}
return mh;
})
…
Well, something similar to the latter is actually already implemented in some way in Kotlin (properties instead of fields+getters/setters).
Anyway, the ‘interceptable’-keyword is non-exisiting in Java, but would be necessary to have some kind of intercept not using proxies or reflection-thingy. It just means: ‘you can intercept it (using the invokedynamic-instruction, although I don’t really care which instruction is used, as long at it’s not proxies and/or reflection)’ and than you need some kind of system to say how to intercept it (in this case, using annotations, but there may be another, better syntax in a new language). It would be great solution for things like security, logging and many more, and maybe there might be something to do with it concerning (checked?) exceptions - I don’t like exception handling polluting my classes, I’d rather have it separate somewhere, but neither do I like ‘not being forced to think about some important exceptions that may break the application’, but that’s another discussion.
Or is there already a (simple) way in Kotlin to mimic this kind of behaviour?
Kurt