private void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
    cNode.addConstructor(constructorNode);
    // GROOVY-5814: Immutable is not compatible with @CompileStatic
    Parameter argsParam = null;
    for (Parameter p : constructorNode.getParameters()) {
      if ("args".equals(p.getName())) {
        argsParam = p;
        break;
      }
    }
    if (argsParam != null) {
      final Parameter arg = argsParam;
      ClassCodeVisitorSupport variableExpressionFix =
          new ClassCodeVisitorSupport() {
            @Override
            protected SourceUnit getSourceUnit() {
              return cNode.getModule().getContext();
            }

            @Override
            public void visitVariableExpression(final VariableExpression expression) {
              super.visitVariableExpression(expression);
              if ("args".equals(expression.getName())) {
                expression.setAccessedVariable(arg);
              }
            }
          };
      variableExpressionFix.visitConstructor(constructorNode);
    }
  }
 private void declare(Parameter[] parameters, ASTNode node) {
   for (Parameter parameter : parameters) {
     if (parameter.hasInitialExpression()) {
       parameter.getInitialExpression().visit(this);
     }
     declare(parameter, node);
   }
 }
 public void visitCatchStatement(CatchStatement statement) {
   pushState();
   Parameter p = statement.getVariable();
   p.setInStaticContext(currentScope.isInStaticContext());
   declare(p, statement);
   super.visitCatchStatement(statement);
   popState();
 }
 private void memorizeInitialExpressions(final MethodNode node) {
   // add node metadata for default parameters because they are erased by the Verifier
   if (node.getParameters() != null) {
     for (Parameter parameter : node.getParameters()) {
       parameter.putNodeMetaData(
           StaticTypesMarker.INITIAL_EXPRESSION, parameter.getInitialExpression());
     }
   }
 }
 public void visitForLoop(ForStatement forLoop) {
   pushState();
   forLoop.setVariableScope(currentScope);
   Parameter p = forLoop.getVariable();
   p.setInStaticContext(currentScope.isInStaticContext());
   if (p != ForStatement.FOR_LOOP_DUMMY) declare(p, forLoop);
   super.visitForLoop(forLoop);
   popState();
 }
  private Statement makeMethodBody(
      ClassNode classNode, MethodSignature methodSignature, Parameter[] parameters) {
    String invokeMethod = methodSignature.isStatic() ? "invokeStatic" : "invokeInstance";
    Expression[] args = new Expression[parameters.length + 2];
    args[0] =
        methodSignature.isStatic()
            ? new ClassExpression(classNode)
            : VariableExpression.THIS_EXPRESSION;
    args[1] = new ConstantExpression(methodSignature.getMethodName());
    int i = 2;
    for (Parameter parameter : parameters) {
      args[i++] = new VariableExpression(parameter.getName());
    }

    return returns(call(getDomainHandlerMethod(classNode), invokeMethod, args(args)));
  }
  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();
  }
Пример #8
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;
  }