Search in sources :

Example 1 with ArgumentListExpression

use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project groovy by apache.

the class MacroInvocationTrap method visitConstructorCallExpression.

@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
    ClassNode type = call.getType();
    if (type instanceof InnerClassNode) {
        if (((InnerClassNode) type).isAnonymous() && MACROCLASS_TYPE.getNameWithoutPackage().equals(type.getSuperClass().getNameWithoutPackage())) {
            //System.out.println("call = " + call.getText());
            try {
                String source = convertInnerClassToSource(type);
                List<Expression> macroArgumentsExpressions = new LinkedList<Expression>();
                macroArgumentsExpressions.add(new ConstantExpression(source));
                macroArgumentsExpressions.add(buildSubstitutionMap(type));
                macroArgumentsExpressions.add(new ClassExpression(ClassHelper.make(ClassNode.class)));
                MethodCallExpression macroCall = new MethodCallExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), MacroTransformation.MACRO_METHOD, new ArgumentListExpression(macroArgumentsExpressions));
                macroCall.setSpreadSafe(false);
                macroCall.setSafe(false);
                macroCall.setImplicitThis(false);
                call.putNodeMetaData(MacroTransformation.class, macroCall);
                List<ClassNode> classes = sourceUnit.getAST().getClasses();
                for (Iterator<ClassNode> iterator = classes.iterator(); iterator.hasNext(); ) {
                    final ClassNode aClass = iterator.next();
                    if (aClass == type || type == aClass.getOuterClass()) {
                        iterator.remove();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
    }
    super.visitConstructorCallExpression(call);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LinkedList(java.util.LinkedList) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MacroBuilder(org.codehaus.groovy.macro.runtime.MacroBuilder)

Example 2 with ArgumentListExpression

use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project groovy by apache.

the class MapConstructorASTTransformation method assignField.

private static void assignField(boolean useSetters, Parameter map, BlockStatement body, String name) {
    ArgumentListExpression nameArg = args(constX(name));
    body.addStatement(ifS(callX(varX(map), "containsKey", nameArg), useSetters ? stmt(callThisX(getSetterName(name), callX(varX(map), "get", nameArg))) : assignS(propX(varX("this"), name), callX(varX(map), "get", nameArg))));
}
Also used : ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 3 with ArgumentListExpression

use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project grails-core by grails.

the class DelegateAsyncTransformation method applyDelegateAsyncTransform.

private void applyDelegateAsyncTransform(ClassNode classNode, ClassNode targetApi, String fieldName) {
    List<MethodNode> methods = targetApi.getAllDeclaredMethods();
    ClassNode promisesClass = ClassHelper.make(Promises.class).getPlainNodeReference();
    MethodNode createPromiseMethodTargetWithDecorators = promisesClass.getDeclaredMethod("createPromise", new Parameter[] { new Parameter(new ClassNode(Closure.class), "c"), new Parameter(new ClassNode(List.class), "c") });
    DelegateAsyncTransactionalMethodTransformer delegateAsyncTransactionalMethodTransformer = lookupAsyncTransactionalMethodTransformer();
    for (MethodNode m : methods) {
        if (isCandidateMethod(m)) {
            MethodNode existingMethod = classNode.getMethod(m.getName(), m.getParameters());
            if (existingMethod == null) {
                ClassNode promiseNode = ClassHelper.make(Promise.class).getPlainNodeReference();
                ClassNode originalReturnType = m.getReturnType();
                if (!originalReturnType.getNameWithoutPackage().equals(VOID)) {
                    ClassNode returnType;
                    if (ClassHelper.isPrimitiveType(originalReturnType.redirect())) {
                        returnType = ClassHelper.getWrapper(originalReturnType.redirect());
                    } else {
                        returnType = alignReturnType(classNode, originalReturnType);
                    }
                    if (!OBJECT_CLASS_NODE.equals(returnType)) {
                        promiseNode.setGenericsTypes(new GenericsType[] { new GenericsType(returnType) });
                    }
                }
                final BlockStatement methodBody = new BlockStatement();
                final BlockStatement promiseBody = new BlockStatement();
                final ClosureExpression closureExpression = new ClosureExpression(new Parameter[0], promiseBody);
                VariableScope variableScope = new VariableScope();
                closureExpression.setVariableScope(variableScope);
                VariableExpression thisObject = new VariableExpression("this");
                ClassNode delegateAsyncUtilsClassNode = new ClassNode(DelegateAsyncUtils.class);
                MethodNode getPromiseDecoratorsMethodNode = delegateAsyncUtilsClassNode.getDeclaredMethods("getPromiseDecorators").get(0);
                ListExpression promiseDecorators = new ListExpression();
                ArgumentListExpression getPromiseDecoratorsArguments = new ArgumentListExpression(thisObject, promiseDecorators);
                delegateAsyncTransactionalMethodTransformer.transformTransactionalMethod(classNode, targetApi, m, promiseDecorators);
                MethodCallExpression getDecoratorsMethodCall = new MethodCallExpression(new ClassExpression(delegateAsyncUtilsClassNode), "getPromiseDecorators", getPromiseDecoratorsArguments);
                getDecoratorsMethodCall.setMethodTarget(getPromiseDecoratorsMethodNode);
                MethodCallExpression createPromiseWithDecorators = new MethodCallExpression(new ClassExpression(promisesClass), "createPromise", new ArgumentListExpression(closureExpression, getDecoratorsMethodCall));
                if (createPromiseMethodTargetWithDecorators != null) {
                    createPromiseWithDecorators.setMethodTarget(createPromiseMethodTargetWithDecorators);
                }
                methodBody.addStatement(new ExpressionStatement(createPromiseWithDecorators));
                final ArgumentListExpression arguments = new ArgumentListExpression();
                Parameter[] parameters = copyParameters(StaticTypeCheckingSupport.parameterizeArguments(classNode, m));
                for (Parameter p : parameters) {
                    p.setClosureSharedVariable(true);
                    variableScope.putReferencedLocalVariable(p);
                    VariableExpression ve = new VariableExpression(p);
                    ve.setClosureSharedVariable(true);
                    arguments.addExpression(ve);
                }
                MethodCallExpression delegateMethodCall = new MethodCallExpression(new VariableExpression(fieldName), m.getName(), arguments);
                promiseBody.addStatement(new ExpressionStatement(delegateMethodCall));
                MethodNode newMethodNode = new MethodNode(m.getName(), Modifier.PUBLIC, promiseNode, parameters, null, methodBody);
                classNode.addMethod(newMethodNode);
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Promises(grails.async.Promises) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Promise(grails.async.Promise) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 4 with ArgumentListExpression

use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project grails-core by grails.

the class LoggingTransformer method addLogField.

public static void addLogField(ClassNode classNode, String logName) {
    FieldNode logVariable = new FieldNode(LOG_PROPERTY, Modifier.STATIC | Modifier.PRIVATE, new ClassNode(Logger.class), classNode, new MethodCallExpression(new ClassExpression(new ClassNode(LoggerFactory.class)), "getLogger", new ArgumentListExpression(new ConstantExpression(logName))));
    classNode.addField(logVariable);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory)

Example 5 with ArgumentListExpression

use of org.codehaus.groovy.ast.expr.ArgumentListExpression in project groovy by apache.

the class BinaryExpressionHelper method assignToArray.

protected void assignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader) {
    // let's replace this assignment to a subscript operator with a
    // method call
    // e.g. x[5] = 10
    // -> (x, [], 5), =, 10
    // -> methodCall(x, "putAt", [5, 10])
    ArgumentListExpression ae = new ArgumentListExpression(index, rhsValueLoader);
    controller.getInvocationWriter().makeCall(parent, receiver, new ConstantExpression("putAt"), ae, InvocationWriter.invokeMethod, false, false, false);
    controller.getOperandStack().pop();
    // return value of assignment
    rhsValueLoader.visit(controller.getAcg());
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Aggregations

ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)164 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)118 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)111 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)100 Expression (org.codehaus.groovy.ast.expr.Expression)99 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)80 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)75 ClassNode (org.codehaus.groovy.ast.ClassNode)67 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)66 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)61 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)60 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)60 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)55 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)53 MethodNode (org.codehaus.groovy.ast.MethodNode)52 Parameter (org.codehaus.groovy.ast.Parameter)51 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)49 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)49 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)46 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)43