SCROLL icon indicating copy to clipboard operation
SCROLL copied to clipboard

Replace reflective access of role-specific behavior / structure with MethodHandles

Open max-leuthaeuser opened this issue 7 years ago • 2 comments

Replace all reflective access of role-specific behavior / structure in ReflectiveHelper.scala with MethodHandels.

Hence, we should do:

  • [ ] replace caches using java.lang.reflect.{Field, Method} with ones storing MethodHandles

  • [ ] use MethodHandle findVirtual(Class<?> refc, String name, MethodType type), MethodHandle findGetter(Class<?> refc, String name, Class<?> type), and MethodHandle findSetter(Class<?> refc, String name, Class<?> type) instead of getDeclaredMethods, getDeclaredFields, getAccessibleMethods, and getAccessibleFields

  • [ ] remove unnecessary code, e.g., def matchMethod[A](m: Method, name: String, args: Seq[A]): Boolean or def hasMember(on: AnyRef, name: String): Boolean since this could be handled by the MethodHandles API directly.

  • [ ] finally, call role-specific functionally with MethodHandle.invoke(Object... args)

max-leuthaeuser avatar Feb 16 '18 15:02 max-leuthaeuser

use MethodHandle findVirtual(Class> refc, String name, MethodType type), MethodHandle findGetter(Class> refc, String name, Class> type), and MethodHandle findSetter(Class> refc, String name, Class<?> type) instead of getDeclaredMethods, getDeclaredFields, getAccessibleMethods, and getAccessibleFields

If someone is going to do this replacement, I wanted to note that findGetter() and findSetter() are most probably the wrong methods in the case of Scala. As I understand it, scalac creates accessor methods based on the property name (<propname>() getter and <propname>_$eq() setter) and one should use Lookup.findVirtual() to find these accessor methods.

martinmo avatar May 13 '18 18:05 martinmo

Good hint. Thanks!

max-leuthaeuser avatar May 13 '18 18:05 max-leuthaeuser