use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy-core by groovy.
the class Verifier method checkReturnInObjectInitializer.
private void checkReturnInObjectInitializer(List init) {
CodeVisitorSupport cvs = new CodeVisitorSupport() {
@Override
public void visitClosureExpression(ClosureExpression expression) {
// return is OK in closures in object initializers
}
public void visitReturnStatement(ReturnStatement statement) {
throw new RuntimeParserException("'return' is not allowed in object initializer", statement);
}
};
for (Iterator iterator = init.iterator(); iterator.hasNext(); ) {
Statement stm = (Statement) iterator.next();
stm.visit(cvs);
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy-core by groovy.
the class ExtendedVerifier method visitConstructorOrMethod.
private void visitConstructorOrMethod(MethodNode node, int methodTarget) {
visitAnnotations(node, methodTarget);
for (int i = 0; i < node.getParameters().length; i++) {
Parameter parameter = node.getParameters()[i];
visitAnnotations(parameter, AnnotationNode.PARAMETER_TARGET);
}
if (this.currentClass.isAnnotationDefinition() && !node.isStaticConstructor()) {
ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
visitor.setReportClass(currentClass);
visitor.checkReturnType(node.getReturnType(), node);
if (node.getParameters().length > 0) {
addError("Annotation members may not have parameters.", node.getParameters()[0]);
}
if (node.getExceptions().length > 0) {
addError("Annotation members may not have a throws clause.", node.getExceptions()[0]);
}
ReturnStatement code = (ReturnStatement) node.getCode();
if (code != null) {
visitor.visitExpression(node.getName(), code.getExpression(), node.getReturnType());
visitor.checkCircularReference(currentClass, node.getReturnType(), code.getExpression());
}
this.source.getErrorCollector().addCollectorContents(errorCollector);
}
Statement code = node.getCode();
if (code != null) {
code.visit(this);
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy-core by groovy.
the class Java5 method setMethodDefaultValue.
private void setMethodDefaultValue(MethodNode mn, Method m) {
Object defaultValue = m.getDefaultValue();
ConstantExpression cExp = ConstantExpression.NULL;
if (defaultValue != null)
cExp = new ConstantExpression(defaultValue);
mn.setCode(new ReturnStatement(cExp));
mn.setAnnotationDefault(true);
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy-core by groovy.
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.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-core by groovy.
the class AnnotationVisitor method checkCircularReference.
public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp) {
if (!isValidAnnotationClass(attrType))
return;
if (!(startExp instanceof AnnotationConstantExpression)) {
addError("Found '" + startExp.getText() + "' when expecting an Annotation Constant", startExp);
return;
}
AnnotationConstantExpression ace = (AnnotationConstantExpression) startExp;
AnnotationNode annotationNode = (AnnotationNode) ace.getValue();
if (annotationNode.getClassNode().equals(searchClass)) {
addError("Circular reference discovered in " + searchClass.getName(), startExp);
return;
}
ClassNode cn = annotationNode.getClassNode();
for (MethodNode method : cn.getMethods()) {
if (method.getReturnType().equals(searchClass)) {
addError("Circular reference discovered in " + cn.getName(), startExp);
}
ReturnStatement code = (ReturnStatement) method.getCode();
if (code == null)
continue;
checkCircularReference(searchClass, method.getReturnType(), code.getExpression());
}
}
Aggregations