Lookup.defineClass

Alan Bateman Alan.Bateman at oracle.com
Tue Mar 7 14:15:15 UTC 2017


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