Top Description Constructors Methods
groovy.transform.stc

public Class FromString

extends ClosureSignatureHint
Class Inheritance
Imports
org.codehaus.groovy.ast.ASTNode, .ClassNode, .MethodNode, org.codehaus.groovy.ast.tools.GenericsUtils, org.codehaus.groovy.control.CompilationUnit, .SourceUnit, java.util.ArrayList, .List

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

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.

private static ClassNode[]

Returns:

a class node if it could be parsed and resolved, null otherwise
parseOption
(final String
a string representing a type
option
,
final SourceUnit
the source unit (of the file being compiled)
sourceUnit
,
final CompilationUnit
the compilation unit (of the file being compiled)
compilationUnit
,
final MethodNode
the method node
mn
,
final ASTNode usage)

Parses a string representing a type, that must be aligned with the current context.

Inherited from groovy.transform.stc.ClosureSignatureHint:
findClassNodepickGenericTypepickGenericType

Constructor Detail

FromStringback to summary
public FromString()

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

Annotations
@Override
parseOptionback to summary
private static ClassNode[] parseOption(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage)

Parses a string representing a type, that must be aligned with the current context. For example, "List<T>" must be converted into the appropriate ClassNode for which T matches the appropriate placeholder.

Parameters
option:String

a string representing a type

sourceUnit:SourceUnit

the source unit (of the file being compiled)

compilationUnit:CompilationUnit

the compilation unit (of the file being compiled)

mn:MethodNode

the method node

Returns:ClassNode[]

a class node if it could be parsed and resolved, null otherwise