MCPcopy Index your code
hub / github.com/apache/groovy / Closure

Class Closure

src/main/java/groovy/lang/Closure.java:76–1427  ·  view source on GitHub ↗

Represents any closure object in Groovy. Groovy allows instances of Closures to be called in a short form. For example: def a = 1 def c = { a } assert c() == 1 To be able to use a Closure in this way with your own subclass, you need to provide

Source from the content-addressed store, hash-verified

74 * </pre>
75 */
76public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {
77
78 /**
79 * With this resolveStrategy set the closure will attempt to resolve property references and methods to the
80 * owner first, then the delegate (<b>this is the default strategy</b>).
81 *
82 * For example the following code:
83 * <pre class="language-groovy groovyTestCase">
84 * class Test {
85 * def x = 30
86 * def y = 40
87 *
88 * def run() {
89 * def data = [ x: 10, y: 20 ]
90 * def cl = { y = x + y }
91 * cl.delegate = data
92 * cl()
93 * assert x == 30
94 * assert y == 70
95 * assert data == [x:10, y:20]
96 * }
97 * }
98 *
99 * new Test().run()
100 * </pre>
101 * Will succeed, because the x and y fields declared in the Test class shadow the variables in the delegate.<p>
102 * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
103 */
104 public static final int OWNER_FIRST = 0;
105
106 /**
107 * With this resolveStrategy set the closure will attempt to resolve property references and methods to the
108 * delegate first then the owner.
109 *
110 * For example the following code:
111 * <pre class="language-groovy groovyTestCase">
112 * class Test {
113 * def x = 30
114 * def y = 40
115 *
116 * def run() {
117 * def data = [ x: 10, y: 20 ]
118 * def cl = { y = x + y }
119 * cl.delegate = data
120 * cl.resolveStrategy = Closure.DELEGATE_FIRST
121 * cl()
122 * assert x == 30
123 * assert y == 40
124 * assert data == [x:10, y:30]
125 * }
126 * }
127 *
128 * new Test().run()
129 * </pre>
130 * This will succeed, because the x and y variables declared in the delegate shadow the fields in the owner class.<p>
131 * <i>Note that local variables are always looked up first, independently of the resolution strategy.</i>
132 */
133 public static final int DELEGATE_FIRST = 1;

Callers

nothing calls this directly

Calls 5

ofMethod · 0.95
setDirectiveMethod · 0.80
withInitialMethod · 0.80
setMetaClassMethod · 0.65
setResolveStrategyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…