public static ThrowStatement throwS(Expression expr) { return new ThrowStatement(expr); }
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; }
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 Statement throwAssertionFailedError(AnnotationNode annotationNode) { ThrowStatement throwStatement = new ThrowStatement( new ConstructorCallExpression(ASSERTION_FAILED_ERROR_TYPE, new ArgumentListExpression( new ConstantExpression("Method is marked with @NotYetImplemented but passes unexpectedly")))); throwStatement.setSourcePosition(annotationNode); return throwStatement; } }
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())); }
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);
/** * Represents how to throw an exception * * <strong>AST</strong> * <pre><code>throwS(newX(IllegalStateException, constX('wrong value')))</code></pre> * * <strong>Result</strong> * <pre><code>throw new IllegalStateException('wrong value')</code></pre> * * @param expression it will be normally a representation of a new * instance of a given Throwable type * @return an instance of {@link ThrowStatement} * @since 0.2.4 */ public static ThrowStatement throwS(final Expression expression) { return new ThrowStatement(expression); } }
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; }
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; }
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; }
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; }
private static ThrowStatement createMissingMethodThrowable(ClassNode classNode, MethodNode declaredMethodNode) { ArgumentListExpression exceptionArgs = new ArgumentListExpression(); exceptionArgs.addExpression(new ConstantExpression(declaredMethodNode.getName())); exceptionArgs.addExpression(new ClassExpression(classNode)); return new ThrowStatement(new ConstructorCallExpression(MISSING_METHOD_EXCEPTION, exceptionArgs)); }
private Statement throwAssertionFailedError(AnnotationNode annotationNode) { ThrowStatement throwStatement = new ThrowStatement( new ConstructorCallExpression(ASSERTION_FAILED_ERROR_TYPE, new ArgumentListExpression( new ConstantExpression("Method is marked with @NotYetImplemented but passes unexpectedly")))); throwStatement.setSourcePosition(annotationNode); return throwStatement; } }
/** * @return Returns the interruption check statement. */ protected Statement createInterruptStatement() { return new IfStatement( new BooleanExpression( createCondition() ), new ThrowStatement( new ConstructorCallExpression(thrownExceptionType, new ArgumentListExpression(new ConstantExpression(getErrorMessage()))) ), new EmptyStatement() ); }
private Statement checkFinalArgNotOverridden(ClassNode cNode, FieldNode fNode) { final String name = fNode.getName(); Expression value = findArg(name); return new IfStatement( equalsNullExpr(value), new EmptyStatement(), new ThrowStatement(new ConstructorCallExpression(READONLYEXCEPTION_TYPE, new ArgumentListExpression(new ConstantExpression(name), new ConstantExpression(cNode.getName()))))); }
private void createConstructor(ClassNode classNode, FieldNode field) { final List list = classNode.getDeclaredConstructors(); MethodNode found = null; for (Iterator it = list.iterator(); it.hasNext(); ) { MethodNode mn = (MethodNode) it.next(); final Parameter[] parameters = mn.getParameters(); if (parameters == null || parameters.length == 0) { found = mn; break; } } if (found == null) { final BlockStatement body = new BlockStatement(); body.addStatement(new IfStatement( new BooleanExpression(new BinaryExpression(new FieldExpression(field), Token.newSymbol("!=",-1,-1), ConstantExpression.NULL)), new ThrowStatement( new ConstructorCallExpression(ClassHelper.make(RuntimeException.class), new ArgumentListExpression( new ConstantExpression("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + ".instance" )))), new EmptyStatement())); classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body)); } } }
private void createConstructor(ClassNode classNode, FieldNode field) { final List list = classNode.getDeclaredConstructors(); MethodNode found = null; for (Iterator it = list.iterator(); it.hasNext(); ) { MethodNode mn = (MethodNode) it.next(); final Parameter[] parameters = mn.getParameters(); if (parameters == null || parameters.length == 0) { found = mn; break; } } if (found == null) { final BlockStatement body = new BlockStatement(); body.addStatement(new IfStatement( new BooleanExpression(new BinaryExpression(new FieldExpression(field), Token.newSymbol("!=",-1,-1), ConstantExpression.NULL)), new ThrowStatement( new ConstructorCallExpression(ClassHelper.make(RuntimeException.class), new ArgumentListExpression( new ConstantExpression("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + ".instance" )))), new EmptyStatement())); classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body)); } } }
private void createConstructor(ClassNode classNode, FieldNode field) { final List list = classNode.getDeclaredConstructors(); MethodNode found = null; for (Iterator it = list.iterator(); it.hasNext(); ) { MethodNode mn = (MethodNode) it.next(); final Parameter[] parameters = mn.getParameters(); if (parameters == null || parameters.length == 0) { found = mn; break; } } if (found == null) { final BlockStatement body = new BlockStatement(); body.addStatement(new IfStatement( new BooleanExpression(new BinaryExpression(new VariableExpression(field), Token.newSymbol("!=",-1,-1), ConstantExpression.NULL)), new ThrowStatement( new ConstructorCallExpression(ClassHelper.make(RuntimeException.class), new ArgumentListExpression( new ConstantExpression("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + ".instance" )))), new EmptyStatement())); classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body)); } } }
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; }
final Statement throwCaughtExceptionStatement = new ThrowStatement(caughtExceptionVariableExpression); final Statement ifExceptionHandlerMethodExistsStatement = new IfStatement(new BooleanExpression(exceptionHandlerMethodExpression), returnStatement, throwCaughtExceptionStatement); catchBlockCode.addStatement(new ExpressionStatement(declareExceptionHandlerMethod));