Example #1
0
  public MethodNode tryFindPossibleMethod(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 return null;

    MethodNode res = null;
    ClassNode node = this;
    TupleExpression args = (TupleExpression) arguments;
    do {
      for (MethodNode method : node.getMethods(name)) {
        if (method.getParameters().length == count) {
          boolean match = true;
          for (int i = 0; i != count; ++i)
            if (!args.getType().isDerivedFrom(method.getParameters()[i].getType())) {
              match = false;
              break;
            }

          if (match) {
            if (res == null) res = method;
            else {
              if (res.getParameters().length != count) return null;
              if (node.equals(this)) return null;

              match = true;
              for (int i = 0; i != count; ++i)
                if (!res.getParameters()[i].getType().equals(method.getParameters()[i].getType())) {
                  match = false;
                  break;
                }
              if (!match) return null;
            }
          }
        }
      }
      node = node.getSuperClass();
    } while (node != null);

    return res;
  }
Example #2
0
  /**
   * Returns true if the given method has a possibly matching instance 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 hasPossibleMethod(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();
    }
    ClassNode node = this;
    do {
      for (MethodNode method : getMethods(name)) {
        if (method.getParameters().length == count && !method.isStatic()) {
          return true;
        }
      }
      node = node.getSuperClass();
    } while (node != null);
    return false;
  }
 protected Expression transformConstructorCallExpression(ConstructorCallExpression cce) {
   inSpecialConstructorCall = cce.isSpecialCall();
   Expression expression = cce.getArguments();
   if (expression instanceof TupleExpression) {
     TupleExpression tuple = (TupleExpression) expression;
     if (tuple.getExpressions().size() == 1) {
       expression = tuple.getExpression(0);
       if (expression instanceof NamedArgumentListExpression) {
         NamedArgumentListExpression namedArgs = (NamedArgumentListExpression) expression;
         List<MapEntryExpression> entryExpressions = namedArgs.getMapEntryExpressions();
         for (int i = 0; i < entryExpressions.size(); i++) {
           entryExpressions.set(
               i,
               (MapEntryExpression)
                   transformMapEntryExpression(entryExpressions.get(i), cce.getType()));
         }
       }
     }
   }
   Expression ret = cce.transformExpression(this);
   inSpecialConstructorCall = false;
   return ret;
 }
Example #4
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;
  }