use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class GenericVisitorAdapterGenerator 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("R result;");
final String resultCheck = "if (result != null) return result;";
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.getNodeReference().isPresent()) {
if (field.isOptional()) {
body.addStatement(f("if (n.%s.isPresent()) {" + " result = n.%s.get().accept(this, arg);" + " %s" + "}", getter, getter, resultCheck));
} else {
body.addStatement(f("{ result = n.%s.accept(this, arg); %s }", getter, resultCheck));
}
}
}
body.addStatement("return null;");
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class GenericVisitorWithDefaultsGenerator 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("return defaultAction(n, arg);");
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class PropertyGenerator method generateGetter.
private void generateGetter(BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
final MethodDeclaration getter = new MethodDeclaration(EnumSet.of(PUBLIC), parseType(property.getTypeNameForGetter()), property.getGetterMethodName());
final BlockStmt body = getter.getBody().get();
body.getStatements().clear();
if (property.isOptional()) {
body.addStatement(f("return Optional.ofNullable(%s);", property.getName()));
} else {
body.addStatement(f("return %s;", property.getName()));
}
replaceWhenSameSignature(nodeCoid, getter);
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class NoCommentHashCodeVisitorGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
final BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
final SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("return ", "* 31 +", ";");
final List<PropertyMetaModel> propertyMetaModels = node.getAllPropertyMetaModels();
if (node.equals(JavaParserMetaModel.lineCommentMetaModel) || node.equals(JavaParserMetaModel.blockCommentMetaModel) || node.equals(JavaParserMetaModel.javadocCommentMetaModel) || propertyMetaModels.isEmpty()) {
builder.append("0");
} else {
for (PropertyMetaModel field : propertyMetaModels) {
final String getter = field.getGetterMethodName() + "()";
if (field.equals(JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel)) {
if (propertyMetaModels.size() == 1) {
builder.append("0");
break;
} else
continue;
}
// Is this field another AST node? Visit it.
if (field.getNodeReference().isPresent()) {
if (field.isOptional()) {
builder.append("(n.%s.isPresent()? n.%s.get().accept(this, arg):0)", getter, getter);
} else {
builder.append("(n.%s.accept(this, arg))", getter);
}
} else {
Class<?> type = field.getType();
if (type.equals(boolean.class)) {
builder.append("(n.%s?1:0)", getter);
} else if (type.equals(int.class)) {
builder.append("n.%s", getter);
} else {
builder.append("(n.%s.hashCode())", getter);
}
}
}
}
body.addStatement(parseStatement(builder.toString()));
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class ObjectIdentityHashCodeVisitorGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
final BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
body.addStatement("return n.hashCode();");
}
Aggregations