Class FromString
- java.lang.Object
-
- groovy.transform.stc.ClosureSignatureHint
-
- groovy.transform.stc.FromString
-
public class FromString extends ClosureSignatureHint
A closure parameter hint class that is convenient if you want to use a String representation of the signature. It makes use of the
option strings
, where each string corresponds to a single signature.The following example describes a closure as accepting a single signature (List<T> list ->):
public <T> T apply(T src, @ClosureParams(value=FromString.class, options="List<T>" Closure<T> cl)
The next example describes a closure as accepting two signatures (List<T> list ->) and (T t ->):
public <T> T apply(T src, @ClosureParams(value=FromString.class, options={"List<T>","T"} Closure<T> cl)
It is advisable not to use this hint as a replacement for the various
FirstParam
,SimpleType
, ... hints because it is actually much slower. Using this hint should therefore be limited to cases where it's not possible to express the signature using the existing hints.- Since:
- 2.3.0
-
-
Constructor Summary
Constructors Constructor Description FromString()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<ClassNode[]>
getClosureSignatures(MethodNode node, SourceUnit sourceUnit, CompilationUnit compilationUnit, String[] options, ASTNode usage)
Subclasses should implement this method, which returns the list of accepted closure signatures.-
Methods inherited from class groovy.transform.stc.ClosureSignatureHint
findClassNode, pickGenericType, pickGenericType
-
-
-
-
Method Detail
-
getClosureSignatures
public List<ClassNode[]> getClosureSignatures(MethodNode node, SourceUnit sourceUnit, CompilationUnit compilationUnit, String[] options, ASTNode usage)
Description copied from class:ClosureSignatureHint
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
Closure
parameter annotated withClosureParams
. 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
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:- the method node corresponding to the target method (here, the
DefaultGroovyMethods.collect(java.lang.Iterable, groovy.lang.Closure)
method - the (optional) list of options found in the annotation
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.
- Specified by:
getClosureSignatures
in classClosureSignatureHint
- Parameters:
node
- the method node for which aClosure
parameter was annotated withClosureParams
sourceUnit
- the source unit of the file being compiledcompilationUnit
- the compilation unit of the file being compiledoptions
- the options, corresponding to theClosureParams.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
- the AST node, in the compiled file, which triggered a call to this method. Normally only used for logging/error handling
- the method node corresponding to the target method (here, the
-
-