setAccessible() alternative with Jigsaw - feedback on Lookup

forax at forax at
Wed Apr 26 15:31:47 UTC 2017

----- Mail original -----
> De: "Matej Novotny" <manovotn at>
> À: "Remi Forax" <forax at>
> Cc: "Alan Bateman" <Alan.Bateman at>, jigsaw-dev at, "Tomas Remes" <tremes at>, "Martin
> Kouba" <mkouba at>
> Envoyé: Mercredi 26 Avril 2017 16:04:08
> Objet: Re: setAccessible() alternative with Jigsaw - feedback on Lookup

>> You can spin one module per package, by creating one ModuleLayer per package,
>> not unlike j.l.r.Proxy does.
> Don't know a bit about it, but it sounds like a way worth exploring.
> Would you have a pointer towards some code showing how to deal with
> ModuleLayers?
> Also checking on j.l.r.Proxy, it uses the "good old" Unsafe.defineClass.
> Can't really see any module-related magic there.

snap, sorry, the code has changed since the last time i looked at it, or it was another code ...

the recipe is:
  1. create a module with ModuleDescriptor.Builder [1]
     add the package you need with packages()
  2. create an implementation of ModuleFinder [2] that only find that module
  3. create a Configuration [3] like that resolve your module
     ModuleLayer boot = ModuleLayer.boot();
     Configuration cf = boot.configuration().resolve(finder, ModuleFinder.of(), moduleName);
  4. create a classloader 
     ClassLoader classLoader = new ClassLoader(parentClassLoader()) { /* make defineClass more visible */};
  5. create your own layer [4]
     ModuleLayer layer = boot.defineModulesWithOneLoader(cf, classloader;
  6. profit (i.e. now you can call define class on your classloader)

> Matej



More information about the jigsaw-dev mailing list