Lookup.defineClass

Remi Forax forax at univ-mlv.fr
Wed Mar 8 10:47:49 UTC 2017


Hi Alan,
i've already updated the only code i had that was using ClassLoader.defineClass after a Method.setAccessible() to use Unsafe.defineClass
(no way to remove it, i need to be able to inject code a la Mockito)
so Lookup.defineClass is a safe replacement of Unsafe.defineClass , i suppose that Unsafe.defineClass should be deprecated and will be removed in 10 ?

The exception if the lookup is PRIVATE should be IllegalStateException and not UnuspportedOperationException.
then, given that the loaded class will have the same protection domain as the lookup class, i do not fully understand why having a PRIVATE lookup is a problem.

Rémi

----- Mail original -----
> De: "Alan Bateman" <Alan.Bateman at oracle.com>
> À: "jigsaw-dev" <jigsaw-dev at openjdk.java.net>
> Envoyé: Mardi 7 Mars 2017 15:15:15
> Objet: Lookup.defineClass

> 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