Experiences with Lookup.defineClass

Alan Bateman Alan.Bateman at oracle.com
Mon Mar 13 14:10:58 UTC 2017


On 11/03/2017 16:48, Rafael Winterhalter wrote:

> :
>
> For testing libraries like Mockito, I would however prefer to use a
> mechanism similar to Unsafe::defineClass. My prefered way would be to add a
> test-specific module to any JDK installation that needs to be enabled by
> some flag. It would be too bad if people had to open their modules just for
> some test code to run where defining a class in a tested class's class
> loader (for example because it is package-private) is a legitimate demand.
> Requiring users to explicitly supply a lookup instance would yield an
> awkward API where I would prefer a parameter
There may be an argument that java agents should have a way to define 
new classes.

However, I do think we need to explore further the cases where there 
isn't a java agent in the picture. If some framework library wants to 
create proxy classes or other code in the same package (and maybe 
eventually the nest) as classes that have its annotations then having 
the application code provide the Lookup with the appropriate access is a 
good way to go. This would be of course require changes further up the 
stack and provide the right initialization points so that the capability 
can be provided.

-Alan


More information about the jigsaw-dev mailing list