Java Code Examples for org.codehaus.groovy.ast.expr.MethodCallExpression#getArguments()
The following examples show how to use
org.codehaus.groovy.ast.expr.MethodCallExpression#getArguments() .
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 |
@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 |
/** * 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: MethodCallExpressionTransformer.java From groovy with Apache License 2.0 | 6 votes |
/** * Identifies a method call expression on {@link DefaultGroovyMethods#is(Object, Object)} and if recognized, transforms it into a {@link CompareIdentityExpression}. * @param call a method call to be transformed * @return null if the method call is not DGM#is, or {@link CompareIdentityExpression} */ private static Expression tryTransformIsToCompareIdentity(MethodCallExpression call) { if (call.isSafe()) return null; MethodNode methodTarget = call.getMethodTarget(); if (methodTarget instanceof ExtensionMethodNode && "is".equals(methodTarget.getName()) && methodTarget.getParameters().length==1) { methodTarget = ((ExtensionMethodNode) methodTarget).getExtensionMethodNode(); ClassNode owner = methodTarget.getDeclaringClass(); if (DGM_CLASSNODE.equals(owner)) { Expression args = call.getArguments(); if (args instanceof ArgumentListExpression) { ArgumentListExpression arguments = (ArgumentListExpression) args; List<Expression> exprs = arguments.getExpressions(); if (exprs.size() == 1) { CompareIdentityExpression cid = new CompareIdentityExpression(call.getObjectExpression(), exprs.get(0)); cid.setSourcePosition(call); return cid; } } } } return null; }
Example 4
Source File: MethodCallExpressionTransformer.java From groovy with Apache License 2.0 | 6 votes |
private static MethodCallExpression transformToMopSuperCall(final ClassNode superCallReceiver, final MethodCallExpression expr) { MethodNode mn = expr.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET); String mopName = MopWriter.getMopMethodName(mn, false); MethodNode direct = new MethodNode( mopName, ACC_PUBLIC | ACC_SYNTHETIC, mn.getReturnType(), mn.getParameters(), mn.getExceptions(), EmptyStatement.INSTANCE ); direct.setDeclaringClass(superCallReceiver); MethodCallExpression result = new MethodCallExpression( new VariableExpression("this"), mopName, expr.getArguments() ); result.setImplicitThis(true); result.setSpreadSafe(false); result.setSafe(false); result.setSourcePosition(expr); result.setMethodTarget(direct); return result; }
Example 5
Source File: TraitReceiverTransformer.java From groovy with Apache License 2.0 | 6 votes |
private Expression transformMethodCallOnThis(final MethodCallExpression call) { Expression method = call.getMethod(); Expression arguments = call.getArguments(); if (method instanceof ConstantExpression) { String methodName = method.getText(); List<MethodNode> methods = traitClass.getMethods(methodName); for (MethodNode methodNode : methods) { if (methodName.equals(methodNode.getName()) && methodNode.isPrivate()) { if (inClosure) { return transformPrivateMethodCallOnThisInClosure(call, arguments, methodName); } return transformPrivateMethodCallOnThis(call, arguments, methodName); } } } if (inClosure) { return transformMethodCallOnThisInClosure(call); } return transformMethodCallOnThisFallBack(call, method, arguments); }
Example 6
Source File: SuperCallTraitTransformer.java From groovy with Apache License 2.0 | 5 votes |
private Expression transformMethodCallExpression(final MethodCallExpression exp) { if (isTraitSuperPropertyExpression(exp.getObjectExpression())) { Expression objectExpression = exp.getObjectExpression(); ClassNode traitReceiver = ((PropertyExpression) objectExpression).getObjectExpression().getType(); if (traitReceiver != null) { // (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this) ClassExpression receiver = new ClassExpression( getHelper(traitReceiver) ); ArgumentListExpression newArgs = new ArgumentListExpression(); Expression arguments = exp.getArguments(); newArgs.addExpression(new VariableExpression("this")); if (arguments instanceof TupleExpression) { List<Expression> expressions = ((TupleExpression) arguments).getExpressions(); for (Expression expression : expressions) { newArgs.addExpression(transform(expression)); } } else { newArgs.addExpression(transform(arguments)); } MethodCallExpression result = new MethodCallExpression( receiver, transform(exp.getMethod()), newArgs ); result.setImplicitThis(false); result.setSpreadSafe(exp.isSpreadSafe()); result.setSafe(exp.isSafe()); result.setSourcePosition(exp); return result; } } return super.transform(exp); }
Example 7
Source File: GroovyGradleParser.java From size-analyzer with Apache License 2.0 | 5 votes |
@Override public void visitMethodCallExpression(MethodCallExpression expression) { // get initial parent, parentParent values. String parent = methodCallStack.size() < 1 ? "" : Iterables.getLast(methodCallStack).getMethodAsString(); String parentParent = getParentParent(); methodCallStack.add(expression); if (expression.getArguments() instanceof ArgumentListExpression) { Expression objectExpression = expression.getObjectExpression(); String newParent = getValidParentString(objectExpression); if (newParent != null) { parentParent = parent; parent = newParent; if (objectExpression instanceof PropertyExpression) { String newParentParent = getValidParentString(((PropertyExpression) objectExpression).getObjectExpression()); if (newParentParent != null) { parentParent = newParentParent; } } } checkDslProperty(expression, parent, parentParent); } super.visitMethodCallExpression(expression); methodCallStack.remove(methodCallStack.size() - 1); }
Example 8
Source File: InvocationWriter.java From groovy with Apache License 2.0 | 5 votes |
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 9
Source File: MacroCallTransformingVisitor.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitMethodCallExpression(MethodCallExpression call) { super.visitMethodCallExpression(call); final List<Expression> callArguments; if (call.getArguments() instanceof TupleExpression) { callArguments = ((TupleExpression) call.getArguments()).getExpressions(); } else { callArguments = Collections.singletonList(call.getArguments()); } List<MethodNode> macroMethods = findMacroMethods(call.getMethodAsString(), callArguments); if (macroMethods.isEmpty()) { // Early return to avoid macro context and arguments creation return; } MacroContext macroContext = new MacroContext(unit, sourceUnit, call); Object[] macroArguments = new Object[callArguments.size() + 1]; macroArguments[0] = macroContext; System.arraycopy(callArguments.toArray(), 0, macroArguments, 1, callArguments.size()); for (MethodNode macroMethodNode : macroMethods) { if (!(macroMethodNode instanceof ExtensionMethodNode)) { throw new IllegalStateException(macroMethodNode + " is not an instance of ExtensionMethodNode"); } if (tryMacroMethod(call, (ExtensionMethodNode) macroMethodNode, macroArguments)) { break; } } }
Example 10
Source File: AstBuilderTransformation.java From groovy with Apache License 2.0 | 5 votes |
/** * Looks for method calls on the AstBuilder class called build that take * a Closure as parameter. This is all needed b/c build is overloaded. * * @param call the method call expression, may not be null */ @Override protected boolean isBuildInvocation(MethodCallExpression call) { if (call == null) throw new IllegalArgumentException("Null: call"); // is method name correct? if (call.getMethod() instanceof ConstantExpression && "buildFromCode".equals(((ConstantExpression) call.getMethod()).getValue())) { // is method object correct type? if (call.getObjectExpression() != null && call.getObjectExpression().getType() != null) { String name = call.getObjectExpression().getType().getName(); if (name != null && !name.isEmpty() && factoryTargets.contains(name)) { // is one of the arguments a closure? if (call.getArguments() != null && call.getArguments() instanceof TupleExpression) { if (((TupleExpression) call.getArguments()).getExpressions() != null) { for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) { if (node instanceof ClosureExpression) { return true; } } } } } } } return false; }
Example 11
Source File: AstBuilderTransformation.java From groovy with Apache License 2.0 | 5 votes |
private static ClosureExpression getClosureArgument(MethodCallExpression call) { if (call.getArguments() instanceof TupleExpression) { for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) { if (node instanceof ClosureExpression) { return (ClosureExpression) node; } } } return null; }
Example 12
Source File: AstBuilderTransformation.java From groovy with Apache License 2.0 | 5 votes |
private static List<Expression> getNonClosureArguments(MethodCallExpression call) { List<Expression> result = new ArrayList<>(); if (call.getArguments() instanceof TupleExpression) { for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) { if (!(node instanceof ClosureExpression)) { result.add((Expression) node); } } } return result; }
Example 13
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 5 votes |
public void visitMethodCallExpression(MethodCallExpression mce) { super.visitMethodCallExpression(mce); Expression aexp = mce.getArguments(); if (aexp instanceof TupleExpression) { TupleExpression arguments = (TupleExpression) aexp; for (Expression e : arguments.getExpressions()) { checkForInvalidDeclaration(e); } } else { checkForInvalidDeclaration(aexp); } }
Example 14
Source File: GroovyClassFilterTransformer.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected Expression transformMethodCall(MethodCallExpression originalCall, Expression transformedObject, Expression transformedMethod, Expression transformedArgs) { Expression originalObject = originalCall.getObjectExpression(); if (allowed(originalObject.getType())) { Expression originalMethod = originalCall.getMethod(); Expression originalArgs = originalCall.getArguments(); Expression unwrappedArgs = unwrapTransformedArguments(transformedArgs, originalArgs); if (unwrappedArgs != null) { if (transformedObject.equals(originalObject) && transformedMethod.equals(originalMethod) && unwrappedArgs.equals(originalArgs)) { if (log.isDebugEnabled()) { log.debug("allowed method call " + originalCall); } return originalCall; } MethodCallExpression transformedCall = new MethodCallExpression( transformedObject, transformedMethod, unwrappedArgs); transformedCall.setSafe(originalCall.isSafe()); transformedCall.setSpreadSafe(originalCall.isSpreadSafe()); if (log.isDebugEnabled()) { log.debug("transformed method call " + transformedCall); } return transformedCall; } } return super.transformMethodCall(originalCall, transformedObject, transformedMethod, transformedArgs); }
Example 15
Source File: MethodRefactoringElement.java From netbeans with Apache License 2.0 | 5 votes |
private List<String> getParamTypes(MethodCallExpression methodCall) { final Expression arguments = methodCall.getArguments(); final List<String> paramTypes = new ArrayList<String>(); if (arguments instanceof ArgumentListExpression) { ArgumentListExpression argumentList = ((ArgumentListExpression) arguments); if (argumentList.getExpressions().size() > 0) { for (Expression argument : argumentList.getExpressions()) { paramTypes.add(ElementUtils.getTypeName(argument.getType())); } } } return paramTypes; }
Example 16
Source File: FindMethodUtils.java From netbeans with Apache License 2.0 | 5 votes |
/** * 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 17
Source File: Methods.java From netbeans with Apache License 2.0 | 5 votes |
private static List<String> getMethodParams(MethodCallExpression methodCall) { final List<String> params = new ArrayList<>(); final Expression arguments = methodCall.getArguments(); if (arguments instanceof ArgumentListExpression) { ArgumentListExpression argumentList = ((ArgumentListExpression) arguments); if (argumentList.getExpressions().size() > 0) { for (Expression argument : argumentList.getExpressions()) { params.add(ElementUtils.getTypeName(argument.getType())); } } } return params; }
Example 18
Source File: Methods.java From netbeans with Apache License 2.0 | 5 votes |
/** * Tries to calculate number of method parameters. * * @param methodCall called method * @return number of method parameters, * 1 in case of named parameters represented by map, * or -1 if it is unknown */ private static int getParameterCount(MethodCallExpression methodCall) { Expression expression = methodCall.getArguments(); if (expression instanceof ArgumentListExpression) { return ((ArgumentListExpression) expression).getExpressions().size(); } else if (expression instanceof NamedArgumentListExpression) { // this is in fact map acting as named parameters // lets return size 1 return 1; } else { return -1; } }
Example 19
Source File: PluginsAndBuildscriptTransformer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private ScriptBlock detectScriptBlock(Statement statement) { if (!(statement instanceof ExpressionStatement)) { return null; } ExpressionStatement expressionStatement = (ExpressionStatement) statement; if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) { return null; } MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression(); if (!AstUtils.targetIsThis(methodCall)) { return null; } if (!(methodCall.getMethod() instanceof ConstantExpression)) { return null; } String methodName = methodCall.getMethod().getText(); if (methodName.equals(PLUGINS) || methodName.equals(classpathBlockName)) { if (!(methodCall.getArguments() instanceof ArgumentListExpression)) { return null; } ArgumentListExpression args = (ArgumentListExpression) methodCall.getArguments(); if (args.getExpressions().size() == 1 && args.getExpression(0) instanceof ClosureExpression) { return new ScriptBlock(methodName, (ClosureExpression) args.getExpression(0)); } else { return null; } } else { return null; } }
Example 20
Source File: MarkupBuilderCodeTransformer.java From groovy with Apache License 2.0 | 4 votes |
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); }