Support using currently loaded classes as TypePool
See original GitHub issueWhen writing an agent, I might want to formulate a transformation based on a type description like this:
Resolution httpServlet = typePool.describe("javax.servlet.http.HttpServlet");
if (httpServlet.isResolved()) {
TypeDescription httpServletType = httpServlet.resolve();
transformer = agentBuilder.type(subTypeOf(httpServletType)))
Now, there are a couple of ways to obtain the typePool. Like typePool = TypePool.Default.ofClassPath();
The problem here is that HttpServlet is not on the classpath.
it is however contained in “allLoadedClasses” of “Instrumentation”
So how about an Instrumentation based type pool which works like this?
private void initByteBuddy(final Instrumentation inst) {
typePool = TypePool.Default.of(new ClassFileLocator() {
@Override
public Resolution locate(String typeName) throws IOException {
Class<?>[] allLoadedClasses = inst.getAllLoadedClasses();
for (Class<?> clazz : allLoadedClasses) {
if (clazz.getName().equals(typeName)) {
return ClassFileLocator.ForClassLoader.read(clazz);
}
}
return Resolution.Illegal.INSTANCE;
}
});
}
This works, but I am unsure about performance and other implications. For example I assume that the type pool will only have loaded classes, so using a Typedescription for the initial load of something might not work.
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
TypePool caching error for classes loaded by Bootstrap ...
Problem description: we use ByteBuddy with our Java agent (and it's awesome :)). We use it with the DescriptionStrategy.
Read more >Redefining an unloaded class with ByteBuddy - java
I see that ByteArrayClassLoader is loading the class using Launcher$AppClassLoader instead of self. This causes the exception.
Read more >List All the Classes Loaded in the JVM - Baeldung
In this tutorial, we'll learn different techniques to list all classes loaded in the JVM. For example, we can load the JVM's heap...
Read more >Runtime Code Generation with Byte Buddy - Oracle Blogs
For that, Byte Buddy supports a concept called TypeDescription , which represents Java classes in an unloaded state. You can populate a pool...
Read more >Class Loading Reference - Quarkus
The flat classpath strategy is also used for GraalVM native images, since GraalVM does not really support multiple ClassLoaders. For all other use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I hired a consultant to fix this issue for us. He will probably send a PR soon 😃
I guess we can close this. The type pool is the wrong solution and we should use raw matchers.