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.
| Constructor and description |
|---|
ProxyGeneratorAdapter(Map<Object, Object> closureMap, Class requestedSuperClass, Class[] interfaces, ClassLoader proxyLoader, boolean emptyBody, Class delegateClass)Construct a proxy generator. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public GroovyObject |
delegatingProxy(Object delegate, Map<Object, Object> map, Object constructorArgs)Instantiates the generated delegating proxy. |
|
public static Closure |
ensureClosure(Object o)Ensures that the provided object is wrapped into a closure if it's not a closure. |
|
public 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)Emits a proxy method that boxes its arguments and calls a closure from the $closures$delegate$map field. |
|
public GroovyObject |
proxy(Map<Object, Object> map, Object constructorArgs)Instantiates the generated proxy. |
|
public String |
proxyName()Returns the proxy name used in the generated bytecode (internal form). |
|
public void |
visit(int version, int access, String name, String signature, String superName, String[] interfaces) |
|
public org.objectweb.asm.MethodVisitor |
visitMethod(int access, String name, String desc, String signature, String[] exceptions) |
| Methods inherited from class | Name |
|---|---|
class org.objectweb.asm.ClassVisitor |
org.objectweb.asm.ClassVisitor#equals(java.lang.Object), org.objectweb.asm.ClassVisitor#getClass(), org.objectweb.asm.ClassVisitor#getDelegate(), org.objectweb.asm.ClassVisitor#hashCode(), org.objectweb.asm.ClassVisitor#notify(), org.objectweb.asm.ClassVisitor#notifyAll(), org.objectweb.asm.ClassVisitor#toString(), org.objectweb.asm.ClassVisitor#visit(int, int, java.lang.String, java.lang.String, java.lang.String, [Ljava.lang.String;), org.objectweb.asm.ClassVisitor#visitAnnotation(java.lang.String, boolean), org.objectweb.asm.ClassVisitor#visitAttribute(org.objectweb.asm.Attribute), org.objectweb.asm.ClassVisitor#visitEnd(), org.objectweb.asm.ClassVisitor#visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object), org.objectweb.asm.ClassVisitor#visitInnerClass(java.lang.String, java.lang.String, java.lang.String, int), org.objectweb.asm.ClassVisitor#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, [Ljava.lang.String;), org.objectweb.asm.ClassVisitor#visitModule(java.lang.String, int, java.lang.String), org.objectweb.asm.ClassVisitor#visitNestHost(java.lang.String), org.objectweb.asm.ClassVisitor#visitNestMember(java.lang.String), org.objectweb.asm.ClassVisitor#visitOuterClass(java.lang.String, java.lang.String, java.lang.String), org.objectweb.asm.ClassVisitor#visitPermittedSubclass(java.lang.String), org.objectweb.asm.ClassVisitor#visitRecordComponent(java.lang.String, java.lang.String, java.lang.String), org.objectweb.asm.ClassVisitor#visitSource(java.lang.String, java.lang.String), org.objectweb.asm.ClassVisitor#visitTypeAnnotation(int, org.objectweb.asm.TypePath, java.lang.String, boolean), org.objectweb.asm.ClassVisitor#wait(), org.objectweb.asm.ClassVisitor#wait(long), org.objectweb.asm.ClassVisitor#wait(long, int) |
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.
closureMap - the delegates implementationsinterfaces - extra interfaces the proxy should implementproxyLoader - the class loader which should be used to load the generated proxydelegateClass - if not null, generate a delegate field with the corresponding classemptyBody - if set to true, the unimplemented abstract methods will receive an empty body instead of
throwing an UnsupportedOperationException. Instantiates the generated delegating proxy. A null constructorArgs
together with a cached no-argument constructor calls that constructor with
map and delegate. Otherwise those two values are appended, in
that order, after the constructor arguments, and a null argument list
is treated as empty.
A failure of the cached constructor is wrapped in GroovyRuntimeException.
delegate - the object later methods are forwarded tomap - closure or property map passed to the proxy constructorconstructorArgs - arguments for the super constructor; null means noneEnsures 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.
Diagnostic: true if this adapter's proxy was defined as a hidden
nestmate (JEP 371). Not part of the stable user-facing proxy contract.
true when the proxy class is hiddenGenerate a call to the delegate object.
Emits a proxy method that boxes its arguments and calls a closure from the
$closures$delegate$map field. The map is read under name, then
under "*" when that entry is missing. The value passes through
ensureClosure(Object) and Closure.call, and the
result is unboxed or cast to the method's return type. The visitor is ended
here.
name - method name, also the closure-map keydesc - ASM method descriptor used for the argument and return typessignature - generic signature passed to org.objectweb.asm.ClassVisitor#visitMethodexceptions - exception names passed to org.objectweb.asm.ClassVisitor#visitMethodaccessFlags - method access flagsnull after the method visitor has been ended Instantiates the generated proxy. A null constructorArgs together
with a cached no-argument constructor calls that constructor with map
alone. Otherwise map is appended after the constructor arguments, and a
null argument list is treated as empty.
A failure of the cached constructor is wrapped in GroovyRuntimeException.
map - closure or property map passed to the proxy constructorconstructorArgs - arguments for the super constructor; null means noneReturns the proxy name used in the generated bytecode (internal form). This is also the name visible in stack traces for visible-class proxies.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.