Search in sources :

Example 1 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project groovy by apache.

the class AsmClassGenerator method visitClosureListExpression.

public void visitClosureListExpression(ClosureListExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushVariableScope(expression.getVariableScope());
    List<Expression> expressions = expression.getExpressions();
    final int size = expressions.size();
    // init declarations
    LinkedList<DeclarationExpression> declarations = new LinkedList<DeclarationExpression>();
    for (int i = 0; i < size; i++) {
        Expression expr = expressions.get(i);
        if (expr instanceof DeclarationExpression) {
            declarations.add((DeclarationExpression) expr);
            DeclarationExpression de = (DeclarationExpression) expr;
            BinaryExpression be = new BinaryExpression(de.getLeftExpression(), de.getOperation(), de.getRightExpression());
            expressions.set(i, be);
            de.setRightExpression(ConstantExpression.NULL);
            visitDeclarationExpression(de);
        }
    }
    LinkedList instructions = new LinkedList();
    BytecodeSequence seq = new BytecodeSequence(instructions);
    BlockStatement bs = new BlockStatement();
    bs.addStatement(seq);
    Parameter closureIndex = new Parameter(ClassHelper.int_TYPE, "__closureIndex");
    ClosureExpression ce = new ClosureExpression(new Parameter[] { closureIndex }, bs);
    ce.setVariableScope(expression.getVariableScope());
    // to keep stack height put a null on stack
    instructions.add(ConstantExpression.NULL);
    // init table
    final Label dflt = new Label();
    final Label tableEnd = new Label();
    final Label[] labels = new Label[size];
    instructions.add(new BytecodeInstruction() {

        public void visit(MethodVisitor mv) {
            mv.visitVarInsn(ILOAD, 1);
            mv.visitTableSwitchInsn(0, size - 1, dflt, labels);
        }
    });
    // visit cases
    for (int i = 0; i < size; i++) {
        final Label label = new Label();
        Object expr = expressions.get(i);
        final boolean isStatement = expr instanceof Statement;
        labels[i] = label;
        instructions.add(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitLabel(label);
                // so expressions need to pop the alibi null
                if (!isStatement)
                    mv.visitInsn(POP);
            }
        });
        instructions.add(expr);
        instructions.add(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitJumpInsn(GOTO, tableEnd);
            }
        });
    }
    // default case
    {
        instructions.add(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitLabel(dflt);
            }
        });
        ConstantExpression text = new ConstantExpression("invalid index for closure");
        ConstructorCallExpression cce = new ConstructorCallExpression(ClassHelper.make(IllegalArgumentException.class), text);
        ThrowStatement ts = new ThrowStatement(cce);
        instructions.add(ts);
    }
    // return
    instructions.add(new BytecodeInstruction() {

        public void visit(MethodVisitor mv) {
            mv.visitLabel(tableEnd);
            mv.visitInsn(ARETURN);
        }
    });
    // load main Closure
    visitClosureExpression(ce);
    // we need later an array to store the curried
    // closures, so we create it here and ave it
    // in a temporary variable
    BytecodeHelper.pushConstant(mv, size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    int listArrayVar = controller.getCompileStack().defineTemporaryVariable("_listOfClosures", true);
    // add curried versions
    for (int i = 0; i < size; i++) {
        // stack: closure
        // we need to create a curried closure version
        // so we store the type on stack
        mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/CurriedClosure");
        // stack: closure, type
        // for a constructor call we need the type two times
        // and the closure after them
        mv.visitInsn(DUP2);
        mv.visitInsn(SWAP);
        // stack: closure,type,type,closure
        // so we can create the curried closure
        mv.visitInsn(ICONST_1);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_0);
        mv.visitLdcInsn(i);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        mv.visitInsn(AASTORE);
        mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/CurriedClosure", "<init>", "(Lgroovy/lang/Closure;[Ljava/lang/Object;)V", false);
        // stack: closure,curriedClosure
        // we need to save the result
        mv.visitVarInsn(ALOAD, listArrayVar);
        mv.visitInsn(SWAP);
        BytecodeHelper.pushConstant(mv, i);
        mv.visitInsn(SWAP);
        mv.visitInsn(AASTORE);
    // stack: closure
    }
    // we don't need the closure any longer, so remove it
    mv.visitInsn(POP);
    // we load the array and create a list from it
    mv.visitVarInsn(ALOAD, listArrayVar);
    createListMethod.call(mv);
    // remove the temporary variable to keep the
    // stack clean
    controller.getCompileStack().removeVar(listArrayVar);
    controller.getOperandStack().pop();
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) Label(org.objectweb.asm.Label) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) LinkedList(java.util.LinkedList) MethodVisitor(org.objectweb.asm.MethodVisitor) Parameter(org.codehaus.groovy.ast.Parameter) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 2 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project grails-core by grails.

the class AbstractGrailsArtefactTransformer method populateAutowiredApiLookupMethod.

protected MethodNode populateAutowiredApiLookupMethod(ClassNode classNode, ClassNode implementationNode, String apiProperty, String methodName, BlockStatement methodBody) {
    addApiLookupFieldAndSetter(classNode, implementationNode, apiProperty, null);
    VariableExpression apiVar = new VariableExpression(apiProperty, implementationNode);
    BlockStatement ifBlock = new BlockStatement();
    ArgumentListExpression arguments = new ArgumentListExpression();
    arguments.addExpression(new ConstantExpression("Method on class [" + classNode + "] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly."));
    ifBlock.addStatement(new ThrowStatement(new ConstructorCallExpression(new ClassNode(IllegalStateException.class), arguments)));
    BlockStatement elseBlock = new BlockStatement();
    elseBlock.addStatement(new ReturnStatement(apiVar));
    methodBody.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(apiVar, GrailsASTUtils.EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION)), ifBlock, elseBlock));
    MethodNode methodNode = new MethodNode(methodName, PUBLIC_STATIC_MODIFIER, implementationNode, ZERO_PARAMETERS, null, methodBody);
    return methodNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 3 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project grails-core by grails.

the class ControllerActionTransformer method wrapMethodBodyWithExceptionHandling.

/**
 * This will wrap the method body in a try catch block which does something
 * like this:
 * <pre>
 * try {
 *     // original method body here
 * } catch (Exception $caughtException) {
 *     Method $method = getExceptionHandlerMethod($caughtException.getClass())
 *     if($method) {
 *         return $method.invoke(this, $caughtException)
 *     } else {
 *         throw $caughtException
 *     }
 * }
 * </pre>
 * @param methodNode the method to add the try catch block to
 */
protected void wrapMethodBodyWithExceptionHandling(final ClassNode controllerClassNode, final MethodNode methodNode) {
    final BlockStatement catchBlockCode = new BlockStatement();
    final String caughtExceptionArgumentName = "$caughtException";
    final Expression caughtExceptionVariableExpression = new VariableExpression(caughtExceptionArgumentName);
    final Expression caughtExceptionTypeExpression = new PropertyExpression(caughtExceptionVariableExpression, "class");
    final Expression thisExpression = new VariableExpression("this");
    final MethodCallExpression getExceptionHandlerMethodCall = new MethodCallExpression(thisExpression, "getExceptionHandlerMethodFor", caughtExceptionTypeExpression);
    applyDefaultMethodTarget(getExceptionHandlerMethodCall, controllerClassNode);
    final ClassNode reflectMethodClassNode = new ClassNode(Method.class);
    final String exceptionHandlerMethodVariableName = "$method";
    final Expression exceptionHandlerMethodExpression = new VariableExpression(exceptionHandlerMethodVariableName, new ClassNode(Method.class));
    final Expression declareExceptionHandlerMethod = new DeclarationExpression(new VariableExpression(exceptionHandlerMethodVariableName, reflectMethodClassNode), Token.newSymbol(Types.EQUALS, 0, 0), getExceptionHandlerMethodCall);
    final ArgumentListExpression invokeArguments = new ArgumentListExpression();
    invokeArguments.addExpression(thisExpression);
    invokeArguments.addExpression(caughtExceptionVariableExpression);
    final MethodCallExpression invokeExceptionHandlerMethodExpression = new MethodCallExpression(new VariableExpression(exceptionHandlerMethodVariableName), "invoke", invokeArguments);
    applyDefaultMethodTarget(invokeExceptionHandlerMethodExpression, reflectMethodClassNode);
    final Statement returnStatement = new ReturnStatement(invokeExceptionHandlerMethodExpression);
    final Statement throwCaughtExceptionStatement = new ThrowStatement(caughtExceptionVariableExpression);
    final Statement ifExceptionHandlerMethodExistsStatement = new IfStatement(new BooleanExpression(exceptionHandlerMethodExpression), returnStatement, throwCaughtExceptionStatement);
    catchBlockCode.addStatement(new ExpressionStatement(declareExceptionHandlerMethod));
    catchBlockCode.addStatement(ifExceptionHandlerMethodExistsStatement);
    final CatchStatement catchStatement = new CatchStatement(new Parameter(new ClassNode(Exception.class), caughtExceptionArgumentName), catchBlockCode);
    final Statement methodBody = methodNode.getCode();
    BlockStatement tryBlock = new BlockStatement();
    BlockStatement codeToHandleAllowedMethods = getCodeToHandleAllowedMethods(controllerClassNode, methodNode.getName());
    tryBlock.addStatement(codeToHandleAllowedMethods);
    tryBlock.addStatement(methodBody);
    final TryCatchStatement tryCatchStatement = new TryCatchStatement(tryBlock, new EmptyStatement());
    tryCatchStatement.addCatch(catchStatement);
    final ArgumentListExpression argumentListExpression = new ArgumentListExpression();
    argumentListExpression.addExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME));
    final PropertyExpression requestPropertyExpression = new PropertyExpression(new VariableExpression("this"), "request");
    final Expression removeAttributeMethodCall = new MethodCallExpression(requestPropertyExpression, "removeAttribute", argumentListExpression);
    final Expression getAttributeMethodCall = new MethodCallExpression(requestPropertyExpression, "getAttribute", new ArgumentListExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME)));
    final VariableExpression attributeValueExpression = new VariableExpression("$allowed_methods_attribute_value", ClassHelper.make(Object.class));
    final Expression initializeAttributeValue = new DeclarationExpression(attributeValueExpression, Token.newSymbol(Types.EQUALS, 0, 0), getAttributeMethodCall);
    final Expression attributeValueMatchesMethodNameExpression = new BinaryExpression(new ConstantExpression(methodNode.getName()), Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), attributeValueExpression);
    final Statement ifAttributeValueMatchesMethodName = new IfStatement(new BooleanExpression(attributeValueMatchesMethodNameExpression), new ExpressionStatement(removeAttributeMethodCall), new EmptyStatement());
    final BlockStatement blockToRemoveAttribute = new BlockStatement();
    blockToRemoveAttribute.addStatement(new ExpressionStatement(initializeAttributeValue));
    blockToRemoveAttribute.addStatement(ifAttributeValueMatchesMethodName);
    final TryCatchStatement tryCatchToRemoveAttribute = new TryCatchStatement(blockToRemoveAttribute, new EmptyStatement());
    tryCatchToRemoveAttribute.addCatch(new CatchStatement(new Parameter(ClassHelper.make(Exception.class), "$exceptionRemovingAttribute"), new EmptyStatement()));
    tryCatchStatement.setFinallyStatement(tryCatchToRemoveAttribute);
    methodNode.setCode(tryCatchStatement);
}
Also used : 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) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DelegatingMethod(grails.compiler.DelegatingMethod) ControllerMethod(grails.web.controllers.ControllerMethod) Method(java.lang.reflect.Method) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) 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) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) RequestParameter(grails.web.RequestParameter) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 4 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project groovy-core by groovy.

the class SingletonASTTransformation method createConstructor.

private void createConstructor(ClassNode classNode, FieldNode field, String propertyName, boolean isStrict) {
    final List<ConstructorNode> cNodes = classNode.getDeclaredConstructors();
    ConstructorNode foundNoArg = null;
    for (ConstructorNode cNode : cNodes) {
        final Parameter[] parameters = cNode.getParameters();
        if (parameters == null || parameters.length == 0) {
            foundNoArg = cNode;
            break;
        }
    }
    if (isStrict && cNodes.size() != 0) {
        for (ConstructorNode cNode : cNodes) {
            addError("@Singleton didn't expect to find one or more additional constructors: remove constructor(s) or set strict=false", cNode);
        }
    }
    if (foundNoArg == null) {
        final BlockStatement body = new BlockStatement();
        body.addStatement(ifS(notNullX(varX(field)), new ThrowStatement(ctorX(make(RuntimeException.class), args(constX("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + "." + propertyName))))));
        classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body));
    }
}
Also used : ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 5 with ThrowStatement

use of org.codehaus.groovy.ast.stmt.ThrowStatement in project groovy by apache.

the class AntlrParserPlugin method throwStatement.

protected Statement throwStatement(AST node) {
    AST expressionNode = node.getFirstChild();
    if (expressionNode == null) {
        expressionNode = node.getNextSibling();
    }
    if (expressionNode == null) {
        throw new ASTRuntimeException(node, "No expression available");
    }
    ThrowStatement throwStatement = new ThrowStatement(expression(expressionNode));
    configureAST(throwStatement, node);
    return throwStatement;
}
Also used : AST(antlr.collections.AST) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Aggregations

ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)8 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)6 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)5 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)5 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)4 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)4 Statement (org.codehaus.groovy.ast.stmt.Statement)4 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)4 Parameter (org.codehaus.groovy.ast.Parameter)3 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)3 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)3 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)3 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)3 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)3 LinkedList (java.util.LinkedList)2 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)2 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)2 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)2