Search in sources :

Example 31 with ConstructorCallExpression

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

the class JavaStubGenerator method printConstructor.

private void printConstructor(PrintWriter out, ClassNode clazz, ConstructorNode constructorNode) {
    printAnnotations(out, constructorNode);
    // printModifiers(out, constructorNode.getModifiers());
    // temporary hack
    out.print("public ");
    String className = clazz.getNameWithoutPackage();
    if (clazz instanceof InnerClassNode)
        className = className.substring(className.lastIndexOf("$") + 1);
    out.println(className);
    printParams(out, constructorNode);
    ConstructorCallExpression constrCall = getConstructorCallExpression(constructorNode);
    if (constrCall == null || !constrCall.isSpecialCall()) {
        out.println(" {}");
    } else {
        out.println(" {");
        printSpecialConstructorArgs(out, constructorNode, constrCall);
        out.println("}");
    }
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression)

Example 32 with ConstructorCallExpression

use of org.codehaus.groovy.ast.expr.ConstructorCallExpression 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 33 with ConstructorCallExpression

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

the class GeneralUtils method copyStatementsWithSuperAdjustment.

public static boolean copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body) {
    Statement preCode = pre.getCode();
    boolean changed = false;
    if (preCode instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) preCode;
        List<Statement> statements = block.getStatements();
        for (int i = 0; i < statements.size(); i++) {
            Statement statement = statements.get(i);
            // adjust the first statement if it's a super call
            if (i == 0 && statement instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) statement;
                Expression preExp = es.getExpression();
                if (preExp instanceof MethodCallExpression) {
                    MethodCallExpression mce = (MethodCallExpression) preExp;
                    String name = mce.getMethodAsString();
                    if ("super".equals(name)) {
                        es.setExpression(new ConstructorCallExpression(ClassNode.SUPER, mce.getArguments()));
                        changed = true;
                    }
                }
            }
            body.addStatement(statement);
        }
    }
    return changed;
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 34 with ConstructorCallExpression

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

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 35 with ConstructorCallExpression

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

the class ConstructorNode method firstStatementIsSpecialConstructorCall.

public boolean firstStatementIsSpecialConstructorCall() {
    Statement code = getFirstStatement();
    if (code == null || !(code instanceof ExpressionStatement))
        return false;
    Expression expression = ((ExpressionStatement) code).getExpression();
    if (!(expression instanceof ConstructorCallExpression))
        return false;
    ConstructorCallExpression cce = (ConstructorCallExpression) expression;
    return cce.isSpecialCall();
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Aggregations

ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)68 Expression (org.codehaus.groovy.ast.expr.Expression)45 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)44 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)37 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)35 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)32 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)30 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)28 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)25 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)25 ClassNode (org.codehaus.groovy.ast.ClassNode)24 Statement (org.codehaus.groovy.ast.stmt.Statement)24 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)21 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)18 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)16 ArrayList (java.util.ArrayList)15 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)15 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)15 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)13 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)10