question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support using currently loaded classes as TypePool

See original GitHub issue

When 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:closed
  • Created 8 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
CodingFabiancommented, Feb 25, 2016

I hired a consultant to fix this issue for us. He will probably send a PR soon 😃

0reactions
CodingFabiancommented, Mar 3, 2016

I guess we can close this. The type pool is the wrong solution and we should use raw matchers.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found