Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Type | Name and description |
---|---|
protected ClassNode |
findClassNode(SourceUnit sourceUnit, CompilationUnit compilationUnit, String className) Finds a class node given a string representing the type. |
List<ClassNode> |
getClosureSignatures(MethodNode node, SourceUnit sourceUnit, CompilationUnit compilationUnit, String[] options, ASTNode usage) |
static ClassNode |
pickGenericType(ClassNode type, int gtIndex) A helper method which will extract the n-th generic type from a class node. |
static ClassNode |
pickGenericType(MethodNode node, int parameterIndex, int gtIndex) A helper method which will extract the n-th generic type from the n-th parameter of a method node. |
Finds a class node given a string representing the type. Performs a lookup in the compilation unit to check if it is done in the same source unit.
sourceUnit
- source unitcompilationUnit
- compilation unitclassName
- the name of the class we want to get a ClassNode for
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 with 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 DefaultGroovyMethods.collect 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.
node
- the method node for which a Closure parameter was annotated with
ClosureParamssourceUnit
- the source unit of the file being compiledcompilationUnit
- the compilation unit of the file being compiledoptions
- the options, corresponding to the ClosureParams.options found on the annotationusage
- the AST node, in the compiled file, which triggered a call to this method. Normally only used for logging/error handlingA helper method which will extract the n-th generic type from a class node.
type
- the class node from which to pick a generic typegtIndex
- the index of the generic type to extractA helper method which will extract the n-th generic type from the n-th parameter of a method node.
node
- the method node from which the generic type should be pickedparameterIndex
- the index of the parameter in the method parameter listgtIndex
- the index of the generic type to extract