Search in sources :

Example 61 with MethodCallExpression

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

the class ModuleNode method createStatementsClass.

protected ClassNode createStatementsClass() {
    ClassNode classNode = getScriptClassDummy();
    if (classNode.getName().endsWith("package-info")) {
        return classNode;
    }
    handleMainMethodIfPresent(methods);
    // return new Foo(new ShellContext(args)).run()
    classNode.addMethod(new MethodNode("main", ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE.makeArray(), "args") }, ClassNode.EMPTY_ARRAY, new ExpressionStatement(new MethodCallExpression(new ClassExpression(ClassHelper.make(InvokerHelper.class)), "runScript", new ArgumentListExpression(new ClassExpression(classNode), new VariableExpression("args"))))));
    MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
    methodNode.setIsScriptBody();
    classNode.addMethod(methodNode);
    classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
    Statement stmt;
    // (like @BaseScript) that could change this.  But this is cautious and anticipates possible compiler changes.
    if (classNode.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR) != null) {
        stmt = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(new VariableExpression("context"))));
    } else {
        // Fallback for non-standard base "script" classes with no context (Binding) constructor.
        stmt = new ExpressionStatement(new MethodCallExpression(new VariableExpression("super"), "setBinding", new ArgumentListExpression(new VariableExpression("context"))));
    }
    classNode.addConstructor(ACC_PUBLIC, new Parameter[] { new Parameter(ClassHelper.make(Binding.class), "context") }, ClassNode.EMPTY_ARRAY, stmt);
    for (MethodNode node : methods) {
        int modifiers = node.getModifiers();
        if ((modifiers & ACC_ABSTRACT) != 0) {
            throw new RuntimeException("Cannot use abstract methods in a script, they are only available inside classes. Method: " + node.getName());
        }
        // br: the old logic seems to add static to all def f().... in a script, which makes enclosing
        // inner classes (including closures) in a def function difficult. Comment it out.
        node.setModifiers(modifiers);
        classNode.addMethod(node);
    }
    return classNode;
}
Also used : InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 62 with MethodCallExpression

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

the class MemoizedASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit source) {
    init(nodes, source);
    AnnotationNode annotationNode = (AnnotationNode) nodes[0];
    AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
    if (MY_TYPE.equals(annotationNode.getClassNode()) && annotatedNode instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) annotatedNode;
        if (methodNode.isAbstract()) {
            addError("Annotation " + MY_TYPE_NAME + " cannot be used for abstract methods.", methodNode);
            return;
        }
        if (methodNode.isVoidMethod()) {
            addError("Annotation " + MY_TYPE_NAME + " cannot be used for void methods.", methodNode);
            return;
        }
        ClassNode ownerClassNode = methodNode.getDeclaringClass();
        MethodNode delegatingMethod = buildDelegatingMethod(methodNode, ownerClassNode);
        ownerClassNode.addMethod(delegatingMethod);
        int modifiers = FieldNode.ACC_PRIVATE | FieldNode.ACC_FINAL;
        if (methodNode.isStatic()) {
            modifiers = modifiers | FieldNode.ACC_STATIC;
        }
        int protectedCacheSize = getMemberIntValue(annotationNode, PROTECTED_CACHE_SIZE_NAME);
        int maxCacheSize = getMemberIntValue(annotationNode, MAX_CACHE_SIZE_NAME);
        MethodCallExpression memoizeClosureCallExpression = buildMemoizeClosureCallExpression(delegatingMethod, protectedCacheSize, maxCacheSize);
        String memoizedClosureFieldName = buildUniqueName(ownerClassNode, CLOSURE_LABEL, methodNode);
        FieldNode memoizedClosureField = new FieldNode(memoizedClosureFieldName, modifiers, newClass(ClassHelper.CLOSURE_TYPE), null, memoizeClosureCallExpression);
        ownerClassNode.addField(memoizedClosureField);
        BlockStatement newCode = new BlockStatement();
        MethodCallExpression closureCallExpression = callX(fieldX(memoizedClosureField), CLOSURE_CALL_METHOD_NAME, args(methodNode.getParameters()));
        closureCallExpression.setImplicitThis(false);
        newCode.addStatement(returnS(closureCallExpression));
        methodNode.setCode(newCode);
        VariableScopeVisitor visitor = new VariableScopeVisitor(source);
        visitor.visitClass(ownerClassNode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

Example 63 with MethodCallExpression

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

the class MemoizedASTTransformation method buildMemoizeClosureCallExpression.

private MethodCallExpression buildMemoizeClosureCallExpression(MethodNode privateMethod, int protectedCacheSize, int maxCacheSize) {
    Parameter[] srcParams = privateMethod.getParameters();
    Parameter[] newParams = cloneParams(srcParams);
    List<Expression> argList = new ArrayList<Expression>(newParams.length);
    for (int i = 0; i < srcParams.length; i++) {
        argList.add(varX(newParams[i]));
    }
    ClosureExpression expression = new ClosureExpression(newParams, stmt(callThisX(privateMethod.getName(), args(argList))));
    MethodCallExpression mce;
    if (protectedCacheSize == 0 && maxCacheSize == 0) {
        mce = callX(expression, MEMOIZE_METHOD_NAME);
    } else if (protectedCacheSize == 0) {
        mce = callX(expression, MEMOIZE_AT_MOST_METHOD_NAME, args(constX(maxCacheSize)));
    } else if (maxCacheSize == 0) {
        mce = callX(expression, MEMOIZE_AT_LEAST_METHOD_NAME, args(constX(protectedCacheSize)));
    } else {
        mce = callX(expression, MEMOIZE_BETWEEN_METHOD_NAME, args(constX(protectedCacheSize), constX(maxCacheSize)));
    }
    mce.setImplicitThis(false);
    return mce;
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ArrayList(java.util.ArrayList) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 64 with MethodCallExpression

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

the class NewifyASTTransformation method isNewMethodStyle.

private boolean isNewMethodStyle(MethodCallExpression mce) {
    final Expression obj = mce.getObjectExpression();
    final Expression meth = mce.getMethod();
    return (obj instanceof ClassExpression && meth instanceof ConstantExpression && ((ConstantExpression) meth).getValue().equals("new"));
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) 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) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 65 with MethodCallExpression

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

the class AutoNewLineTransformer method createNewLine.

private Statement createNewLine(final ASTNode node) {
    MethodCallExpression mce = new MethodCallExpression(new VariableExpression("this"), "newLine", ArgumentListExpression.EMPTY_ARGUMENTS);
    mce.setImplicitThis(true);
    mce.setSourcePosition(node);
    ExpressionStatement stmt = new ExpressionStatement(mce);
    stmt.setSourcePosition(node);
    return stmt;
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)131 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)89 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)81 Expression (org.codehaus.groovy.ast.expr.Expression)77 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)73 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)59 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)58 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)51 ClassNode (org.codehaus.groovy.ast.ClassNode)43 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)42 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)40 MethodNode (org.codehaus.groovy.ast.MethodNode)36 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)36 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)36 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)35 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)34 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)31 FieldNode (org.codehaus.groovy.ast.FieldNode)26 Parameter (org.codehaus.groovy.ast.Parameter)23