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;
}
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;");
}
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));
}
}
}
}
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);");
}
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);
}
Aggregations