Class ClassLoaderForClassArtifacts

java.lang.Object
java.lang.ClassLoader
org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts
Direct Known Subclasses:
CallSiteClassLoader

public class ClassLoaderForClassArtifacts extends ClassLoader
A specialized ClassLoader used to define per-class artifact classes such as generated meta-method dispatchers.

Since Groovy 6.0 this loader first attempts to define each artifact as a hidden nestmate of the target via HiddenClassDefiner.tryDefineNestmate(Class, byte[], boolean). When that soft-fails (unopened package, unsuitable host, …) it falls back to ClassLoader.defineClass(byte[], int, int) with the protection domain captured at construction. Module / nest-host policy is documented on HiddenClassDefiner.

See Also:
  • Field Details

    • klazz

      public final SoftReference<Class> klazz
      Soft reference to the class for which artifacts are generated.
  • Constructor Details

    • ClassLoaderForClassArtifacts

      public ClassLoaderForClassArtifacts(Class klazz)
      Creates a new artifact class loader for the specified class.
      Parameters:
      klazz - the class whose artifact classes are to be defined via this loader
  • Method Details

    • define

      public Class define(String name, byte[] bytes)
      Defines a class from bytecode, preferring a hidden nestmate of the target.
      Parameters:
      name - the binary name used for the fallback (visible-class) path
      bytes - the class-file bytes
      Returns:
      the defined class
    • defineClassAndGetConstructor

      public Constructor defineClassAndGetConstructor(String name, byte[] bytes, Class<?>... parameterTypes)
      Defines a class from bytecode and returns the public constructor matching the given parameter types, or null if definition or lookup fails.

      Uses Class.getConstructor(Class[]) so the returned constructor is always publicly accessible (same contract as before hidden-class support).

      Parameters:
      name - the binary name (for fallback visible-class definition)
      bytes - the class-file bytes
      parameterTypes - the constructor parameter types to look up
      Returns:
      the matching public constructor, or null
    • loadClass

      public Class loadClass(String name) throws ClassNotFoundException
      Overrides:
      loadClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • createClassName

      public String createClassName(Method method)
      Generates a unique class name for an artifact associated with the given method.
      Parameters:
      method - the method for which the artifact is generated
      Returns:
      a unique class name
    • createClassName

      public String createClassName(String methodName)
      Generates a unique class name for an artifact associated with the given method name.

      For classes in the java.* package hierarchy the name is prefixed to avoid the restricted java. namespace. The counter suffix ensures uniqueness when multiple artifacts share the same logical name.

      Parameters:
      methodName - the method name component of the artifact class name
      Returns:
      a unique class name