Search in sources :

Example 6 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 7 with ArgumentListExpression

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

the class InvokeDynamicWriter method makeIndyCall.

private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
    OperandStack operandStack = controller.getOperandStack();
    String sig = prepareIndyCall(receiver, implicitThis);
    // load arguments
    int numberOfArguments = 1;
    ArgumentListExpression ae = makeArgumentList(arguments);
    boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
    if (containsSpreadExpression) {
        controller.getAcg().despreadList(ae.getExpressions(), true);
        sig += getTypeDescription(Object[].class);
    } else {
        for (Expression arg : ae.getExpressions()) {
            arg.visit(controller.getAcg());
            if (arg instanceof CastExpression) {
                operandStack.box();
                controller.getAcg().loadWrapper(arg);
                sig += getTypeDescription(Wrapper.class);
            } else {
                sig += getTypeDescription(operandStack.getTopOperand());
            }
            numberOfArguments++;
        }
    }
    sig += ")Ljava/lang/Object;";
    String callSiteName = METHOD.getCallSiteName();
    if (adapter == null)
        callSiteName = INIT.getCallSiteName();
    int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
    finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) Wrapper(org.codehaus.groovy.runtime.wrappers.Wrapper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 8 with ArgumentListExpression

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

the class StaticTypesClosureWriter method createDirectCallMethod.

private void createDirectCallMethod(final ClassNode closureClass, final MethodNode doCallMethod) {
    // in case there is no "call" method on the closure, we can create a "fast invocation" paths
    // to avoid going through ClosureMetaClass by call(Object...) method
    // we can't have a specialized version of call(Object...) because the dispatch logic in ClosureMetaClass
    // is too complex!
    // call(Object)
    Parameter args = new Parameter(ClassHelper.OBJECT_TYPE, "args");
    MethodCallExpression doCall1arg = new MethodCallExpression(new VariableExpression("this", closureClass), "doCall", new ArgumentListExpression(new VariableExpression(args)));
    doCall1arg.setImplicitThis(true);
    doCall1arg.setMethodTarget(doCallMethod);
    closureClass.addMethod(new MethodNode("call", Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE, new Parameter[] { args }, ClassNode.EMPTY_ARRAY, new ReturnStatement(doCall1arg)));
    // call()
    MethodCallExpression doCallNoArgs = new MethodCallExpression(new VariableExpression("this", closureClass), "doCall", new ArgumentListExpression(new ConstantExpression(null)));
    doCallNoArgs.setImplicitThis(true);
    doCallNoArgs.setMethodTarget(doCallMethod);
    closureClass.addMethod(new MethodNode("call", Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new ReturnStatement(doCallNoArgs)));
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 9 with ArgumentListExpression

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

the class StaticTypesStatementWriter method writeIteratorBasedForEachLoop.

private void writeIteratorBasedForEachLoop(CompileStack compileStack, OperandStack operandStack, MethodVisitor mv, ForStatement loop, Expression collectionExpression, ClassNode collectionType, Parameter loopVariable) {
    // Declare the loop counter.
    BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
    if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(collectionType, ITERABLE_CLASSNODE)) {
        MethodCallExpression iterator = new MethodCallExpression(collectionExpression, "iterator", new ArgumentListExpression());
        iterator.setMethodTarget(collectionType.getMethod("iterator", Parameter.EMPTY_ARRAY));
        iterator.setImplicitThis(false);
        iterator.visit(controller.getAcg());
    } else {
        collectionExpression.visit(controller.getAcg());
        mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/DefaultGroovyMethods", "iterator", "(Ljava/lang/Object;)Ljava/util/Iterator;", false);
        operandStack.replace(ClassHelper.Iterator_TYPE);
    }
    // Then get the iterator and generate the loop control
    int iteratorIdx = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
    Label continueLabel = compileStack.getContinueLabel();
    Label breakLabel = compileStack.getBreakLabel();
    mv.visitLabel(continueLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorHasNext(mv);
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    mv.visitJumpInsn(IFEQ, breakLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorNext(mv);
    operandStack.push(ClassHelper.OBJECT_TYPE);
    operandStack.storeVar(variable);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    mv.visitJumpInsn(GOTO, continueLabel);
    mv.visitLabel(breakLabel);
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Label(org.objectweb.asm.Label) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 10 with ArgumentListExpression

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

the class TraitComposer method createForwarderMethod.

private static void createForwarderMethod(ClassNode trait, ClassNode targetNode, MethodNode helperMethod, MethodNode originalMethod, ClassNode helperClassNode, Map<String, ClassNode> genericsSpec, Parameter[] helperMethodParams, Parameter[] traitMethodParams, Parameter[] forwarderParams, ArgumentListExpression helperMethodArgList) {
    MethodCallExpression mce = new MethodCallExpression(new ClassExpression(helperClassNode), helperMethod.getName(), helperMethodArgList);
    mce.setImplicitThis(false);
    genericsSpec = GenericsUtils.addMethodGenerics(helperMethod, genericsSpec);
    ClassNode[] exceptionNodes = correctToGenericsSpecRecurse(genericsSpec, copyExceptions(helperMethod.getExceptions()));
    ClassNode fixedReturnType = correctToGenericsSpecRecurse(genericsSpec, helperMethod.getReturnType());
    Expression forwardExpression = genericsSpec.isEmpty() ? mce : new CastExpression(fixedReturnType, mce);
    int access = helperMethod.getModifiers();
    // we could rely on the first parameter name ($static$self) but that information is not
    // guaranteed to be always present
    boolean isHelperForStaticMethod = helperMethodParams[0].getOriginType().equals(ClassHelper.CLASS_Type);
    if (Modifier.isPrivate(access) && !isHelperForStaticMethod) {
        // see GROOVY-7213
        return;
    }
    if (!isHelperForStaticMethod) {
        access = access ^ Opcodes.ACC_STATIC;
    }
    MethodNode forwarder = new MethodNode(helperMethod.getName(), access, fixedReturnType, forwarderParams, exceptionNodes, new ExpressionStatement(forwardExpression));
    List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
    // at this point, should *always* stay empty
    List<AnnotationNode> notCopied = Collections.emptyList();
    GeneralUtils.copyAnnotatedNodeAnnotations(helperMethod, copied, notCopied);
    if (!copied.isEmpty()) {
        forwarder.addAnnotations(copied);
    }
    if (originalMethod != null) {
        GenericsType[] newGt = GenericsUtils.applyGenericsContextToPlaceHolders(genericsSpec, originalMethod.getGenericsTypes());
        newGt = removeNonPlaceHolders(newGt);
        forwarder.setGenericsTypes(newGt);
    }
    // add a helper annotation indicating that it is a bridge method
    AnnotationNode bridgeAnnotation = new AnnotationNode(Traits.TRAITBRIDGE_CLASSNODE);
    bridgeAnnotation.addMember("traitClass", new ClassExpression(trait));
    bridgeAnnotation.addMember("desc", new ConstantExpression(BytecodeHelper.getMethodDescriptor(helperMethod.getReturnType(), traitMethodParams)));
    forwarder.addAnnotation(bridgeAnnotation);
    if (!shouldSkipMethod(targetNode, forwarder.getName(), forwarderParams)) {
        targetNode.addMethod(forwarder);
    }
    createSuperForwarder(targetNode, forwarder, genericsSpec);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) LinkedList(java.util.LinkedList) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) GenericsType(org.codehaus.groovy.ast.GenericsType) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)108 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)73 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)72 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)66 Expression (org.codehaus.groovy.ast.expr.Expression)61 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)50 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)49 ClassNode (org.codehaus.groovy.ast.ClassNode)38 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)38 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)36 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)34 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)33 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)32 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)28 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)28 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)28 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)27 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)26 MethodNode (org.codehaus.groovy.ast.MethodNode)23 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)22