MethodHandles.Lookup.defineResource?

Peter Levart peter.levart at gmail.com
Tue Aug 28 19:43:48 UTC 2018



On 08/28/2018 06:38 PM, Stephen Colebourne wrote:
> On 28 August 2018 at 08:17, Alan Bateman <Alan.Bateman at oracle.com> wrote:
>> On 28/08/2018 07:55, Peter Levart wrote:
>> Right, and any class injected with Lookup.defineClass can be located with
>> methods such as Class.forName or Lookup.findClass. There is no equivalent
>> for resources, at least not without extending the ClassLoader API in ways
>> that would work with existing implementations.
> OK, so maybe we've established what could be done - a new method on
> ClassLoader that would initially not work on implementations outside
> the JDK.
>
> So, there is a functionality gap here, but I'm guessing no particular
> desire to make a change.
>
> Stephen

Stephen,

Do you think this functionality is really needed in programs? It seems 
useful just for testing. What you're asking is to make a kind of 
resource repository from ClassLoader. The next thing after 
defineResource() would be removeResource() or hideResource(), etc...

defineClass() doesn't really fall into that category. It doesn't expose 
the .class file resource. defineClass() is there for an entirely 
different purpose. Classes hold code which executes in the confines of 
the JVM security model governing access to class members, so there's no 
way to define a class that would have access to members of encapsulated 
classes of some module without defining it in the same module, which 
means that it has to be defined by the same class loader as encapsulated 
classes it wants to access.

Testing loading and interpretation of resources OTOH can be performed by 
staging a separate ClassLoader and handing it to tested code.

So is there a way to achieve what you want for your test with existing 
API? What does your tested method accept as input?

Regards, Peter



More information about the core-libs-dev mailing list