Search in sources :

Example 66 with ReturnStatement

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);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Iterator(java.util.Iterator) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

Example 67 with ReturnStatement

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);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 68 with ReturnStatement

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);
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 69 with ReturnStatement

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);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 70 with ReturnStatement

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());
    }
}
Also used : ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Aggregations

ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)74 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)44 Statement (org.codehaus.groovy.ast.stmt.Statement)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)37 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)32 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 MethodNode (org.codehaus.groovy.ast.MethodNode)28 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)26 Expression (org.codehaus.groovy.ast.expr.Expression)25 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)24 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)23 ClassNode (org.codehaus.groovy.ast.ClassNode)22 Parameter (org.codehaus.groovy.ast.Parameter)22 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)21 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)18 ArrayList (java.util.ArrayList)17 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)17 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)15