use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class MethodTest method testMethods.
public void testMethods() throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
Statement statementA = new ReturnStatement(new ConstantExpression("calledA"));
Statement statementB = new ReturnStatement(new ConstantExpression("calledB"));
Statement emptyStatement = new BlockStatement();
classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA));
classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB));
classNode.addMethod(new MethodNode("noReturnMethodA", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
classNode.addMethod(new MethodNode("noReturnMethodB", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
classNode.addMethod(new MethodNode("c", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.getDeclaredConstructor().newInstance();
assertTrue("Created instance of class: " + bean, bean != null);
assertCallMethod(bean, "a", "calledA");
assertCallMethod(bean, "b", "calledB");
assertCallMethod(bean, "noReturnMethodA", null);
assertCallMethod(bean, "noReturnMethodB", null);
assertCallMethod(bean, "c", null);
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class PostconditionGenerator method addPostcondition.
private void addPostcondition(MethodNode method, BlockStatement postconditionBlockStatement) {
final BlockStatement block = (BlockStatement) method.getCode();
// if return type is not void, than a "result" variable is provided in the postcondition expression
final List<Statement> statements = block.getStatements();
if (statements.size() > 0) {
Expression contractsEnabled = localVarX(BaseVisitor.GCONTRACTS_ENABLED_VAR, ClassHelper.boolean_TYPE);
if (!isPrimitiveVoid(method.getReturnType())) {
List<ReturnStatement> returnStatements = AssertStatementCreationUtility.getReturnStatements(method);
for (ReturnStatement returnStatement : returnStatements) {
BlockStatement localPostconditionBlockStatement = block(new VariableScope(), postconditionBlockStatement.getStatements());
Expression result = localVarX("result", method.getReturnType());
localPostconditionBlockStatement.getStatements().add(0, declS(result, returnStatement.getExpression()));
AssertStatementCreationUtility.injectResultVariableReturnStatementAndAssertionCallStatement(block, method.getReturnType().redirect(), returnStatement, localPostconditionBlockStatement);
}
setOldVariablesIfEnabled(block, contractsEnabled);
} else if (method instanceof ConstructorNode) {
block.addStatements(postconditionBlockStatement.getStatements());
} else {
setOldVariablesIfEnabled(block, contractsEnabled);
block.addStatements(postconditionBlockStatement.getStatements());
}
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class ClassInvariantGenerator method addInvariantAssertionStatement.
/**
* Adds the current class-invariant to the given <tt>method</tt>.
*
* @param type the {@link org.codehaus.groovy.ast.ClassNode} which declared the given {@link org.codehaus.groovy.ast.MethodNode}
* @param method the current {@link org.codehaus.groovy.ast.MethodNode}
*/
public void addInvariantAssertionStatement(final ClassNode type, final MethodNode method) {
final String invariantMethodName = getInvariantMethodName(type);
final MethodNode invariantMethod = type.getDeclaredMethod(invariantMethodName, Parameter.EMPTY_ARRAY);
if (invariantMethod == null)
return;
Statement invariantMethodCall = stmt(callThisX(invariantMethod.getName()));
final Statement statement = method.getCode();
if (statement instanceof BlockStatement && !isPrimitiveVoid(method.getReturnType()) && !(method instanceof ConstructorNode)) {
final BlockStatement blockStatement = (BlockStatement) statement;
final List<ReturnStatement> returnStatements = AssertStatementCreationUtility.getReturnStatements(method);
for (ReturnStatement returnStatement : returnStatements) {
AssertStatementCreationUtility.addAssertionCallStatementToReturnStatement(blockStatement, returnStatement, invariantMethodCall);
}
if (returnStatements.isEmpty())
blockStatement.addStatement(invariantMethodCall);
} else if (statement instanceof BlockStatement) {
final BlockStatement blockStatement = (BlockStatement) statement;
blockStatement.addStatement(invariantMethodCall);
} else {
final BlockStatement assertionBlock = new BlockStatement();
assertionBlock.addStatement(statement);
assertionBlock.addStatement(invariantMethodCall);
method.setCode(assertionBlock);
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class TailRecursiveASTTransformation method replaceRecursiveReturnsInsideClosures.
@SuppressWarnings("Instanceof")
private void replaceRecursiveReturnsInsideClosures(final MethodNode method, final Map<Integer, Map<String, Object>> positionMapping) {
Closure<Boolean> whenRecursiveReturn = new Closure<Boolean>(this, this) {
public Boolean doCall(Statement statement, boolean inClosure) {
if (!inClosure)
return false;
if (!(statement instanceof ReturnStatement)) {
return false;
}
Expression inner = ((ReturnStatement) statement).getExpression();
if (!(inner instanceof MethodCallExpression) && !(inner instanceof StaticMethodCallExpression)) {
return false;
}
return isRecursiveIn(inner, method);
}
};
Closure<Statement> replaceWithThrowLoopException = new Closure<Statement>(this, this) {
public Statement doCall(ReturnStatement statement) {
return new ReturnStatementToIterationConverter(AstHelper.recurByThrowStatement()).convert(statement, positionMapping);
}
};
StatementReplacer replacer = new StatementReplacer(whenRecursiveReturn, replaceWithThrowLoopException);
replacer.replaceIn(method.getCode());
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class TailRecursiveASTTransformation method replaceRecursiveReturnsOutsideClosures.
@SuppressWarnings("Instanceof")
private void replaceRecursiveReturnsOutsideClosures(final MethodNode method, final Map<Integer, Map<String, Object>> positionMapping) {
Closure<Boolean> whenRecursiveReturn = new Closure<Boolean>(this, this) {
public Boolean doCall(Statement statement, boolean inClosure) {
if (inClosure)
return false;
if (!(statement instanceof ReturnStatement)) {
return false;
}
Expression inner = ((ReturnStatement) statement).getExpression();
if (!(inner instanceof MethodCallExpression) && !(inner instanceof StaticMethodCallExpression)) {
return false;
}
return isRecursiveIn(inner, method);
}
};
Closure<Statement> replaceWithContinueBlock = new Closure<Statement>(this, this) {
public Statement doCall(ReturnStatement statement) {
return new ReturnStatementToIterationConverter().convert(statement, positionMapping);
}
};
StatementReplacer replacer = new StatementReplacer(whenRecursiveReturn, replaceWithContinueBlock);
replacer.replaceIn(method.getCode());
}
Aggregations