use of org.codehaus.groovy.GroovyBugError in project gcontracts by andresteingress.
the class TryCatchBlockGenerator method generateTryCatchBlock.
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;
}
Aggregations