|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
java.lang.Object groovy.transform.ConditionalInterrupt
@Retention(RetentionPolicy.SOURCE) @Target([ ElementType.METHOD, ElementType.TYPE]) @GroovyASTTransformationClass(["org.codehaus.groovy.transform.ConditionalInterruptibleASTTransformation"]) @interface ConditionalInterrupt
Allows "interrupt-safe" executions of scripts by adding a custom check for interruption
on loops (for, while, do), the first statement of closures, and the first statement of methods.
This is especially useful when executing foreign scripts that you do not have control over. Inject this
transformation into a script that you need to interrupt based on some custom criteria.
Annotating anything in a script will cause for loops, while loops, methods, and closures to make a
check against the specified closure. If the closure yields true (according to GroovyTruth), then the script
will throw an InterruptedException. The annotation by default applies to any classes defined in the script
as well. Annotated a class will cause (by default) all classes in the entire file ('Compilation Unit') to be
enhanced. You can fine tune what is enhanced using the annotation parameters.
Extensive usage examples can be found in the unit test for this class. A smaller example is presented here.
The following is sample usage of the annotation:
@ConditionalInterrupt({ counter++> 10})
import groovy.transform.ConditionalInterrupt
counter = 0
def scriptMethod() {
4.times {
println 'executing script method...'
}
}
scriptMethod()
Which results in the following code being generated. Notice the checks and exceptions:
public class script1291741477073 extends groovy.lang.Script { Object counter = 0 public java.lang.Object run() { counter = 0 } public java.lang.Object scriptMethod() { if (this.conditionalTransform$condition()) { throw new java.lang.InterruptedException('Execution interrupted. The following condition failed: { counter++> 10}') } 4.times({ if (this.conditionalTransform$condition()) { throw new java.lang.InterruptedException('Execution interrupted. The following condition failed: { counter++> 10}') } this.println('executing script method...') }) } private java.lang.Object conditionalTransform$condition() { counter++ > 10 } }
import groovy.transform.ConditionalInterrupt
def counter = 0
@ConditionalInterrupt({ counter++> 10})
class MyClass {
def myMethod() {
4.times {
println 'executing script method...'
}
}
}
new MyClass().myMethod()
Method Summary | |
---|---|
boolean
|
applyToAllClasses()
By default, annotating anything in a source file ('Compilation Unit') will trigger this transformation for all classes and scripts in that file. |
boolean
|
checkOnMethodStart()
By default an isInterrupted check is added to the start of all user-defined methods. |
java.lang.Class
|
thrown()
Sets the type of exception which is thrown. |
java.lang.Class
|
value()
Condition should be set as a closure expression. |
Methods inherited from class java.lang.Object | |
---|---|
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Method Detail |
---|
boolean applyToAllClasses()
boolean checkOnMethodStart()
java.lang.Class thrown()
java.lang.Class value()
Groovy Documentation