Hi,

just coming to the example you brought, because I exercised this a week ago:

> [...] For example... can you
> call Object#clone on Object using MethodHandles? I actually did not try
> this, but since the method is protected and in a module I would assume
> no. You had been able to do so by reflection. Let us not discuss about
> if that makes sense or not. It just shows for me that (ii) is simply not
> there.

Hi, it is impossible to call Object#clone with MethodHandles. Not even in the 
same module unless you unreflect an accessible reflective method. I tried 
something similar last week to fix the Cglib issues with invoking protected 
ClassLoader#defineClass. The idea was "the standard MethodHandle" trick:

Create an abstract subclass of ClassLoader - just to have a static 
initialization block. In this static block you get a (private) Lookup object to 
lookup the protected defineClass method. Which works phantastically! As it is 
virtual, the MethodHandle should now allow to call the method with any 
ClassLoader as first parameter (the receiver of virtual method). This works for 
all methods, but not protected ones. If you read the Javadocs it is in the 
specs. For protected methods, the methodhandle is "modified" and the receiver 
argument is casted to the Lookup's class. And because of this you get a 
ClassCastException if you don't call with a receiver that is assignable to the 
current Lookup's class.

Too stupid, the designers of MethodHandles were way too intelligent :-)

See my investigations:
https://github.com/cglib/cglib/issues/93#issuecomment-269255311

Uwe

Reply via email to