Class CachedSAMClass

java.lang.Object
org.codehaus.groovy.reflection.CachedClass
org.codehaus.groovy.reflection.stdclasses.CachedSAMClass

public class CachedSAMClass extends CachedClass
Provides optimized reflection caching for Single Abstract Method (SAM) types. Converts Closure arguments to SAM interface implementations via dynamic proxies or aggregates.
  • Constructor Details

    • CachedSAMClass

      public CachedSAMClass(Class clazz, ClassInfo classInfo)
      Constructs a cached class representation for a SAM type. Locates and caches the single abstract method of the class.
      Parameters:
      clazz - the SAM type to cache
      classInfo - the class information associated with this cached class
      Throws:
      GroovyBugError - if the class does not have exactly one abstract method
  • Method Details

    • isAssignableFrom

      public boolean isAssignableFrom(Class argument)
      Determines if the given class is assignable to this SAM type. Accepts null, Closure instances, and instances already assignable to the SAM type.
      Overrides:
      isAssignableFrom in class CachedClass
      Parameters:
      argument - the class to check
      Returns:
      true if the class can be assigned to this SAM type, false otherwise
    • coerceArgument

      public Object coerceArgument(Object argument)
      Coerces the given argument to this SAM type. Converts Closure arguments to SAM interface implementations via dynamic proxies.
      Overrides:
      coerceArgument in class CachedClass
      Parameters:
      argument - the argument to coerce
      Returns:
      the coerced SAM instance, or the original argument if already compatible
    • coerceToSAM

      public static Object coerceToSAM(Closure argument, Method method, Class clazz)
      Coerces a closure to a SAM interface implementation. Automatically detects whether the SAM type is an interface or class.
      Parameters:
      argument - the closure to convert
      method - the single abstract method of the SAM type
      clazz - the SAM class or interface to coerce to
      Returns:
      a dynamic proxy or aggregate instance implementing the SAM type
    • coerceToSAM

      public static Object coerceToSAM(Closure argument, Method method, Class clazz, boolean isInterface)
      Coerces a closure to a SAM interface or class implementation. For interfaces, uses dynamic proxies or trait-aware aggregates. For classes, uses proxy generators to instantiate from base class.
      Parameters:
      argument - the closure to convert
      method - the single abstract method of the SAM type
      clazz - the SAM class or interface to coerce to
      isInterface - true if the SAM type is an interface, false for a class
      Returns:
      a dynamic proxy or aggregate instance implementing the SAM type
    • getSAMMethod

      public static Method getSAMMethod(Class<?> c)
      Finds the abstract method of given class, if it is a SAM type.