Re: Lookup.defineClass

2017-04-03 Thread Alan Bateman
On 03/04/2017 15:15, Russell Gold wrote: Upon further testing, this turns out to be less capable than the “Unsafe” version - in particular, I cannot create a test stub in a closed package. The problem is that unit tests often need to do a number of things like this that make no sense in a prod

Re: Lookup.defineClass

2017-04-03 Thread Russell Gold
Upon further testing, this turns out to be less capable than the “Unsafe” version - in particular, I cannot create a test stub in a closed package. The problem is that unit tests often need to do a number of things like this that make no sense in a production environment. The problem has to do

Re: Lookup.defineClass

2017-03-14 Thread Remi Forax
yes :) Remi On March 8, 2017 9:41:13 PM GMT+01:00, John Rose wrote: >On Mar 8, 2017, at 10:47 AM, Remi Forax wrote: >> >> The exception if the lookup is PRIVATE should be >IllegalStateException and not UnuspportedOperationException. >> then, given that the loaded class will have the same prot

Re: Lookup.defineClass

2017-03-14 Thread Alan Bateman
On 14/03/2017 17:37, Russell Gold wrote: 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

Re: Lookup.defineClass

2017-03-14 Thread Russell Gold
time 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 existi

Re: Experiences with Lookup.defineClass

2017-03-13 Thread Alan Bateman
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 b

Re: Experiences with Lookup.defineClass

2017-03-11 Thread John Rose
On Mar 11, 2017, at 9:55 AM, Michael Rasmussen wrote: > > What should the Lookup be based on though, since you don't have a Class yet? Lookups are designed to represent computations in the context of a pre-existing class. I guess a Lookup for this use case would have to be based on a pre-exist

Re: Experiences with Lookup.defineClass

2017-03-11 Thread Rafael Winterhalter
You are right, that is a problem. However, I do not think that the lookup would need to be based on the class as long as it has the same priviledges. In this case, the lookupClass method could return null or throw an unsupported operation exception if the class is yet unloaded. I do not think that

Re: Experiences with Lookup.defineClass

2017-03-11 Thread Michael Rasmussen
> For this to work, I do however think it is crucial to add a parameter to > the ClassFileTransformer::transform method where a lookup object should be > provided as an additional argument in order to allow defining additional > classes in the instrumented class's loader and module. What should th

Experiences with Lookup.defineClass

2017-03-11 Thread Rafael Winterhalter
I have implemented support for Lookup::defineClass in my library Byte Buddy and my first impression is quite positive, I think this can offer a great alternative for at least some use cases. For this to work, I do however think it is crucial to add a parameter to the ClassFileTransformer::transfor

Re: Lookup.defineClass

2017-03-08 Thread John Rose
On Mar 8, 2017, at 10:47 AM, Remi Forax wrote: > > 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

Re: Lookup.defineClass

2017-03-08 Thread Remi Forax
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 su

Lookup.defineClass

2017-03-07 Thread Alan Bateman
ime. 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