use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class ObjectIdentityEqualsVisitorGenerator 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 n == arg;");
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class ModifierVisitorGenerator 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 property : node.getAllPropertyMetaModels()) {
if (property.isNode()) {
if (property.isNodeList()) {
body.addStatement(f("NodeList<%s> %s = modifyList(n.%s(), arg);", property.getTypeNameGenerified(), property.getName(), property.getGetterMethodName()));
} else if (property.isOptional()) {
body.addStatement(f("%s %s = n.%s().map(s -> (%s) s.accept(this, arg)).orElse(null);", property.getTypeNameGenerified(), property.getName(), property.getGetterMethodName(), property.getTypeNameGenerified()));
} else {
body.addStatement(f("%s %s = (%s) n.%s().accept(this, arg);", property.getTypeNameGenerified(), property.getName(), property.getTypeNameGenerified(), property.getGetterMethodName()));
}
}
}
if (node.is(BinaryExpr.class)) {
body.addStatement("if (left == null) return right;");
body.addStatement("if (right == null) return left;");
} else {
final SeparatedItemStringBuilder collapseCheck = new SeparatedItemStringBuilder("if(", "||", ") return null;");
for (PropertyMetaModel property : node.getAllPropertyMetaModels()) {
if (property.isRequired() && property.isNode()) {
if (property.isNodeList()) {
if (property.isNonEmpty()) {
collapseCheck.append(f("%s.isEmpty()", property.getName()));
}
} else {
collapseCheck.append(f("%s==null", property.getName()));
}
}
}
if (collapseCheck.hasItems()) {
body.addStatement(collapseCheck.toString());
}
}
for (PropertyMetaModel property : node.getAllPropertyMetaModels()) {
if (property.isNode()) {
body.addStatement(f("n.%s(%s);", property.getSetterMethodName(), property.getName()));
}
}
body.addStatement("return n;");
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class PropertyGenerator method generateSetter.
private void generateSetter(BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
final String name = property.getName();
// Fill body
final String observableName = camelCaseToScreaming(name.startsWith("is") ? name.substring(2) : name);
declaredProperties.put(observableName, property);
if (property == JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel) {
// Node.comment has a very specific setter that we shouldn't overwrite.
return;
}
final MethodDeclaration setter = new MethodDeclaration(EnumSet.of(PUBLIC), parseType(property.getContainingNodeMetaModel().getTypeNameGenerified()), property.getSetterMethodName());
if (property.getContainingNodeMetaModel().hasWildcard()) {
setter.setType(parseType("T"));
}
setter.addAndGetParameter(property.getTypeNameForSetter(), property.getName()).addModifier(FINAL);
final BlockStmt body = setter.getBody().get();
body.getStatements().clear();
if (property.isRequired()) {
Class<?> type = property.getType();
if (property.isNonEmpty() && property.isSingular()) {
body.addStatement(f("assertNonEmpty(%s);", name));
} else if (type != boolean.class && type != int.class) {
body.addStatement(f("assertNotNull(%s);", name));
}
}
body.addStatement(f("if (%s == this.%s) { return (%s) this; }", name, name, setter.getType()));
body.addStatement(f("notifyPropertyChange(ObservableProperty.%s, this.%s, %s);", observableName, name, name));
if (property.isNode()) {
body.addStatement(f("if (this.%s != null) this.%s.setParentNode(null);", name, name));
}
body.addStatement(f("this.%s = %s;", name, name));
if (property.isNode()) {
body.addStatement(f("setAsParentNodeOf(%s);", name));
}
if (property.getContainingNodeMetaModel().hasWildcard()) {
body.addStatement(f("return (T) this;"));
} else {
body.addStatement(f("return this;"));
}
replaceWhenSameSignature(nodeCoid, setter);
if (property.getContainingNodeMetaModel().hasWildcard()) {
annotateSuppressWarnings(setter);
}
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class ReplaceMethodGenerator method generateNode.
@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
MethodDeclaration replaceNodeMethod = (MethodDeclaration) parseBodyDeclaration("public boolean replace(Node node, Node replacementNode) {}");
nodeCu.addImport(Node.class);
nodeMetaModel.getSuperNodeMetaModel().ifPresent(s -> annotateOverridden(replaceNodeMethod));
final BlockStmt body = replaceNodeMethod.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 {
check = attributeCheck(property, property.getSetterMethodName());
}
if (property.isOptional()) {
check = f("if (%s != null) { %s }", property.getName(), check);
}
body.addStatement(check);
}
if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
body.addStatement("return super.replace(node, replacementNode);");
} else {
body.addStatement("return false;");
}
addOrReplaceWhenSameSignature(nodeCoid, replaceNodeMethod);
}
Aggregations