use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class FinalVariableAnalyzer method returningBlock.
/**
* @return true if the block's last statement is a return or throw
*/
private boolean returningBlock(Statement block) {
if (block instanceof ReturnStatement || block instanceof ThrowStatement) {
return true;
}
if (!(block instanceof BlockStatement)) {
return false;
}
BlockStatement bs = (BlockStatement) block;
if (bs.getStatements().size() == 0) {
return false;
}
Statement last = DefaultGroovyMethods.last(bs.getStatements());
if (last instanceof ReturnStatement || last instanceof ThrowStatement) {
return true;
}
return false;
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class Java8 method setMethodDefaultValue.
private static void setMethodDefaultValue(final MethodNode mn, final Method m) {
ConstantExpression cExp = new ConstantExpression(m.getDefaultValue());
mn.setCode(new ReturnStatement(cExp));
mn.setAnnotationDefault(true);
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class AssertStatementCreationUtility method getReturnStatements.
/**
* Gets a list of {@link org.codehaus.groovy.ast.stmt.ReturnStatement} instances from the given {@link MethodNode}.
*
* @param method the {@link org.codehaus.groovy.ast.MethodNode} that holds the given <tt>lastStatement</tt>
* @return a {@link org.codehaus.groovy.ast.stmt.ReturnStatement} or <tt>null</tt>
*/
public static List<ReturnStatement> getReturnStatements(MethodNode method) {
final ReturnStatementVisitor returnStatementVisitor = new ReturnStatementVisitor();
returnStatementVisitor.visitMethod(method);
final List<ReturnStatement> returnStatements = returnStatementVisitor.getReturnStatements();
final BlockStatement blockStatement = (BlockStatement) method.getCode();
if (returnStatements.isEmpty()) {
final int statementCount = blockStatement.getStatements().size();
if (statementCount > 0) {
final Statement lastStatement = blockStatement.getStatements().get(statementCount - 1);
if (lastStatement instanceof ExpressionStatement) {
final ReturnStatement returnStatement = new ReturnStatement((ExpressionStatement) lastStatement);
returnStatement.setSourcePosition(lastStatement);
blockStatement.getStatements().remove(lastStatement);
blockStatement.addStatement(returnStatement);
returnStatements.add(returnStatement);
}
}
}
return returnStatements;
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy-core by groovy.
the class StaticTypeCheckingSupport method evaluateExpression.
/**
* A helper method that can be used to evaluate expressions as found in annotation
* parameters. For example, it will evaluate a constant, be it referenced directly as
* an integer or as a reference to a field.
*
* If this method throws an exception, then the expression cannot be evaluated on its own.
*
* @param expr the expression to be evaluated
* @param config the compiler configuration
* @return the result of the expression
*/
public static Object evaluateExpression(Expression expr, CompilerConfiguration config) {
String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$');
ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
ReturnStatement code = new ReturnStatement(expr);
node.addMethod(new MethodNode("eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code));
CompilerConfiguration copyConf = new CompilerConfiguration(config);
CompilationUnit cu = new CompilationUnit(copyConf);
cu.addClassNode(node);
cu.compile(Phases.CLASS_GENERATION);
@SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes());
try {
return aClass.getMethod("eval").invoke(null);
} catch (IllegalAccessException e) {
throw new GroovyBugError(e);
} catch (InvocationTargetException e) {
throw new GroovyBugError(e);
} catch (NoSuchMethodException e) {
throw new GroovyBugError(e);
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy-core by groovy.
the class DefaultGrailsDomainClassInjector method injectToStringMethod.
private void injectToStringMethod(ClassNode classNode) {
final boolean hasToString = /*GrailsASTUtils.*/
implementsZeroArgMethod(classNode, "toString");
if (!hasToString) {
GStringExpression ge = new GStringExpression(classNode.getName() + " : ${id}");
ge.addString(new ConstantExpression(classNode.getName() + " : "));
ge.addValue(new VariableExpression("id"));
Statement s = new ReturnStatement(ge);
MethodNode mn = new MethodNode("toString", Modifier.PUBLIC, new ClassNode(String.class), new Parameter[0], new ClassNode[0], s);
// if(LOG.isDebugEnabled()) {
// LOG.debug("[GrailsDomainInjector] Adding method [toString()] to class [" + classNode.getName() + "]");
// }
classNode.addMethod(mn);
}
}
Aggregations