Search in sources :

Example 16 with CodeVisitorSupport

use of org.codehaus.groovy.ast.CodeVisitorSupport in project groovy by apache.

the class AstBuilder method visitCompactConstructorDeclaration.

@Override
public MethodNode visitCompactConstructorDeclaration(CompactConstructorDeclarationContext ctx) {
    ClassNode classNode = ctx.getNodeMetaData(CLASS_DECLARATION_CLASS_NODE);
    Objects.requireNonNull(classNode, "classNode should not be null");
    if (!AnnotatedNodeUtils.hasAnnotation(classNode, RECORD_TYPE_CLASS)) {
        createParsingFailedException("Only `record` can have compact constructor", ctx);
    }
    List<ModifierNode> modifierNodeList = ctx.getNodeMetaData(COMPACT_CONSTRUCTOR_DECLARATION_MODIFIERS);
    Objects.requireNonNull(modifierNodeList, "modifierNodeList should not be null");
    ModifierManager modifierManager = new ModifierManager(this, modifierNodeList);
    if (modifierManager.containsAny(VAR)) {
        throw createParsingFailedException("var cannot be used for compact constructor declaration", ctx);
    }
    String methodName = this.visitMethodName(ctx.methodName());
    String className = classNode.getNodeMetaData(CLASS_NAME);
    if (!methodName.equals(className)) {
        createParsingFailedException("Compact constructor should have the same name with record: " + className, ctx.methodName());
    }
    Parameter[] header = classNode.getNodeMetaData(RECORD_HEADER);
    Objects.requireNonNull(header, "record header should not be null");
    final Parameter[] parameters = cloneParams(header);
    Statement code = this.visitMethodBody(ctx.methodBody());
    code.visit(new CodeVisitorSupport() {

        @Override
        public void visitPropertyExpression(PropertyExpression expression) {
            final String propertyName = expression.getPropertyAsString();
            if (THIS_STR.equals(expression.getObjectExpression().getText()) && Arrays.stream(parameters).anyMatch(p -> p.getName().equals(propertyName))) {
                createParsingFailedException("Cannot assign a value to final variable `" + propertyName + "`", expression.getProperty());
            }
            super.visitPropertyExpression(expression);
        }
    });
    attachTupleConstructorAnnotationToRecord(classNode, parameters, code);
    return null;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) CodeVisitorSupport(org.codehaus.groovy.ast.CodeVisitorSupport) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) Parameter(org.codehaus.groovy.ast.Parameter) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ModifierNode(org.codehaus.groovy.ast.ModifierNode)

Aggregations

CodeVisitorSupport (org.codehaus.groovy.ast.CodeVisitorSupport)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)12 Parameter (org.codehaus.groovy.ast.Parameter)9 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)8 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)8 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)7 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)7 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)7 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)6 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)6 Expression (org.codehaus.groovy.ast.expr.Expression)6 ArrayList (java.util.ArrayList)5 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)5 FieldNode (org.codehaus.groovy.ast.FieldNode)5 Variable (org.codehaus.groovy.ast.Variable)5 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)5 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)5 Statement (org.codehaus.groovy.ast.stmt.Statement)5 ClassNode (org.codehaus.groovy.ast.ClassNode)4