Top Description Inners Fields Constructors Methods
groovy.transform.stc

public Class MapEntryOrKeyValue

extends ClosureSignatureHint
Class Inheritance
Imports
org.codehaus.groovy.ast.ASTNode, .ClassHelper, .ClassNode, .GenericsType, .MethodNode, org.codehaus.groovy.control.CompilationUnit, .SourceUnit, java.util.Arrays, .Collections, .List, .Map

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.util.Map.Entry map entry or on a key,value pair.

The result is a closure which can have the following forms:

This 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) { ... }

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final ClassNode

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public List<ClassNode[]>
getClosureSignatures(final MethodNode
the method node for which a groovy.lang.Closure parameter was annotated with ClosureParams
node
,
final SourceUnit
the 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 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.
options
,
final ASTNode
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.transform.stc.ClosureSignatureHint.getClosureSignatures.

Subclasses should implement this method, which returns the list of accepted closure signatures.

Inherited from groovy.transform.stc.ClosureSignatureHint:
findClassNodepickGenericTypepickGenericType

Field Detail

MAPENTRY_TYPEback to summary
private static final ClassNode MAPENTRY_TYPE

Constructor Detail

MapEntryOrKeyValueback to summary
public MapEntryOrKeyValue()

Method Detail

getClosureSignaturesback to summary
public List<ClassNode[]> getClosureSignatures(final MethodNode node, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final String[] options, final ASTNode usage)

Implements abstract groovy.transform.stc.ClosureSignatureHint.getClosureSignatures.

Doc from groovy.transform.stc.ClosureSignatureHint.getClosureSignatures.

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.lang.Closure parameter annotated with groovy.transform.stc.ClosureParams. So imagine the following code needs to be compiled:

@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 org.codehaus.groovy.runtime.DefaultGroovyMethods#collect(java.lang.Iterable, groovy.lang.Closure) method signature to infer the type of it. With the annotation, it will now try to find an annotation on the closure parameter. If it finds it, then an instance of the hint class is created and the type checker calls it with the following arguments:

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.

Parameters
node:MethodNode

the method node for which a groovy.lang.Closure parameter was annotated with ClosureParams

sourceUnit:SourceUnit

the source unit of the file being compiled

compilationUnit:CompilationUnit

the compilation unit of the file being compiled

options:String[]

the options, corresponding to the 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.

usage:ASTNode

the AST node, in the compiled file, which triggered a call to this method. Normally only used for logging/error handling

groovy.transform.stc back to summary

private final Class MapEntryOrKeyValue.Options

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv final int

Constructor Summary

AccessConstructor and Description
private
Options(final int parameterIndex, final boolean generateIndex)

Method Summary

Modifier and TypeMethod and Description
pack-priv static MapEntryOrKeyValue.Options
parse(MethodNode mn, ASTNode source, String[] options)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

generateIndexback to summary
pack-priv final boolean generateIndex
parameterIndexback to summary
pack-priv final int parameterIndex

Constructor Detail

Optionsback to summary
private Options(final int parameterIndex, final boolean generateIndex)

Method Detail

parseback to summary
pack-priv static MapEntryOrKeyValue.Options parse(MethodNode mn, ASTNode source, String[] options) throws IncorrectTypeHintException