|
Groovy 2.2.0 | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
java.lang.Object groovy.transform.ConditionalInterrupt
@Retention(RetentionPolicy.SOURCE) @Target([ElementType.PACKAGE, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE, ElementType.LOCAL_VARIABLE]) @GroovyASTTransformationClass(["org.codehaus.groovy.transform.ConditionalInterruptibleASTTransformation"]) @interface ConditionalInterrupt
Allows "interrupt-safe" executions of scripts by adding a custom check for interruption into loops (for, while, ...) and at the start of closures and 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.
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 (XXXXXX will be replaced with some runtime generated hashCode). 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.conditionalTransformXXXXXX$condition()) { throw new java.lang.InterruptedException('Execution interrupted. The following condition failed: { counter++> 10}') } 4.times({ if (this.conditionalTransformXXXXXX$condition()) { throw new java.lang.InterruptedException('Execution interrupted. The following condition failed: { counter++> 10}') } this.println('executing script method...') }) } private java.lang.Object conditionalTransformXXXXXX$condition() { counter++ > 10 } }Note that when you're annotating scripts, the variable scoping semantics are unchanged. Therefore, you must be careful about the variable scope you're using. Make sure that variables you reference in the closure parameter are in scope during script execution. The following example will throw a MissingPropertyException because counter is not in scope for a class:
import groovy.transform.ConditionalInterrupt
def counter = 0
@ConditionalInterrupt({ counter++> 10})
class MyClass {
def myMethod() {
4.times {
println 'executing script method...'
}
}
}
new MyClass().myMethod()
Additional usage examples can be found in the unit test for this class.
Method Summary | |
---|---|
boolean
|
applyToAllClasses()
Set this to false if you have multiple classes within one source file and only want a conditional check on some of the classes. |
boolean
|
applyToAllMembers()
Set this to false if you have multiple methods/closures within a class or script and only want conditional checks on some of them. |
boolean
|
checkOnMethodStart()
By default a conditional check is added to the start of all user-defined methods. |
Class
|
thrown()
Sets the type of exception which is thrown. |
Class
|
value()
Conditional check - set as a closure expression. |
Methods inherited from class Object | |
---|---|
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
Method Detail |
---|
boolean applyToAllClasses()
applyToAllMembers
.
boolean applyToAllMembers()
applyToAllClasses
is automatically set to false.
Set to true (the default) for blanket coverage of conditional checks on all methods, loops
and closures within the class/script.
boolean checkOnMethodStart()
Class thrown()
Class value()
Copyright © 2003-2013 The Codehaus. All rights reserved.