Locating a class file from a class loader by name

Alan Bateman Alan.Bateman at oracle.com
Fri Nov 13 18:28:33 UTC 2015


On 13/11/2015 17:48, Rafael Winterhalter wrote:
> Hi Alan,
> thanks for your quick answer. I am less worried about the generated 
> code then I am about the generation process.
>
> For example, a ClassFileTransformer might want to instrument all 
> classes that implement a certain interface. For this, the transformer 
> needs to parse the provided class file, extract the interfaces and 
> locate their class files and so forth in order to resolve the type 
> hierarchy. For this, it is now possible to query the ClassLoader which 
> is an argument provided to the transformer. I do not know how I would 
> access the module at this point or get hold of the class file.
I assume you know this but if you have the Instrumentation object then 
you can invoke getAllLoadedClasses and filter this to the set of classes 
that implement the interface. Then call retransformClasses with this set 
(as an array) and your ClassFileTransformer should be invoked with the 
Class<?> + class bytes for each of these classes. This puts the onus on 
the VM to reconstitute the class bytes, no need for you to attempt to 
locate the .class file (which may not exist of course). It also allows 
multiple agents to work together.

>
> Also, I would expect that a Java agent that worked in Java 8 would 
> also work in Java 9. If I had to use Java 9 APIs in order to patch my 
> application, I would be required to offer two versions of my library, 
> one for Java 9+ and one for Java 8-. That would be unfortunate.
>
Existing java agents that are instrumenting code on the class path 
should continue to work as before. These agents are oblivious to modules 
but in many cases will be able to instrument code in named modules too. 
The VM is providing some "readability for free"  to keep existing 
instrumentation agents working. Beyond these cases then I expect agents 
will need to become module aware because they are instrumenting code in 
ways that interacts with modules and access checks. The current builds 
don't have these APIs yet. As to making use of newer APIs then it might 
add a bit of complexity to these agents or maybe to their how they are 
built.

-Alan


More information about the jigsaw-dev mailing list