public <T,R> List<R> doSomething(List<T> source, Closure<R> consumer)
The problem this annotation tries to solve is to define the expected parameter types of the
consumer closure. The generics type defined in Closure<R> correspond to the
result type of the closure, but tell nothing about what the closure must accept as arguments.
There's no way in Java or Groovy to express the type signature of the expected closure call method from outside the closure itself, so we rely on an annotation here. Unfortunately, annotations also have limitations (like not being able to use generics placeholder as annotation values) that prevent us from expressing the type directly.
Additionally, closures are polymorphic. This means that a single closure can be used with different, valid,
parameter signatures. A typical use case can be found when a closure accepts either a java.
or a (key,value) pair, like the org.
method.
For those reasons, the ClosureParams annotation takes these arguments:
ClosureParams#value() defines a groovy.transform.stc.ClosureSignatureHint hint class
that the compiler will use to infer the parameter typesClosureParams#conflictResolutionStrategy() defines a groovy.transform.stc.ClosureSignatureConflictResolver resolver
class that the compiler will use to potentially reduce ambiguities remaining after initial inference calculationsClosureParams#options(), a set of options that are passed to the hint when the type is inferred (and also available to the resolver)As a result, the previous signature can be written like this:
public <T,R> List<R> doSomething(List<T> source, @ClosureParams(FirstParam.FirstGenericType.class) Closure<R> consumer)
Which uses the FirstParam. first generic type of the first argument
| Modifier and Type | Method and Description |
|---|---|
| public Class | |
| public String[] | |
| public Class |
| conflictResolutionStrategy | back to summary |
|---|---|
| public Class | |
| options | back to summary |
|---|---|
| public String[] options() | |
| value | back to summary |
|---|---|
| public Class | |