setAccessible() alternative with Jigsaw - feedback on Lookup

forax at univ-mlv.fr forax at univ-mlv.fr
Wed Apr 26 15:31:47 UTC 2017


----- Mail original -----
> De: "Matej Novotny" <manovotn at redhat.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Alan Bateman" <Alan.Bateman at oracle.com>, jigsaw-dev at openjdk.java.net, "Tomas Remes" <tremes at redhat.com>, "Martin
> Kouba" <mkouba at redhat.com>
> 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

Rémi

[1] http://download.java.net/java/jdk9/docs/api/java/lang/module/ModuleDescriptor.Builder.html
[2] http://download.java.net/java/jdk9/docs/api/java/lang/module/ModuleFinder.html
[3] http://download.java.net/java/jdk9/docs/api/java/lang/module/Configuration.html#resolve-java.lang.module.ModuleFinder-java.lang.module.ModuleFinder-java.util.Collection-
[4] http://download.java.net/java/jdk9/docs/api/java/lang/ModuleLayer.html#defineModulesWithOneLoader-java.lang.module.Configuration-java.lang.ClassLoader-


More information about the jigsaw-dev mailing list