Java Code Examples for org.codehaus.groovy.ast.expr.MethodCallExpression#getMethodAsString()

The following examples show how to use org.codehaus.groovy.ast.expr.MethodCallExpression#getMethodAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@Override
public void visitTupleExpression(TupleExpression tupleExpression) {
  if (!methodCallStack.isEmpty()) {
    MethodCallExpression call = Iterables.getLast(methodCallStack);
    if (call.getArguments() == tupleExpression) {
      String parent = call.getMethodAsString();
      String parentParent = getParentParent();
      if (!(tupleExpression instanceof ArgumentListExpression)) {
        Map<String, String> namedArguments = new HashMap<>();
        for (Expression subExpr : tupleExpression.getExpressions()) {
          if (subExpr instanceof NamedArgumentListExpression) {
            NamedArgumentListExpression nale = (NamedArgumentListExpression) subExpr;
            for (MapEntryExpression mae : nale.getMapEntryExpressions()) {
              namedArguments.put(
                  mae.getKeyExpression().getText(), mae.getValueExpression().getText());
            }
          }
        }
        checkMethodCall(parent, parentParent, namedArguments);
      }
    }
  }
  super.visitTupleExpression(tupleExpression);
}
 
Example 2
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
/**
 * This will return an initial guess as to the string representation of the parent parent object,
 * based solely on the method callstack hierarchy. Any direct property or variable parents should
 * be resolved by using the getValidStringRepresentation function.
 */
private String getParentParent() {
  for (int i = methodCallStack.size() - 2; i >= 0; i--) {
    MethodCallExpression expression = methodCallStack.get(i);
    Expression arguments = expression.getArguments();
    if (arguments instanceof ArgumentListExpression) {
      ArgumentListExpression ale = (ArgumentListExpression) arguments;
      List<Expression> expressions = ale.getExpressions();
      if (expressions.size() == 1 && expressions.get(0) instanceof ClosureExpression) {
        return expression.getMethodAsString();
      }
    }
  }

  return null;
}
 
Example 3
Source File: GeneralUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static boolean copyStatementsWithSuperAdjustment(final ClosureExpression pre, final BlockStatement body) {
    Statement preCode = pre.getCode();
    boolean changed = false;
    if (preCode instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) preCode;
        List<Statement> statements = block.getStatements();
        for (int i = 0, n = statements.size(); i < n; i += 1) {
            Statement statement = statements.get(i);
            // adjust the first statement if it's a super call
            if (i == 0 && statement instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) statement;
                Expression preExp = es.getExpression();
                if (preExp instanceof MethodCallExpression) {
                    MethodCallExpression mce = (MethodCallExpression) preExp;
                    String name = mce.getMethodAsString();
                    if ("super".equals(name)) {
                        es.setExpression(new ConstructorCallExpression(ClassNode.SUPER, mce.getArguments()));
                        changed = true;
                    }
                }
            }
            body.addStatement(statement);
        }
    }
    return changed;
}
 
Example 4
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 5 votes vote down vote up
/** Handles a groovy BinaryExpression such as foo = true, or bar.baz.foo = true. */
@Override
public void visitBinaryExpression(BinaryExpression binaryExpression) {
  if (!methodCallStack.isEmpty()) {
    MethodCallExpression call = Iterables.getLast(methodCallStack);
    String parent = call.getMethodAsString();
    String parentParent = getParentParent();
    Expression leftExpression = binaryExpression.getLeftExpression();
    Expression rightExpression = binaryExpression.getRightExpression();
    if (rightExpression instanceof ConstantExpression
        && (leftExpression instanceof PropertyExpression
            || leftExpression instanceof VariableExpression)) {
      String value = rightExpression.getText();
      String property = "";
      if (leftExpression instanceof PropertyExpression) {
        Expression leftPropertyExpression = ((PropertyExpression) leftExpression).getProperty();
        if (!(leftPropertyExpression instanceof ConstantExpression)) {
          return;
        }
        property = ((ConstantExpression) leftPropertyExpression).getText();
        Expression leftObjectExpression =
            ((PropertyExpression) leftExpression).getObjectExpression();
        parentParent = parent;
        parent = getValidParentString(leftObjectExpression);
        if (leftObjectExpression instanceof PropertyExpression) {
          parentParent =
              getValidParentString(
                  ((PropertyExpression) leftObjectExpression).getObjectExpression());
        }
      } else {
        property = ((VariableExpression) leftExpression).getName();
      }
      checkDslPropertyAssignment(
          property, value, parent, parentParent, binaryExpression.getLineNumber());
    }
  }
  super.visitBinaryExpression(binaryExpression);
}
 
Example 5
Source File: Methods.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isSameMethod(MethodCallExpression methodCall1, MethodCallExpression methodCall2) {
    String method1 = methodCall1.getMethodAsString();
    if (method1 != null && method1.equals(methodCall2.getMethodAsString())) {
        int size1 = getParameterCount(methodCall1);
        int size2 = getParameterCount(methodCall2);
        // not comparing parameter types for now, only their count
        // is it even possible to make some check for parameter types?
        if (size1 >= 0 && size1 == size2) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: FindMethodUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Find and add method usage if the given type contains method corresponding
 * with the given method call. In other words we are looking for the method
 * declaration in the given type and the method we are looking for is based
 * on given method call. Might return null if the type can't be interfered
 * properly and thus we are not able to find correct method - this is typical
 * for dynamic types.
 *
 * @param type where we are looking for the specific method
 * @param methodCall method call for which we want to find method in given
 * type or null if the type is dynamic
 */
private static MethodNode findMethod(ClassNode type, MethodCallExpression methodCall) {
    String findingMethod = methodCall.getMethodAsString();
    Expression arguments = methodCall.getArguments();
    
    if (!type.isResolved()) {
        type = type.redirect();
    }

    MethodNode method = type.tryFindPossibleMethod(findingMethod, arguments);
    if (method != null) {
        return method;
    }
    return findMostAccurateMethod(methodCall, type.getMethods(findingMethod));
}
 
Example 7
Source File: FindRepositoriesVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
    if (null != call.getMethodAsString()) {
        switch (call.getMethodAsString()) {
            case "maven":
                call.getArguments().visit(new MavenVisitor());
                break;
            case "ivy":
                call.getArguments().visit(new IvyVisitor());
                break;
            case "mavenCentral":
                MavenCentralRepository centralRepository = new MavenCentralRepository();
                if (!repositories.contains(centralRepository)) {
                    repositories.add(centralRepository);
                }
                break;
            case "jcenter":
                JCenterRepository centerRepository = new JCenterRepository();
                if (!repositories.contains(centerRepository)) {
                    repositories.add(centerRepository);
                }
                break;
            default:
                break;
        }
    }
}
 
Example 8
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformSuperMethodCall(final MethodCallExpression call) {
    String method = call.getMethodAsString();
    if (method == null) {
        throwSuperError(call);
    }

    Expression arguments = transform(call.getArguments());
    ArgumentListExpression superCallArgs = new ArgumentListExpression();
    if (arguments instanceof ArgumentListExpression) {
        ArgumentListExpression list = (ArgumentListExpression) arguments;
        for (Expression expression : list) {
            superCallArgs.addExpression(expression);
        }
    } else {
        superCallArgs.addExpression(arguments);
    }
    MethodCallExpression transformed = new MethodCallExpression(
            weaved,
            Traits.getSuperTraitMethodName(traitClass, method),
            superCallArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(false);
    return transformed;
}
 
Example 9
Source File: InvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean isClosureCall(final MethodCallExpression call) {
    // are we a local variable?
    // it should not be an explicitly "this" qualified method call
    // and the current class should have a possible method
    ClassNode classNode = controller.getClassNode();
    String methodName = call.getMethodAsString();
    if (methodName == null) return false;
    if (!call.isImplicitThis()) return false;
    if (!AsmClassGenerator.isThisExpression(call.getObjectExpression())) return false;
    FieldNode field = classNode.getDeclaredField(methodName);
    if (field == null) return false;
    if (isStaticInvocation(call) && !field.isStatic()) return false;
    Expression arguments = call.getArguments();
    return !classNode.hasPossibleMethod(methodName, arguments);
}
 
Example 10
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression transformMethodCall(final MethodCallExpression exp) {
    String name = exp.getMethodAsString();
    if (exp.isImplicitThis() && "include".equals(name)) {
        return tryTransformInclude(exp);
    } else if (exp.isImplicitThis() && name.startsWith(":")) {
        List<Expression> args;
        if (exp.getArguments() instanceof ArgumentListExpression) {
            args = ((ArgumentListExpression) exp.getArguments()).getExpressions();
        } else {
            args = Collections.singletonList(exp.getArguments());
        }
        Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args)));
        MethodCallExpression call = new MethodCallExpression(
                new VariableExpression("this"),
                "methodMissing",
                newArguments
        );
        call.setImplicitThis(true);
        call.setSafe(exp.isSafe());
        call.setSpreadSafe(exp.isSpreadSafe());
        call.setSourcePosition(exp);
        return call;
    } else if (name!=null && name.startsWith("$")) {
        MethodCallExpression reformatted = new MethodCallExpression(
                exp.getObjectExpression(),
                name.substring(1),
                exp.getArguments()
        );
        reformatted.setImplicitThis(exp.isImplicitThis());
        reformatted.setSafe(exp.isSafe());
        reformatted.setSpreadSafe(exp.isSpreadSafe());
        reformatted.setSourcePosition(exp);
        // wrap in a stringOf { ... } closure call
        ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted));
        clos.setVariableScope(new VariableScope());
        MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"),
                "stringOf",
                clos);
        stringOf.setImplicitThis(true);
        stringOf.setSourcePosition(reformatted);
        return stringOf;
    }
    return super.transform(exp);
}
 
Example 11
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 3 votes vote down vote up
/**
 * This will evaluate the dsl property assignment, to store the valid value. In
 * android.defaultConfig.minSdkVersion 15, "minSdkVersion 15" is the methodCall, defaultConfig is
 * the parent, and android is the parentParent expression. This can also be written as android {
 * defaultConfig { minSdkVersion 15 } } in the build.gradle file as well.
 *
 * @param call is a the method call expression.
 * @param parent is the string representation for the parent object.
 * @param parentParent is the string representation for the parent of the parent object.
 */
private void checkDslProperty(MethodCallExpression call, String parent, String parentParent) {
  String property = call.getMethodAsString();
  if (property == null) {
    return;
  }
  String value = getText(call.getArguments(), content);
  checkDslPropertyAssignment(property, value, parent, parentParent, call.getLineNumber());
}