A special hint which handles a common use case in the Groovy methods that work on maps. In case of an
iteration on a list of map entries, you often want the user to be able to work either on a java. map entry
or on a key,value pair.
The result is a closure which can have the following forms:
{ key, value -> ... } where key is the key of a map entry, and value the corresponding value{ entry -> ... } where entry is a java.util.Map.Entry map entry{ ... } where it is an implicit java.util.Map.Entry map entryThis hint handles all those cases by picking the generics from the first argument of the method (by default).
The options array is used to modify the behavior of this hint. Each string in the option array consists of a key=value pair.
void doSomething(String str, Map<K,>V map, @ClosureParams(value=MapEntryOrKeyValue.class,options="argNum=1") Closure c) { ... }
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final ClassNode |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public List | getClosureSignatures(final MethodNode
the method node for which a node, final SourceUnit groovy. parameter was annotated with
ClosureParamsthe source unit of the file being compiled sourceUnit, final CompilationUnit the compilation unit of the file being compiled compilationUnit, final String[] the options, corresponding to the options, final ASTNode ClosureParams#options() found on the annotation @return a non-null list of signature, where a signature corresponds to an array of class nodes, each of them matching a parameter.the AST node, in the compiled file, which triggered a call to this method. Normally only used for logging/error handling usage)Implements abstract groovy. Subclasses should implement this method, which returns the list of accepted closure signatures. |
| MAPENTRY_TYPE | back to summary |
|---|---|
| private static final ClassNode MAPENTRY_TYPE | |
| MapEntryOrKeyValue | back to summary |
|---|---|
| public MapEntryOrKeyValue() | |
| getClosureSignatures | back to summary |
|---|---|
| public List Implements abstract groovy. Doc from groovy. Subclasses should implement this method, which returns the list of accepted closure signatures. The compiler will call this method each time, in a source file, a method call using a closure
literal is encountered and that the target method has the corresponding @groovy.transform.TypeChecked
void doSomething() {
println ['a','b'].collect { it.toUpperCase() }
}
The collect method accepts a closure, but normally, the type checker doesn't have enough type information
in the sole
Now, the hint instance can return the list of expected parameters. Here, it would have to say that the collect method accepts a closure for which the only argument is of the type of the first generic type of the first argument. With that type information, the type checker can now infer that the type of it is String, because the first argument (here the receiver of the collect method) is a List<String> Subclasses are therefore expected to return the signatures according to the available context, which is only the target method and the potential options.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final boolean | |
| pack-priv final int |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static MapEntryOrKeyValue. |
| generateIndex | back to summary |
|---|---|
| pack-priv final boolean generateIndex | |
| parameterIndex | back to summary |
|---|---|
| pack-priv final int parameterIndex | |
| Options | back to summary |
|---|---|
| private Options(final int parameterIndex, final boolean generateIndex) | |
| parse | back to summary |
|---|---|
| pack-priv static MapEntryOrKeyValue. | |