Search in sources :

Example 1 with SpreadExpression

use of org.codehaus.groovy.ast.expr.SpreadExpression in project groovy by apache.

the class ClassNodeUtils method hasPossibleStaticMethod.

/**
     * Returns true if the given method has a possibly matching static method with the given name and arguments.
     *
     * @param cNode     the ClassNode of interest
     * @param name      the name of the method of interest
     * @param arguments the arguments to match against
     * @param trySpread whether to try to account for SpreadExpressions within the arguments
     * @return true if a matching method was found
     */
public static boolean hasPossibleStaticMethod(ClassNode cNode, String name, Expression arguments, boolean trySpread) {
    int count = 0;
    boolean foundSpread = false;
    if (arguments instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) arguments;
        for (Expression arg : tuple.getExpressions()) {
            if (arg instanceof SpreadExpression) {
                foundSpread = true;
            } else {
                count++;
            }
        }
    } else if (arguments instanceof MapExpression) {
        count = 1;
    }
    for (MethodNode method : cNode.getMethods(name)) {
        if (method.isStatic()) {
            Parameter[] parameters = method.getParameters();
            // do fuzzy match for spread case: count will be number of non-spread args
            if (trySpread && foundSpread && parameters.length >= count)
                return true;
            if (parameters.length == count)
                return true;
            // handle varargs case
            if (parameters.length > 0 && parameters[parameters.length - 1].getType().isArray()) {
                if (count >= parameters.length - 1)
                    return true;
                // fuzzy match any spread to a varargs
                if (trySpread && foundSpread)
                    return true;
            }
            // handle parameters with default values
            int nonDefaultParameters = 0;
            for (Parameter parameter : parameters) {
                if (!parameter.hasInitialExpression()) {
                    nonDefaultParameters++;
                }
            }
            if (count < parameters.length && nonDefaultParameters <= count) {
                return true;
            }
        // TODO handle spread with nonDefaultParams?
        }
    }
    return false;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Parameter(org.codehaus.groovy.ast.Parameter) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression)

Aggregations

MethodNode (org.codehaus.groovy.ast.MethodNode)1 Parameter (org.codehaus.groovy.ast.Parameter)1 Expression (org.codehaus.groovy.ast.expr.Expression)1 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)1 SpreadExpression (org.codehaus.groovy.ast.expr.SpreadExpression)1 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)1