Search in sources :

Example 1 with LowestUpperBoundClassNode

use of org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode in project groovy-core by groovy.

the class StaticTypeCheckingVisitor method adjustWithTraits.

/**
 * A special method handling the "withTrait" call for which the type checker knows more than
 * what the type signature is able to tell. If "withTrait" is detected, then a new class node
 * is created representing the list of trait interfaces.
 *
 * @param directMethodCallCandidate a method selected by the type checker
 * @param receiver the receiver of the method call
 *@param args the arguments of the method call
 * @param returnType the original return type, as inferred by the type checker   @return fixed return type if the selected method is {@link org.codehaus.groovy.runtime.DefaultGroovyMethods#withTraits(Object, Class[]) withTraits}
 */
private ClassNode adjustWithTraits(final MethodNode directMethodCallCandidate, final ClassNode receiver, final ClassNode[] args, final ClassNode returnType) {
    if (directMethodCallCandidate instanceof ExtensionMethodNode) {
        ExtensionMethodNode emn = (ExtensionMethodNode) directMethodCallCandidate;
        if ("withTraits".equals(emn.getName()) && "DefaultGroovyMethods".equals(emn.getExtensionMethodNode().getDeclaringClass().getNameWithoutPackage())) {
            List<ClassNode> nodes = new LinkedList<ClassNode>();
            Collections.addAll(nodes, receiver.getInterfaces());
            for (ClassNode arg : args) {
                if (isClassClassNodeWrappingConcreteType(arg)) {
                    nodes.add(arg.getGenericsTypes()[0].getType());
                } else {
                    nodes.add(arg);
                }
            }
            return new LowestUpperBoundClassNode(returnType.getName() + "Composed", OBJECT_TYPE, nodes.toArray(new ClassNode[nodes.size()]));
        }
    }
    return returnType;
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)1 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)1