Package groovy.util

Class ProxyGenerator

java.lang.Object
groovy.util.ProxyGenerator

public class ProxyGenerator extends Object
Generates 'Proxy' objects which implement interfaces, maps of closures and/or extend classes/delegates.
  • Field Details

    • INSTANCE

      public static final ProxyGenerator INSTANCE
      Shared proxy generator instance used by Groovy's coercion helpers.
  • Constructor Details

    • ProxyGenerator

      public ProxyGenerator()
  • Method Details

    • getDebug

      public boolean getDebug()
      Reports whether generated source is echoed for debugging.
      Returns:
      true when debug output is enabled
    • setDebug

      public void setDebug(boolean debug)
      Instructs ProxyGenerator to dump generated Groovy source code to standard output during construction. This is useful for debugging purposes but should be turned off in production.
      Parameters:
      debug - true if you want generated source to be printed
    • getEmptyMethods

      public boolean getEmptyMethods()
      Reports whether unimplemented generated methods use empty bodies.
      Returns:
      true when empty method bodies are generated
    • setEmptyMethods

      public void setEmptyMethods(boolean emptyMethods)
      Changes generated methods to have empty implementations.

      Methods in generated aggregates not supplied in a closures map or base class are given 'default' implementations. The implementation will normally throw an UnsupportedOperationException but setting this boolean will leave it empty.

      Parameters:
      emptyMethods - true if you want generated methods to be empty
    • getOverride

      public ClassLoader getOverride()
      Returns the override class loader used for proxy generation.
      Returns:
      the override class loader, or null
    • setOverride

      public void setOverride(ClassLoader override)
      Sets the override class loader used for proxy generation.
      Parameters:
      override - the override class loader
    • instantiateAggregateFromBaseClass

      public GroovyObject instantiateAggregateFromBaseClass(Class clazz)
      Creates an aggregate proxy that extends the supplied base class.
      Parameters:
      clazz - the base class to extend
      Returns:
      the generated proxy
    • instantiateAggregateFromBaseClass

      public GroovyObject instantiateAggregateFromBaseClass(Map map, Class clazz)
      Creates an aggregate proxy that extends the supplied base class.
      Parameters:
      map - closures implementing selected methods
      clazz - the base class to extend
      Returns:
      the generated proxy
    • instantiateAggregateFromBaseClass

      public GroovyObject instantiateAggregateFromBaseClass(Closure cl, Class clazz)
      Creates an aggregate proxy that routes all unresolved methods through one closure.
      Parameters:
      cl - fallback closure for method calls
      clazz - the base class to extend
      Returns:
      the generated proxy
    • instantiateAggregateFromBaseClass

      public GroovyObject instantiateAggregateFromBaseClass(Class clazz, Object[] constructorArgs)
      Creates an aggregate proxy with constructor arguments.
      Parameters:
      clazz - the base class to extend
      constructorArgs - constructor arguments for the proxy instance
      Returns:
      the generated proxy
    • instantiateAggregateFromBaseClass

      public GroovyObject instantiateAggregateFromBaseClass(Map map, Class clazz, Object[] constructorArgs)
      Creates an aggregate proxy with closures and constructor arguments.
      Parameters:
      map - closures implementing selected methods
      clazz - the base class to extend
      constructorArgs - constructor arguments for the proxy instance
      Returns:
      the generated proxy
    • instantiateAggregateFromInterface

      public GroovyObject instantiateAggregateFromInterface(Class clazz)
      Creates an aggregate proxy that implements a single interface.
      Parameters:
      clazz - the interface to implement
      Returns:
      the generated proxy
    • instantiateAggregateFromInterface

      public GroovyObject instantiateAggregateFromInterface(Map map, Class clazz)
      Creates an aggregate proxy that implements a single interface.
      Parameters:
      map - closures implementing selected methods
      clazz - the interface to implement
      Returns:
      the generated proxy
    • instantiateAggregate

      public GroovyObject instantiateAggregate(List<Class> interfaces)
      Creates an aggregate proxy implementing the supplied interfaces.
      Parameters:
      interfaces - interfaces to implement
      Returns:
      the generated proxy
    • instantiateAggregate

      public GroovyObject instantiateAggregate(Map closureMap, List<Class> interfaces)
      Creates an aggregate proxy implementing the supplied interfaces.
      Parameters:
      closureMap - closures implementing selected methods
      interfaces - interfaces to implement
      Returns:
      the generated proxy
    • instantiateAggregate

      public GroovyObject instantiateAggregate(Map closureMap, List<Class> interfaces, Class clazz)
      Creates an aggregate proxy with optional interfaces and base class.
      Parameters:
      closureMap - closures implementing selected methods
      interfaces - interfaces to implement
      clazz - the base class to extend
      Returns:
      the generated proxy
    • instantiateAggregate

      public GroovyObject instantiateAggregate(Map closureMap, List<Class> interfaces, Class clazz, Object[] constructorArgs)
      Creates an aggregate proxy with optional interfaces, base class and constructor arguments.
      Parameters:
      closureMap - closures implementing selected methods
      interfaces - interfaces to implement
      clazz - the base class to extend
      constructorArgs - constructor arguments for the proxy instance
      Returns:
      the generated proxy
    • instantiateDelegate

      public GroovyObject instantiateDelegate(Object delegate)
      Creates a delegating proxy around the supplied object.
      Parameters:
      delegate - the delegate object
      Returns:
      the generated proxy
    • instantiateDelegate

      public GroovyObject instantiateDelegate(List<Class> interfaces, Object delegate)
      Creates a delegating proxy with additional implemented interfaces.
      Parameters:
      interfaces - interfaces to implement
      delegate - the delegate object
      Returns:
      the generated proxy
    • instantiateDelegate

      public GroovyObject instantiateDelegate(Map closureMap, List<Class> interfaces, Object delegate)
      Creates a delegating proxy with supplemental closure implementations.
      Parameters:
      closureMap - closures implementing selected methods
      interfaces - interfaces to implement
      delegate - the delegate object
      Returns:
      the generated proxy
    • instantiateDelegateWithBaseClass

      public GroovyObject instantiateDelegateWithBaseClass(Map closureMap, List<Class> interfaces, Object delegate)
      Creates a delegating proxy whose base class is the delegate type.
      Parameters:
      closureMap - closures implementing selected methods
      interfaces - interfaces to implement
      delegate - the delegate object
      Returns:
      the generated proxy
    • instantiateDelegateWithBaseClass

      public GroovyObject instantiateDelegateWithBaseClass(Map closureMap, List<Class> interfaces, Object delegate, Class baseClass)
      Creates a delegating proxy with an explicit base class.
      Parameters:
      closureMap - closures implementing selected methods
      interfaces - interfaces to implement
      delegate - the delegate object
      baseClass - the base class to extend
      Returns:
      the generated proxy
    • instantiateDelegateWithBaseClass

      public GroovyObject instantiateDelegateWithBaseClass(Map closureMap, List<Class> interfaces, Object delegate, Class baseClass, String name)
      Creates a proxy with a delegate object.
      Parameters:
      closureMap - the closure for methods not handled by the delegate
      interfaces - interfaces to be implemented
      delegate - the delegate object
      baseClass - the base class
      name - the name of the proxy, unused, but kept for compatibility with previous versions of Groovy.
      Returns:
      a proxy object implementing the specified interfaces, and delegating to the provided object