use of com.github.javaparser.metamodel.PropertyMetaModel 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.metamodel.PropertyMetaModel 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.metamodel.PropertyMetaModel 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.metamodel.PropertyMetaModel 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);
}
use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class YamlPrinter method output.
public void output(Node node, String name, int level, StringBuilder builder) {
assertNotNull(node);
NodeMetaModel metaModel = node.getMetaModel();
List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
if (outputNodeType)
builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): ");
else
builder.append(System.lineSeparator() + indent(level) + name + ": ");
level++;
for (PropertyMetaModel a : attributes) {
builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \"" + a.getValue(node).toString() + "\"");
}
for (PropertyMetaModel sn : subNodes) {
Node nd = (Node) sn.getValue(node);
if (nd != null)
output(nd, sn.getName(), level, builder);
}
for (PropertyMetaModel sl : subLists) {
NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
if (nl != null && nl.isNonEmpty()) {
builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
String slName = sl.getName();
slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName;
for (Node nd : nl) output(nd, "- " + slName, level + 1, builder);
}
}
}
Aggregations