Search in sources :

Example 11 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class NodeWithOptionalBlockStmt method createBody.

default BlockStmt createBody() {
    BlockStmt block = new BlockStmt();
    setBody(block);
    return block;
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 12 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class CloneVisitorGenerator method generateVisitMethodBody.

@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
    visitMethod.getParameters().forEach(p -> p.setFinal(true));
    BlockStmt body = visitMethod.getBody().get();
    body.getStatements().clear();
    for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
        final String getter = field.getGetterMethodName() + "()";
        if (field.getNodeReference().isPresent()) {
            if (field.isOptional() && field.isNodeList()) {
                body.addStatement(f("NodeList<%s> %s = cloneList(n.%s.orElse(null), arg);", field.getTypeNameGenerified(), field.getName(), getter));
            } else if (field.isNodeList()) {
                body.addStatement(f("NodeList<%s> %s = cloneList(n.%s, arg);", field.getTypeNameGenerified(), field.getName(), getter));
            } else {
                body.addStatement(f("%s %s = cloneNode(n.%s, arg);", field.getTypeNameGenerified(), field.getName(), getter));
            }
        }
    }
    SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder(f("%s r = new %s(", node.getTypeNameGenerified(), node.getTypeNameGenerified()), ",", ");");
    builder.append("n.getTokenRange().orElse(null)");
    for (PropertyMetaModel field : node.getConstructorParameters()) {
        if (field.getName().equals("comment")) {
            continue;
        }
        if (field.getNodeReference().isPresent()) {
            builder.append(field.getName());
        } else {
            builder.append(f("n.%s()", field.getGetterMethodName()));
        }
    }
    body.addStatement(builder.toString());
    body.addStatement("r.setComment(comment);");
    body.addStatement("return r;");
}
Also used : SeparatedItemStringBuilder(com.github.javaparser.utils.SeparatedItemStringBuilder) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 13 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class VoidVisitorAdapterGenerator method generateVisitMethodBody.

@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
    visitMethod.getParameters().forEach(p -> p.setFinal(true));
    BlockStmt body = visitMethod.getBody().get();
    body.getStatements().clear();
    for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
        final String getter = field.getGetterMethodName() + "()";
        if (field.getNodeReference().isPresent()) {
            if (field.isOptional() && field.isNodeList()) {
                body.addStatement(f("n.%s.ifPresent( l -> l.forEach( v -> v.accept(this, arg)));", getter));
            } else if (field.isOptional()) {
                body.addStatement(f("n.%s.ifPresent(l -> l.accept(this, arg));", getter));
            } else if (field.isNodeList()) {
                body.addStatement(f("n.%s.forEach(p -> p.accept(this, arg));", getter));
            } else {
                body.addStatement(f("n.%s.accept(this, arg);", getter));
            }
        }
    }
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 14 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class VoidVisitorWithDefaultsGenerator method generateVisitMethodBody.

@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
    visitMethod.getParameters().forEach(p -> p.setFinal(true));
    BlockStmt body = visitMethod.getBody().get();
    body.getStatements().clear();
    body.addStatement("defaultAction(n, arg);");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 15 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class RemoveMethodGenerator method generateNode.

@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
    MethodDeclaration removeNodeMethod = (MethodDeclaration) parseBodyDeclaration("public boolean remove(Node node) {}");
    nodeCu.addImport(Node.class);
    nodeMetaModel.getSuperNodeMetaModel().ifPresent(s -> annotateOverridden(removeNodeMethod));
    final BlockStmt body = removeNodeMethod.getBody().get();
    body.addStatement("if (node == null) return false;");
    for (PropertyMetaModel property : nodeMetaModel.getDeclaredPropertyMetaModels()) {
        if (!property.isNode()) {
            continue;
        }
        String check;
        if (property.isNodeList()) {
            check = nodeListCheck(property);
        } else {
            if (property.isRequired()) {
                continue;
            }
            String removeAttributeMethodName = generateRemoveMethodForAttribute(nodeCoid, nodeMetaModel, property);
            check = attributeCheck(property, removeAttributeMethodName);
        }
        if (property.isOptional()) {
            check = f("if (%s != null) { %s }", property.getName(), check);
        }
        body.addStatement(check);
    }
    if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
        body.addStatement("return super.remove(node);");
    } else {
        body.addStatement("return false;");
    }
    addOrReplaceWhenSameSignature(nodeCoid, removeNodeMethod);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Aggregations

BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)39 PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)9 SeparatedItemStringBuilder (com.github.javaparser.utils.SeparatedItemStringBuilder)5 Then (org.jbehave.core.annotations.Then)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)3 VoidType (com.github.javaparser.ast.type.VoidType)3 AClass (scenelib.annotations.el.AClass)3 InitializerDeclaration (com.github.javaparser.ast.body.InitializerDeclaration)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 Statement (com.github.javaparser.ast.stmt.Statement)2 ArrayType (com.github.javaparser.ast.type.ArrayType)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 Type (com.github.javaparser.ast.type.Type)2