private void doVisitMethodCall(Expression node) { String methodName = AstUtil.getMethodName(node); if (methodName != null && methodName.startsWith(EXPRESSION_MARKER_PREFIX)) { ArgumentListExpression args = (ArgumentListExpression) AstUtil.getArguments(node); if (args != null && args.getExpressions().size() == 1) addNode(expressions, methodName.substring(EXPRESSION_MARKER_PREFIX.length()), args.getExpressions().get(0)); } }
/** * Called on method call checks in order to determine if a method call corresponds to the * idiomatic o.with { ... } structure * * @param name name of the method called * @param callArguments arguments of the method * @return true if the name is "with" and arguments consist of a single closure */ public static boolean isWithCall(final String name, final Expression callArguments) { boolean isWithCall = "with".equals(name) && callArguments instanceof ArgumentListExpression; if (isWithCall) { ArgumentListExpression argList = (ArgumentListExpression) callArguments; List<Expression> expressions = argList.getExpressions(); isWithCall = expressions.size() == 1 && expressions.get(0) instanceof ClosureExpression; } return isWithCall; }
protected void checkForbiddenSpreadArgument(ArgumentListExpression argumentList) { for (Expression arg : argumentList.getExpressions()) { if (arg instanceof SpreadExpression) { addStaticTypeError("The spread operator cannot be used as argument of method or closure calls with static type checking because the number of arguments cannot be determined at compile time", arg); } } }
@SuppressWarnings("unchecked") private void addPositionalArgs(ArgumentListExpression args) { List<Expression> expressions = args.getExpressions(); if (expressions.size() > 0 && expressions.get(0) instanceof MapExpression) { boolean isMixed = expressions.size() > 1; addNamedArgs((MapExpression) expressions.get(0), isMixed); if (isMixed) { addPositionalListArgs(expressions.subList(1, expressions.size()), true); } } else { addPositionalListArgs(expressions, false); } }
private static ClassNode getDeclaringClass(MethodNode method, Expression arguments) { ClassNode declaringClass = method.getDeclaringClass(); // correcting declaring class for extension methods: if (arguments instanceof ArgumentListExpression) { ArgumentListExpression al = (ArgumentListExpression) arguments; List<Expression> list = al.getExpressions(); if (list.isEmpty()) return declaringClass; Expression exp = list.get(0); ClassNode cn = exp.getNodeMetaData(ExtensionMethodDeclaringClass.class); if (cn != null) return cn; } return declaringClass; }
protected ClassNode[] getArgumentTypes(ArgumentListExpression args) { List<Expression> arglist = args.getExpressions(); ClassNode[] ret = new ClassNode[arglist.size()]; for (int i = 0; i < arglist.size(); i++) { Expression exp = arglist.get(i); if (isNullConstant(exp)) { ret[i] = UNKNOWN_PARAMETER_TYPE; } else { ret[i] = getInferredTypeFromTempInfo(exp, getType(exp)); } } return ret; }
public void visitArgumentlistExpression(ArgumentListExpression ale) { if (containsSpreadExpression(ale)) { despreadList(ale.getExpressions(), true); } else { visitTupleExpression(ale, true); } }
/** * if Class.forName(x) is recognized, make a direct method call */ protected boolean makeClassForNameCall(Expression origin, Expression receiver, Expression message, Expression arguments) { if (! (receiver instanceof ClassExpression)) return false; ClassExpression ce = (ClassExpression) receiver; if (!ClassHelper.CLASS_Type.equals(ce.getType())) return false; String msg = getMethodName(message); if (!"forName".equals(msg)) return false; ArgumentListExpression ae = makeArgumentList(arguments); if (ae.getExpressions().size()!=1) return false; return writeDirectMethodCall(CLASS_FOR_NAME_STRING,false, receiver, ae); }
private void rewriteArgumentList(MethodCallExpression call, ClassExpression objectExpression) { if (SpockRuntime.class.getName().equals(objectExpression.getType().getName())) { String methodName = call.getMethod().getText(); if (REWRITE_METHOD_ARGS.contains(methodName)) { List<Expression> arguments = new ArrayList<>(((ArgumentListExpression)call.getArguments()).getExpressions()); Expression expression = arguments.get(0); if (expression instanceof VariableExpression && isErrorCollectorExpression(((VariableExpression)expression))) { arguments.set(0, errorCollectorExpression()); } expression = arguments.get(1); if (expression instanceof VariableExpression && isValueRecorderExpression(((VariableExpression)expression))) { arguments.set(1, valueRecorderExpression()); } call.setArguments(new ArgumentListExpression(arguments)); } } }
@Override @SuppressWarnings("unchecked") public void visitArgumentlistExpression(ArgumentListExpression expr) { ArgumentListExpression conversion = new ArgumentListExpression( convertAll(expr.getExpressions())); conversion.setSourcePosition(expr); result = recordNa(conversion); }
public Expression transformExpression(ExpressionTransformer transformer) { Expression ret = new ArgumentListExpression(transformExpressions(getExpressions(), transformer)); ret.setSourcePosition(this); ret.copyNodeMetaData(this); return ret; }
@Override @SuppressWarnings("unchecked") public void visitArgumentlistExpression(ArgumentListExpression expr) { result = new ExpressionInfo( TextRegion.of(expr), TextPosition.startOf(expr), null, convertAll(expr.getExpressions()) ).setRelevant(false); }
private Expression transformMethodCall(MethodCallExpression mce, Expression argsExp) { ClassNode classType; if (isNewMethodStyle(mce)) { classType = mce.getObjectExpression().getType(); } else { classType = findMatchingCandidateClass(mce); } if (classType != null) { Expression argsToUse = argsExp; if (classType.getOuterClass() != null && ((classType.getModifiers() & org.objectweb.asm.Opcodes.ACC_STATIC) == 0)) { if (!(argsExp instanceof ArgumentListExpression)) { addError("Non-static inner constructor arguments must be an argument list expression; pass 'this' pointer explicitely as first constructor argument otherwise.", mce); return mce; } final ArgumentListExpression argsListExp = (ArgumentListExpression) argsExp; final List<Expression> argExpList = argsListExp.getExpressions(); final VariableExpression thisVarExp = new VariableExpression("this"); final List<Expression> expressionsWithThis = new ArrayList<Expression>(argExpList.size() + 1); expressionsWithThis.add(thisVarExp); expressionsWithThis.addAll(argExpList); argsToUse = new ArgumentListExpression(expressionsWithThis); } return new ConstructorCallExpression(classType, argsToUse); } // set the args as they might have gotten Newify transformed GROOVY-3491 mce.setArguments(argsExp); return mce; }
boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments); if (containsSpreadExpression) { controller.getAcg().despreadList(ae.getExpressions(), true); sig.append(getTypeDescription(Object[].class)); } else { for (Expression arg : ae.getExpressions()) { arg.visit(controller.getAcg()); if (arg instanceof CastExpression) {
/** * 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; } }
@Override public void visitConstructorCallExpression(final ConstructorCallExpression call) { super.visitConstructorCallExpression(call); MethodNode target = (MethodNode) call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET); if (target==null && call.getLineNumber()>0) { addError("Target constructor for constructor call expression hasn't been set", call); } else { if (target==null) { // try to find a target ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments()); List<Expression> expressions = argumentListExpression.getExpressions(); ClassNode[] args = new ClassNode[expressions.size()]; for (int i = 0; i < args.length; i++) { args[i] = typeChooser.resolveType(expressions.get(i), classNode); } MethodNode constructor = findMethodOrFail(call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args); call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, constructor); target = constructor; } } if (target!=null) { memorizeInitialExpressions(target); } }
protected void inferDiamondType(final ConstructorCallExpression cce, final ClassNode lType) { // check if constructor call expression makes use of the diamond operator ClassNode node = cce.getType(); if (node.isUsingGenerics() && node.getGenericsTypes() != null && node.getGenericsTypes().length == 0) { ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(cce.getArguments()); if (argumentListExpression.getExpressions().isEmpty()) { adjustGenerics(lType, node); } else { ClassNode type = getType(argumentListExpression.getExpression(0)); if (type.isUsingGenerics()) { adjustGenerics(type, node); } } // store inferred type on CCE storeType(cce, node); } }
if (callArgs instanceof ArgumentListExpression) { ArgumentListExpression args = (ArgumentListExpression) callArgs; int size = args.getExpressions().size(); paraTypes = new Parameter[size]; int i=0; for (Expression exp: args.getExpressions()) { ClassNode type = typeChooser.resolveType(exp, node); if (!validTypeForCall(type)) return;
List<Expression> args = makeArgumentList(call.getArguments()).getExpressions(); Parameter[] params = cn.getParameters();
private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) { ArgumentListExpression args = new ArgumentListExpression(); Parameter[] forwarderMethodParameters = forwarderMethod.getParameters(); for (final Parameter forwarderMethodParameter : forwarderMethodParameters) { args.addExpression(new VariableExpression(forwarderMethodParameter)); } BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE)); MethodCallExpression superCall = new MethodCallExpression( new VariableExpression("super"), forwarderMethod.getName(), args ); superCall.setImplicitThis(false); CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this")); MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS); getProxy.setImplicitThis(true); StaticMethodCallExpression proxyCall = new StaticMethodCallExpression( ClassHelper.make(InvokerHelper.class), "invokeMethod", new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions())) ); IfStatement stmt = new IfStatement( new BooleanExpression(instanceOfExpr), new ExpressionStatement(new CastExpression(returnType,proxyCall)), new ExpressionStatement(superCall) ); return stmt; }