Package org.codehaus.groovy.runtime

# Class ProxyGeneratorAdapter

java.lang.Object
org.objectweb.asm.ClassVisitor
org.codehaus.groovy.runtime.ProxyGeneratorAdapter

public class ProxyGeneratorAdapter extends org.objectweb.asm.ClassVisitor
A proxy generator responsible for mapping a map of closures to a class implementing a list of interfaces. For example, the following code:

         abstract class Foo {
             abstract void bar();
             abstract void baz();
         }
         def dyn = [bar: { println 'hello' }, baz: { println 'world'}] as Foo

will generate a proxy class which extends class Foo and delegates method calls to the provided closures. The generated proxy implements the `GroovyObject` interface. Additionally, this proxy generator supports delegation to another object. In that case, if a method is defined both in the closure map and the delegate, the version from the map is preferred. This allows overriding methods from delegates with ease. Internally, the proxy generator makes use of ASM to generate bytecode, for improved performance as compared to the legacy proxy generation mechanism which made use of string templates.

Since:
  2.0.0

- ## Field Summary

  ### Fields inherited from class org.objectweb.asm.ClassVisitor

  `api, cv`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ProxyGeneratorAdapter(Map<Object,Object> closureMap, Class requestedSuperClass, Class[] interfaces, ClassLoader proxyLoader, boolean emptyBody, Class delegateClass)`
  Construct a proxy generator.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `GroovyObject`
  `delegatingProxy(Object delegate, Map<Object,Object> map, Object... constructorArgs)`
  `static Closure`
  `ensureClosure(Object o)`
  Ensures that the provided object is wrapped into a closure if it's not a closure.
  `boolean`
  `isProxyHidden()`
  Diagnostic: `true` if this adapter's proxy was defined as a hidden nestmate (JEP 371).
  `protected org.objectweb.asm.MethodVisitor`
  `makeDelegateCall(String name, String desc, String signature, String[] exceptions, int accessFlags)`
  Generate a call to the delegate object.
  `protected org.objectweb.asm.MethodVisitor`
  `makeDelegateToClosureCall(String name, String desc, String signature, String[] exceptions, int accessFlags)`
  `GroovyObject`
  `proxy(Map<Object,Object> map, Object... constructorArgs)`
  `String`
  `proxyName()`
  Returns the proxy name used in the generated bytecode (internal form).
  `void`
  `visit(int version, int access, String name, String signature, String superName, String[] interfaces)`
  `org.objectweb.asm.MethodVisitor`
  `visitMethod(int access, String name, String desc, String signature, String[] exceptions)`

  ### Methods inherited from class org.objectweb.asm.ClassVisitor

  `getDelegate, visitAnnotation, visitAttribute, visitEnd, visitField, visitInnerClass, visitModule, visitNestHost, visitNestMember, visitOuterClass, visitPermittedSubclass, visitRecordComponent, visitSource, visitTypeAnnotation`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ProxyGeneratorAdapter

    public ProxyGeneratorAdapter(Map<Object,Object> closureMap, Class requestedSuperClass, Class[] interfaces, ClassLoader proxyLoader, boolean emptyBody, Class delegateClass)
    Construct a proxy generator. This generator is used when we need to create a proxy object for a class or an interface given a map of closures.
    Parameters:
      `closureMap` - the delegates implementations
      `superClass` - corresponding to the superclass class visitor
      `interfaces` - extra interfaces the proxy should implement
      `proxyLoader` - the class loader which should be used to load the generated proxy
      `delegateClass` - if not null, generate a delegate field with the corresponding class
      `emptyBody` - if set to true, the unimplemented abstract methods will receive an empty body instead of throwing an `UnsupportedOperationException`.

- ## Method Details

  - ### isProxyHidden

    public boolean isProxyHidden()
    Diagnostic: `true` if this adapter's proxy was defined as a hidden nestmate (JEP 371). Not part of the stable user-facing proxy contract.
    Returns:
      `true` when the proxy class is hidden

  - ### visit

    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
    Overrides:
      `visit` in class `org.objectweb.asm.ClassVisitor`

  - ### proxyName

    public String proxyName()
    Returns the proxy name used in the generated bytecode (internal form). This is also the name visible in stack traces for visible-class proxies.
    Returns:
      the proxy name

  - ### visitMethod

    public org.objectweb.asm.MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions)
    Overrides:
      `visitMethod` in class `org.objectweb.asm.ClassVisitor`

  - ### makeDelegateCall

    protected org.objectweb.asm.MethodVisitor makeDelegateCall(String name, String desc, String signature, String[] exceptions, int accessFlags)
    Generate a call to the delegate object.

  - ### makeDelegateToClosureCall

    protected org.objectweb.asm.MethodVisitor makeDelegateToClosureCall(String name, String desc, String signature, String[] exceptions, int accessFlags)

  - ### proxy

    public GroovyObject proxy(Map<Object,Object> map, Object... constructorArgs)

  - ### delegatingProxy

    public GroovyObject delegatingProxy(Object delegate, Map<Object,Object> map, Object... constructorArgs)

  - ### ensureClosure

    public static Closure ensureClosure(Object o)
    Ensures that the provided object is wrapped into a closure if it's not a closure. Do not trust IDEs, this method is used in bytecode.