jooby-project / jooby

The modular web framework for Java and Kotlin
https://jooby.io
Apache License 2.0
1.7k stars 199 forks source link

ReflectiveBeanConverter should take MethodHandles.Lookup #3498

Open agentgt opened 1 month ago

agentgt commented 1 month ago

Given the discussions on #3496 (and corresponding PR #3494) as well as the new @BindParam I think the reflective bean converter needs some cleanup and possibly should be available as public API.

Some issues:

The last point is important in a modular world. There are two choices in allow reflective access if your application is modular. You either

  1. open everything up (as in put open module declaration or package)
  2. or you pass a MethodHandles.Lookup to the library (jooby) doing the reflection.

Thus we should allow one want to do something like (the following may not be correct but roughly):

Jooby app = ...;
var lookup = MethodHandles.lookup(); // the consumer of jooby calls this
app.converter(new ReflectiveBeanConverter(lookup)); // or however we replace the default one.

On a separate note Handlebars.java should do something similar for version 5. In fact you could do

var lookup = MethodHandles.lookup(); // the consumer of jooby calls this
app.registerLookup(lookup); // register lookup to be used by jooby modules.
// jooby handlebars module then does 
var lookup app.getLookup();
Handlebars handlebars = new Handlebars(lookup); // or whatever is analogous

I can try to take a stab at a PR if you like. A bonus to using MethodHandles is that they are supposedly can be faster than regular reflection.

jknack commented 1 month ago

Make sense, send a PR we can work from there.