Class BytecodeWithUrl

java.lang.Object
io.opentelemetry.javaagent.tooling.BytecodeWithUrl

public abstract class BytecodeWithUrl extends Object
Provides the bytecode and the original resource URL for loaded and not-yet loaded classes. The implementation is based on ClassFileLocator.ForClassLoader, with the difference that it preserves the original classfile resource URL. In addition BytecodeWithUrl created can be generated for runtime generated types, this class will take care of runtime generating URLs for those.
  • Method Details

    • getUrl

      public abstract URL getUrl()
      Provides a URL pointing to the specific classfile.
      Returns:
      the URL
    • getBytecode

      public abstract byte[] getBytecode()
      Provides the bytecode of the class. The result is the same as calling URL.openStream() on getUrl() and draining that stream.
      Returns:
      the bytecode of the class.
    • cached

      public abstract BytecodeWithUrl cached()
      Creates a cached copy of this BytecodeWithUrl. The cached copy eagerly loads the bytecode, so that getBytecode() is guaranteed to not cause any IO. This comes at the cost of a higher heap consumption, as the bytecode is kept in memory.
      Returns:
      an ClassFileSource implementing the described caching behaviour.
    • create

      public static BytecodeWithUrl create(String className, ClassLoader classLoader)
      Creates a BytecodeWithUrl for the class with the provided fully qualified name. The .class file for the provided classname must be available as a resource in the provided classloader. The class is guaranteed to not be loaded during this process.
      Parameters:
      className - the fully qualified name of the class to copy
      classLoader - the classloader
      Returns:
      the ClassCopySource which can be used to copy the provided class to other classloaders.
    • create

      public static BytecodeWithUrl create(Class<?> loadedClass)
      Same as create(String, ClassLoader), but easier to use for already loaded classes.
      Parameters:
      loadedClass - the class to copy
      Returns:
      the ClassCopySource which can be used to copy the provided class to other classloaders.
    • create

      public static BytecodeWithUrl create(String className, byte[] bytecode)
      Creates a BytecodeWithUrl for a runtime-generated type. It will also provide an artificially generated URL pointing to the in-memory bytecode.
      Parameters:
      className - the name of the class represented by the provided bytecode
      bytecode - the bytecode of the class
      Returns:
      the BytecodeWithUrl referring to this dynamically generated class
    • create

      public static BytecodeWithUrl create(net.bytebuddy.dynamic.DynamicType.Unloaded<?> dynamicType)
      Invokes create(String, byte[]) for the provided dynamic type.
      Parameters:
      dynamicType - the type to generate the BytecodeWithUrl
      Returns:
      the BytecodeWithUrl referring to this dynamically generated type