Class CachedConstructor

java.lang.Object
org.codehaus.groovy.reflection.ParameterTypes
org.codehaus.groovy.reflection.CachedConstructor
All Implemented Interfaces:
MetaMember

public class CachedConstructor extends ParameterTypes implements MetaMember
Caches reflection information about a single constructor for fast lookup and invocation.

Extends ParameterTypes to manage parameter type information and implements MetaMember for meta-programming support. Provides methods to invoke constructors with argument coercion and error handling.

  • Constructor Details

    • CachedConstructor

      public CachedConstructor(CachedClass clazz, Constructor c)
      Constructs a CachedConstructor for the given constructor within a cached class.
      Parameters:
      clazz - the cached class that declares this constructor
      c - the Java constructor to cache
    • CachedConstructor

      public CachedConstructor(Constructor c)
      Constructs a CachedConstructor for the given Java constructor. Automatically resolves the cached class from the constructor's declaring class.
      Parameters:
      c - the Java constructor to cache
  • Method Details

    • getPT

      protected Class[] getPT()
      Description copied from class: ParameterTypes
      Protected method subclasses override to provide parameter types. Default implementation throws UnsupportedOperationException.
      Overrides:
      getPT in class ParameterTypes
      Returns:
      the native parameter class array
    • getName

      public String getName()
      Returns the name of the constructor (same as the declaring class name).
      Specified by:
      getName in interface MetaMember
      Returns:
      the constructor name
    • getModifiers

      public int getModifiers()
      Returns the modifiers of this constructor (e.g., public, private, protected). See Modifier for modifier constants.
      Specified by:
      getModifiers in interface MetaMember
      Returns:
      the constructor modifiers
    • isSynthetic

      public boolean isSynthetic()
      Returns whether this constructor is synthetic (generated by the compiler).
      Specified by:
      isSynthetic in interface MetaMember
      Returns:
      true if this constructor is synthetic; false otherwise
    • toString

      public String toString()
      Returns the string representation of this cached constructor.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the underlying constructor
    • find

      public static CachedConstructor find(Constructor constructor)
      Finds a CachedConstructor corresponding to the specified Java constructor by searching the cached constructors of its declaring class.
      Parameters:
      constructor - the constructor to find
      Returns:
      the cached constructor
      Throws:
      RuntimeException - if the constructor is not found in the cache
    • doConstructorInvoke

      public Object doConstructorInvoke(Object[] argumentArray)
      Invokes this constructor with the specified arguments after coercing them to the parameter types.
      Parameters:
      argumentArray - the arguments to pass to the constructor
      Returns:
      a new instance of the class
      Throws:
      GroovyRuntimeException - if the invocation fails
    • invoke

      public Object invoke(Object[] argumentArray)
      Invokes this constructor with the specified arguments. Handles access control, abstract class checks, and exception wrapping.
      Parameters:
      argumentArray - the arguments to pass to the constructor
      Returns:
      a new instance of the class
      Throws:
      GroovyRuntimeException - if invocation fails, access is denied, or the class is abstract
    • getCachedClass

      public CachedClass getCachedClass()
      Returns the cached class that declares this constructor.
      Returns:
      the cached class
    • getDeclaringClass

      public Class getDeclaringClass()
      Returns the class that declares this constructor.
      Returns:
      the declaring class
    • getCachedConstructor

      public Constructor getCachedConstructor()
      Returns the underlying Java constructor object, making it accessible if necessary.
      Returns:
      the cached constructor