Howdy all. I have a question. I have MVC controlle...
# reflect
c
Howdy all. I have a question. I have MVC controller classes and action methods (ehh functions) on those classes. The framework is in Java, but all our code is Kotlin. We looooooove KFunction and KClass as that allowed us to move away from string references to methods and classes as prescribed by the Java framework, giving us really cool type safety and refactorability. Anyway, the authorization is implemented with annotations on the controller classes and their methods. For example
@RequiresAnyUserPermissionOf(...)
that takes one or more permission enum instances. We check on each incoming request that that user user is allowed to the action by using reflection to get the annotation of the method and the class and all superclasses (up to some point). I consider caching all this by making some maps like
Map<KFunction<Result>, List<Permission>>
and
Map<KClass<Controller>, List<Permission>>
so it is all cached and we do not have to (recursively) reflect all over the place to now what permissions are on a certain controller class or controller action method. I have no clue what a KClass or KFunction actually is! Would this be a good idea? Or should I make these maps based on string representations (and the convert the KFunction or KClass to a string when doing a lookup)? Or doing a bunch of these reflections class to find the annotations soooooo fast that I should not bother at all... Just curious what you have to say on this, and as to how a KFunction or KClass actually is represented in memory.
I think I found most of my answers in here: https://github.com/JetBrains/kotlin/blob/master/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KFunctionImpl.kt#L225 The
hashCode
and
equals
show me enough of the internals to know that this approach of caching in a HashMap based on
KFunction
keys should work