public void visitClosureExpression(ClosureExpression expression) {
    pushState();

    expression.setVariableScope(currentScope);

    if (expression.isParameterSpecified()) {
      Parameter[] parameters = expression.getParameters();
      for (Parameter parameter : parameters) {
        parameter.setInStaticContext(currentScope.isInStaticContext());
        if (parameter.hasInitialExpression()) {
          parameter.getInitialExpression().visit(this);
        }
        declare(parameter, expression);
      }
    } else if (expression.getParameters() != null) {
      Parameter var = new Parameter(ClassHelper.OBJECT_TYPE, "it");
      var.setInStaticContext(currentScope.isInStaticContext());
      currentScope.putDeclaredVariable(var);
    }

    super.visitClosureExpression(expression);
    markClosureSharedVariables();

    popState();
  }
 private void declare(Parameter[] parameters, ASTNode node) {
   for (Parameter parameter : parameters) {
     if (parameter.hasInitialExpression()) {
       parameter.getInitialExpression().visit(this);
     }
     declare(parameter, node);
   }
 }
Пример #3
0
  /**
   * Returns true if the given method has a possibly matching static method with the given name and
   * arguments.
   *
   * @param name the name of the method of interest
   * @param arguments the arguments to match against
   * @return true if a matching method was found
   */
  public boolean hasPossibleStaticMethod(String name, Expression arguments) {
    int count = 0;

    if (arguments instanceof TupleExpression) {
      TupleExpression tuple = (TupleExpression) arguments;
      // TODO this won't strictly be true when using list expansion in argument calls
      count = tuple.getExpressions().size();
    } else if (arguments instanceof MapExpression) {
      count = 1;
    }

    for (MethodNode method : getMethods(name)) {
      if (method.isStatic()) {
        Parameter[] parameters = method.getParameters();
        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;
        }

        // handle parameters with default values
        int nonDefaultParameters = 0;
        for (Parameter parameter : parameters) {
          if (!parameter.hasInitialExpression()) {
            nonDefaultParameters++;
          }
        }

        if (count < parameters.length && nonDefaultParameters <= count) {
          return true;
        }
      }
    }
    return false;
  }