Lookup.defineClass

Russell Gold russell.gold at oracle.com
Tue Mar 14 17:37:04 UTC 2017


Hi Alan,

I am trying this in SimpleStub, and it seems to work for my current test cases if I do this:
> MethodHandles.Lookup in = MethodHandles.privateLookupIn( baseClass, MethodHandles.lookup() ).dropLookupMode( MethodHandles.Lookup.PRIVATE );
> return in.defineClass(classBytes);

My tests create stubs from public, package-private and protected classes. Is that what you are expecting? It seems a bit overly complex. 

In gmbal, I see that this may not suffice, as the current interfaces provide no way to supply a class to use as a base. Figuring out how those interfaces are being used might change my answer.

- Russ

> On Mar 7, 2017, at 9:15 AM, Alan Bateman <alan.bateman at oracle.com> wrote:
> 
> One of the issues that strong encapsulation exposes is the number of libraries and frameworks that hack into the ClassLoader's defineClass methods to define proxy and other classes. We've been looking to get some of these use-cases onto a "supported footing" for some time. To that end, a promising proposal is to add a defineClass method to MethodHandles.Lookup so that a class can be defined to the same class loader and in the same runtime package as the Lookup's lookup class.
> 
> An initial implementation of Lookup.defineClass is in the current Jigsaw EA builds [1], the javadoc is also online [2]. We are currently looking for help in trying out this method to see how it might be used to replace existing hacks. The proposed method is not a drop-in replacement for the non-public ClassLoader.defineClass, it instead relies on being able to obtain a Lookup with PACKAGE access on a target class in the package. The Lookup is ideally obtained cooperatively but it could be obtained non-cooperatively too (using the new privateLookupIn and dropping PRIVATE access for example). Using a Lookup is likely a significant change for frameworks using bytecode libraries today and it might be that a number of libraries will need to be updated to try this out. If you do try it out then please reply here and let us know how you got on.
> 
> -Alan
> 
> [1] https://jdk9.java.net/jigsaw/
> [2] http://download.java.net/java/jigsaw/docs/api/java/lang/invoke/MethodHandles.Lookup.html#defineClass-byte:A-
> 



More information about the jigsaw-dev mailing list