private TryCatchStatement createCleanupTryCatch(CleanupBlock block, VariableExpression featureThrowableVar) { List<Statement> cleanupStats = new ArrayList<>(block.getAst()); TryCatchStatement tryCatchStat = new TryCatchStatement( new BlockStatement(cleanupStats, new VariableScope()), EmptyStatement.INSTANCE); tryCatchStat.addCatch(createHandleSuppressedThrowableStatement(featureThrowableVar)); return tryCatchStat; }
private TryCatchStatement tryCatchAssertionFailedError(AnnotationNode annotationNode, MethodNode methodNode, ArrayList<Statement> statements) { TryCatchStatement tryCatchStatement = new TryCatchStatement(new BlockStatement(statements, methodNode.getVariableScope()), EmptyStatement.INSTANCE); tryCatchStatement.addCatch(new CatchStatement(new Parameter(CATCHED_THROWABLE_TYPE, "ex"), ReturnStatement.RETURN_NULL_OR_VOID)); return tryCatchStatement; }
protected Statement tryStatement(AST tryStatementNode) { AST tryNode = tryStatementNode.getFirstChild(); Statement tryStatement = statement(tryNode); Statement finallyStatement = EmptyStatement.INSTANCE; AST node = tryNode.getNextSibling(); // let's do the catch nodes List<CatchStatement> catches = new ArrayList<CatchStatement>(); for (; isType(LITERAL_catch, node); node = node.getNextSibling()) { final List<CatchStatement> catchStatements = catchStatement(node); catches.addAll(catchStatements); } if (isType(LITERAL_finally, node)) { finallyStatement = statement(node); node = node.getNextSibling(); } if (finallyStatement instanceof EmptyStatement && catches.isEmpty()) { throw new ASTRuntimeException(tryStatementNode, "A try statement must have at least one catch or finally block."); } TryCatchStatement tryCatchStatement = new TryCatchStatement(tryStatement, finallyStatement); configureAST(tryCatchStatement, tryStatementNode); for (CatchStatement statement : catches) { tryCatchStatement.addCatch(statement); } return tryCatchStatement; }
@Override public void visitCleanupBlock(CleanupBlock block) { for (Block b : method.getBlocks()) { if (b == block) break; moveVariableDeclarations(b.getAst(), method.getStatements()); } VariableExpression featureThrowableVar = new VariableExpression("$spock_feature_throwable", nodeCache.Throwable); method.getStatements().add(createVariableDeclarationStatement(featureThrowableVar)); List<Statement> featureStats = new ArrayList<>(); for (Block b : method.getBlocks()) { if (b == block) break; featureStats.addAll(b.getAst()); } CatchStatement featureCatchStat = createThrowableAssignmentAndRethrowCatchStatement(featureThrowableVar); List<Statement> cleanupStats = Collections.<Statement>singletonList( createCleanupTryCatch(block, featureThrowableVar)); TryCatchStatement tryFinally = new TryCatchStatement( new BlockStatement(featureStats, new VariableScope()), new BlockStatement(cleanupStats, new VariableScope())); tryFinally.addCatch(featureCatchStat); method.getStatements().add(tryFinally); // a cleanup-block may only be followed by a where-block, whose // statements are copied to newly generated methods rather than // the original method movedStatsBackToMethod = true; }
private void rewriteWhenBlockForExceptionCondition(WhenBlock block) { List<Statement> tryStats = block.getAst(); List<Statement> blockStats = new ArrayList<>(); block.setAst(blockStats); blockStats.add( new ExpressionStatement( setThrownException(ConstantExpression.NULL))); // ensure variables remain in same scope // by moving variable defs to the very beginning of the method, they don't // need to be moved again if feature method also has a cleanup-block moveVariableDeclarations(tryStats, method.getStatements()); TryCatchStatement tryCatchStat = new TryCatchStatement( new BlockStatement(tryStats, new VariableScope()), new BlockStatement()); blockStats.add(tryCatchStat); tryCatchStat.addCatch( new CatchStatement( new Parameter(nodeCache.Throwable, SpockNames.SPOCK_EX), new BlockStatement( Arrays.<Statement>asList( new ExpressionStatement( setThrownException( new VariableExpression(SpockNames.SPOCK_EX)))), new VariableScope()))); }
new TryCatchStatement( new BlockStatement(allStats, new VariableScope()), new ExpressionStatement(
private TryCatchStatement surroundSpecialTryCatch(Expression executeAndVerify) { final TryCatchStatement tryCatchStatement = new TryCatchStatement( new ExpressionStatement(executeAndVerify), EmptyStatement.INSTANCE ); tryCatchStatement.addCatch( new CatchStatement( new Parameter(new ClassNode(Throwable.class), "throwable"), new ExpressionStatement( AstUtil.createDirectMethodCall( new ClassExpression(resources.getAstNodeCache().SpockRuntime), resources.getAstNodeCache().SpockRuntime_GroupConditionFailedWithException, new ArgumentListExpression(Arrays.<Expression>asList( new VariableExpression(SpockNames.ERROR_COLLECTOR), new VariableExpression("throwable") // throwable )) ) ) ) ); return tryCatchStatement; }
private TryCatchStatement surroundWithTryCatch(Expression condition, Expression message, Expression executeAndVerify) { final TryCatchStatement tryCatchStatement = new TryCatchStatement( new ExpressionStatement(executeAndVerify), EmptyStatement.INSTANCE ); tryCatchStatement.addCatch( new CatchStatement( new Parameter(new ClassNode(Throwable.class), "throwable"), new ExpressionStatement( AstUtil.createDirectMethodCall( new ClassExpression(resources.getAstNodeCache().SpockRuntime), resources.getAstNodeCache().SpockRuntime_ConditionFailedWithException, new ArgumentListExpression(Arrays.asList( new VariableExpression(SpockNames.ERROR_COLLECTOR), new VariableExpression(SpockNames.VALUE_RECORDER), // recorder new ConstantExpression(resources.getSourceText(condition)), // text new ConstantExpression(condition.getLineNumber()), // line new ConstantExpression(condition.getColumnNumber()), // column message == null ? ConstantExpression.NULL : message, // message new VariableExpression("throwable") // throwable )) ) ) ) ); return tryCatchStatement; }
new TryCatchStatement(originalCode, stmt(releaseLock))));
/** * Returns the created {@link TryCatchStatement} instance. At * least the "try" statement and the "finally" statement must be * provided. Otherwise it will throw a {@link * IllegalStateException} * * @return a {@link TryCatchStatement} instance * @throws IllegalStateException if neither try nor finally statements are provided * @since 0.2.3 */ public TryCatchStatement build() { if (tryStatement == null || finallyStatement == null) { throw new IllegalStateException("Both try statement and finally statement must be provided"); } final TryCatchStatement tryCatchStmt = new TryCatchStatement(tryStatement, finallyStatement); if (!catchStatements.isEmpty()) { tryCatchStmt .getCatchStatements() .addAll(catchStatements); } return tryCatchStmt; } }
Statement code = ex.getCode(); code = createImplicitReturn(code); ex.setCode(new BlockStatement(new Statement[] {startStats,new TryCatchStatement(code,endStats)},ex.getVariableScope()));
Statement code = ex.getCode(); code = createImplicitReturn(code); ex.setCode(new BlockStatement(new Statement[] {startStats,new TryCatchStatement(code,endStats)},ex.getVariableScope()));
private TryCatchStatement tryCatchAssertionFailedError(AnnotationNode annotationNode, MethodNode methodNode, ArrayList<Statement> statements) { TryCatchStatement tryCatchStatement = new TryCatchStatement( block(methodNode.getVariableScope(), statements), EmptyStatement.INSTANCE); tryCatchStatement.addCatch(catchS(param(CATCHED_THROWABLE_TYPE, "ex"), ReturnStatement.RETURN_NULL_OR_VOID)); return tryCatchStatement; }
protected Statement tryStatement(AST tryStatementNode) { AST tryNode = tryStatementNode.getFirstChild(); Statement tryStatement = statement(tryNode); Statement finallyStatement = EmptyStatement.INSTANCE; AST node = tryNode.getNextSibling(); // let's do the catch nodes List catches = new ArrayList(); for (; node != null && isType(LITERAL_catch, node); node = node.getNextSibling()) { catches.add(catchStatement(node)); } if (isType(LITERAL_finally, node)) { finallyStatement = statement(node); node = node.getNextSibling(); } if (finallyStatement instanceof EmptyStatement && catches.size() == 0) { throw new ASTRuntimeException(tryStatementNode, "A try statement must have at least one catch or finally block."); } TryCatchStatement tryCatchStatement = new TryCatchStatement(tryStatement, finallyStatement); configureAST(tryCatchStatement, tryStatementNode); for (Iterator iter = catches.iterator(); iter.hasNext();) { CatchStatement statement = (CatchStatement) iter.next(); tryCatchStatement.addCatch(statement); } return tryCatchStatement; }
protected Statement tryStatement(AST tryStatementNode) { AST tryNode = tryStatementNode.getFirstChild(); Statement tryStatement = statement(tryNode); Statement finallyStatement = EmptyStatement.INSTANCE; AST node = tryNode.getNextSibling(); // let's do the catch nodes List catches = new ArrayList(); for (; node != null && isType(LITERAL_catch, node); node = node.getNextSibling()) { catches.add(catchStatement(node)); } if (isType(LITERAL_finally, node)) { finallyStatement = statement(node); node = node.getNextSibling(); } if (finallyStatement instanceof EmptyStatement && catches.size() == 0) { throw new ASTRuntimeException(tryStatementNode, "A try statement must have at least one catch or finally block."); } TryCatchStatement tryCatchStatement = new TryCatchStatement(tryStatement, finallyStatement); configureAST(tryCatchStatement, tryStatementNode); for (Iterator iter = catches.iterator(); iter.hasNext();) { CatchStatement statement = (CatchStatement) iter.next(); tryCatchStatement.addCatch(statement); } return tryCatchStatement; }
protected Statement tryStatement(AST tryStatementNode) { AST tryNode = tryStatementNode.getFirstChild(); Statement tryStatement = statement(tryNode); Statement finallyStatement = EmptyStatement.INSTANCE; AST node = tryNode.getNextSibling(); // let's do the catch nodes List catches = new ArrayList(); for (; node != null && isType(LITERAL_catch, node); node = node.getNextSibling()) { catches.add(catchStatement(node)); } if (isType(LITERAL_finally, node)) { finallyStatement = statement(node); node = node.getNextSibling(); } if (finallyStatement instanceof EmptyStatement && catches.size() == 0) { throw new ASTRuntimeException(tryStatementNode, "A try statement must have at least one catch or finally block."); } TryCatchStatement tryCatchStatement = new TryCatchStatement(tryStatement, finallyStatement); configureAST(tryCatchStatement, tryStatementNode); for (Iterator iter = catches.iterator(); iter.hasNext();) { CatchStatement statement = (CatchStatement) iter.next(); tryCatchStatement.addCatch(statement); } return tryCatchStatement; }
private TryCatchStatement tryCatchAssertionFailedError(AnnotationNode annotationNode, MethodNode methodNode, ArrayList<Statement> statements) { TryCatchStatement tryCatchStatement = new TryCatchStatement(new BlockStatement(statements, methodNode.getVariableScope()), EmptyStatement.INSTANCE); tryCatchStatement.addCatch(new CatchStatement(new Parameter(CATCHED_THROWABLE_TYPE, "ex"), ReturnStatement.RETURN_NULL_OR_VOID)); return tryCatchStatement; }
protected Statement tryStatement(AST tryStatementNode) { AST tryNode = tryStatementNode.getFirstChild(); Statement tryStatement = statement(tryNode); Statement finallyStatement = EmptyStatement.INSTANCE; AST node = tryNode.getNextSibling(); // let's do the catch nodes List<CatchStatement> catches = new ArrayList<CatchStatement>(); for (; node != null && isType(LITERAL_catch, node); node = node.getNextSibling()) { catches.add(catchStatement(node)); } if (isType(LITERAL_finally, node)) { finallyStatement = statement(node); node = node.getNextSibling(); } if (finallyStatement instanceof EmptyStatement && catches.size() == 0) { throw new ASTRuntimeException(tryStatementNode, "A try statement must have at least one catch or finally block."); } TryCatchStatement tryCatchStatement = new TryCatchStatement(tryStatement, finallyStatement); configureAST(tryCatchStatement, tryStatementNode); for (CatchStatement statement : catches) { tryCatchStatement.addCatch(statement); } return tryCatchStatement; }
public static BlockStatement generateTryCatchBlockForInlineMode(final ClassNode assertionErrorClass, final String message, final Statement assertStatement) { final Class powerAssertionErrorClass = loadPowerAssertionErrorClass(); if (powerAssertionErrorClass == null) throw new GroovyBugError("GContracts >= 1.1.2 needs Groovy 1.7 or above!"); VariableExpression newErrorVariableExpression = new VariableExpression("newError", assertionErrorClass); newErrorVariableExpression.setAccessedVariable(newErrorVariableExpression); ExpressionStatement expr = new ExpressionStatement(new DeclarationExpression(newErrorVariableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), new ConstructorCallExpression(assertionErrorClass, new ArgumentListExpression(new BinaryExpression(new ConstantExpression(message), Token.newSymbol(Types.PLUS, -1, -1), new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getMessage", ArgumentListExpression.EMPTY_ARGUMENTS)))))); ExpressionStatement exp2 = new ExpressionStatement(new MethodCallExpression(newErrorVariableExpression, "setStackTrace", new ArgumentListExpression( new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getStackTrace", ArgumentListExpression.EMPTY_ARGUMENTS) ))); final BlockStatement catchBlock = new BlockStatement(); catchBlock.addStatement(expr); catchBlock.addStatement(exp2); catchBlock.addStatement(new ThrowStatement(newErrorVariableExpression)); final TryCatchStatement tryCatchStatement = new TryCatchStatement(assertStatement, new EmptyStatement()); tryCatchStatement.addCatch(new CatchStatement(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error"), catchBlock)); final BlockStatement assertBlockStatement = new BlockStatement(); assertBlockStatement.addStatement(tryCatchStatement); return assertBlockStatement; }
public static BlockStatement generateTryCatchBlock(final ClassNode assertionErrorClass, final String message, final Statement assertStatement) { final String $_gc_closure_result = "$_gc_closure_result"; final VariableExpression variableExpression = new VariableExpression($_gc_closure_result, ClassHelper.Boolean_TYPE); variableExpression.setAccessedVariable(variableExpression); // if the assert statement is successful the return variable will be true else false final BlockStatement overallBlock = new BlockStatement(); overallBlock.addStatement(new ExpressionStatement(new DeclarationExpression(variableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), ConstantExpression.FALSE))); final BlockStatement assertBlockStatement = new BlockStatement(); assertBlockStatement.addStatement(assertStatement); assertBlockStatement.addStatement(new ExpressionStatement(new BinaryExpression(variableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), ConstantExpression.TRUE))); final Class powerAssertionErrorClass = loadPowerAssertionErrorClass(); if (powerAssertionErrorClass == null) throw new GroovyBugError("GContracts >= 1.1.2 needs Groovy 1.7 or above!"); VariableExpression newErrorVariableExpression = new VariableExpression("newError", assertionErrorClass); newErrorVariableExpression.setAccessedVariable(newErrorVariableExpression); ExpressionStatement expr = new ExpressionStatement(new DeclarationExpression(newErrorVariableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), new ConstructorCallExpression(assertionErrorClass, new ArgumentListExpression(new BinaryExpression(new ConstantExpression(message), Token.newSymbol(Types.PLUS, -1, -1), new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getMessage", ArgumentListExpression.EMPTY_ARGUMENTS)))))); ExpressionStatement exp2 = new ExpressionStatement(new MethodCallExpression(newErrorVariableExpression, "setStackTrace", new ArgumentListExpression( new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getStackTrace", ArgumentListExpression.EMPTY_ARGUMENTS) ))); final BlockStatement catchBlock = new BlockStatement(); catchBlock.addStatement(expr); catchBlock.addStatement(exp2); final TryCatchStatement tryCatchStatement = new TryCatchStatement(assertBlockStatement, new EmptyStatement()); tryCatchStatement.addCatch(new CatchStatement(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error"), catchBlock)); overallBlock.addStatement(tryCatchStatement); overallBlock.addStatement(new ReturnStatement(variableExpression)); return overallBlock; }