public static CatchStatement catchS(Parameter variable, Statement code) { return new CatchStatement(variable, code); }
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; }
CatchStatement answer = new CatchStatement(catchParameter, code); configureAST(answer, catchNode); catches.add(answer); ClassNode exceptionType = buildName(exceptionNodes); Parameter catchParameter = new Parameter(exceptionType, variable); CatchStatement answer = new CatchStatement(catchParameter, code); configureAST(answer, catchNode); catches.add(answer);
private CatchStatement createHandleSuppressedThrowableStatement(VariableExpression featureThrowableVar) { Parameter catchParameter = new Parameter(nodeCache.Throwable, "$spock_tmp_throwable"); MethodCallExpression addSuppressedCall = new MethodCallExpression(featureThrowableVar, "addSuppressed", new ArgumentListExpression(new VariableExpression(catchParameter))); BinaryExpression featureThrowableNotNullExpr = createVariableNotNullExpression(featureThrowableVar); List<Statement> addSuppressedStats = Collections.<Statement>singletonList(new ExpressionStatement(addSuppressedCall)); List<Statement> throwFeatureStats = Collections.<Statement>singletonList(new ThrowStatement(new VariableExpression(catchParameter))); IfStatement ifFeatureNotNullStat = new IfStatement(new BooleanExpression(featureThrowableNotNullExpr), new BlockStatement(addSuppressedStats, new VariableScope()), new BlockStatement(throwFeatureStats, new VariableScope())); return new CatchStatement(catchParameter, new BlockStatement( Collections.<Statement>singletonList(ifFeatureNotNullStat), new VariableScope())); }
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()))); }
private CatchStatement createThrowableAssignmentAndRethrowCatchStatement(VariableExpression assignmentVar) { Parameter catchParameter = new Parameter(nodeCache.Throwable, "$spock_tmp_throwable"); BinaryExpression assignThrowableExpr = new BinaryExpression( new VariableExpression(assignmentVar), Token.newSymbol(Types.ASSIGN, -1, -1), new VariableExpression(catchParameter)); return new CatchStatement(catchParameter, new BlockStatement( Arrays.asList( new ExpressionStatement(assignThrowableExpr), new ThrowStatement(new VariableExpression(catchParameter))), new VariableScope())); }
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; }
/** * Adds one catch statement to catch a specific exception giving a * specific name for the exception variable. * * @param exceptionType the {@link ClassNode} of the exception you want to catch * @param exceptionVarName the name of the exception variable * @param stmt the code handling the exception * @return the current builder instance * @since 0.2.3 */ public TryCatchStatementBuilder addCatchStmt(final ClassNode exceptionType, final String exceptionVarName, final Statement stmt) { final Parameter param = A.NODES.param(exceptionVarName).type(exceptionType).build(); final CatchStatement catchStatement = new CatchStatement(param, stmt); catchStatements.add(catchStatement); return this; }
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 CatchStatement catchStatement(AST catchNode) { AST node = catchNode.getFirstChild(); Parameter parameter = parameter(node); ClassNode exceptionType = parameter.getType(); String variable = parameter.getName(); node = node.getNextSibling(); Statement code = statement(node); Parameter catchParameter = new Parameter(exceptionType,variable); CatchStatement answer = new CatchStatement(catchParameter, code); configureAST(answer, catchNode); return answer; }
protected CatchStatement catchStatement(AST catchNode) { AST node = catchNode.getFirstChild(); Parameter parameter = parameter(node); ClassNode exceptionType = parameter.getType(); String variable = parameter.getName(); node = node.getNextSibling(); Statement code = statement(node); Parameter catchParameter = new Parameter(exceptionType,variable); CatchStatement answer = new CatchStatement(catchParameter, code); configureAST(answer, catchNode); return answer; }
protected CatchStatement catchStatement(AST catchNode) { AST node = catchNode.getFirstChild(); Parameter parameter = parameter(node); ClassNode exceptionType = parameter.getType(); String variable = parameter.getName(); node = node.getNextSibling(); Statement code = statement(node); Parameter catchParameter = new Parameter(exceptionType, variable); CatchStatement answer = new CatchStatement(catchParameter, code); configureAST(answer, catchNode); return answer; }
protected CatchStatement catchStatement(AST catchNode) { AST node = catchNode.getFirstChild(); Parameter parameter = parameter(node); ClassNode exceptionType = parameter.getType(); String variable = parameter.getName(); node = node.getNextSibling(); Statement code = statement(node); Parameter catchParameter = new Parameter(exceptionType,variable); CatchStatement answer = new CatchStatement(catchParameter, code); configureAST(answer, catchNode); return answer; }
catchBlockCode.addStatement(ifExceptionHandlerMethodExistsStatement); final CatchStatement catchStatement = new CatchStatement(new Parameter(new ClassNode(Exception.class), caughtExceptionArgumentName), catchBlockCode); final Statement methodBody = methodNode.getCode(); tryCatchToRemoveAttribute.addCatch(new CatchStatement(new Parameter(ClassHelper.make(Exception.class), "$exceptionRemovingAttribute"), new EmptyStatement()));
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; }