use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class MainConstructorGenerator method generateNode.
@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
if (nodeMetaModel.is(Node.class)) {
return;
}
ConstructorDeclaration constructor = new ConstructorDeclaration().setPublic(true).setName(nodeCoid.getNameAsString()).addParameter(TokenRange.class, "tokenRange").setJavadocComment("\n * This constructor is used by the parser and is considered private.\n ");
BlockStmt body = constructor.getBody();
SeparatedItemStringBuilder superCall = new SeparatedItemStringBuilder("super(", ", ", ");");
superCall.append("tokenRange");
for (PropertyMetaModel parameter : nodeMetaModel.getConstructorParameters()) {
constructor.addParameter(parameter.getTypeNameForSetter(), parameter.getName());
if (nodeMetaModel.getDeclaredPropertyMetaModels().contains(parameter)) {
body.addStatement(f("%s(%s);", parameter.getSetterMethodName(), parameter.getName()));
} else {
superCall.append(parameter.getName());
}
}
body.getStatements().addFirst(parseExplicitConstructorInvocationStmt(superCall.toString()));
body.addStatement("customInitialization();");
addOrReplaceWhenSameSignature(nodeCoid, constructor);
nodeCu.addImport(TokenRange.class);
}
use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class GenericListVisitorAdapterGenerator 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("List<R> result = new ArrayList<>();");
body.addStatement("List<R> tmp;");
final String resultCheck = "if (tmp != null) result.addAll(tmp);";
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.getNodeReference().isPresent()) {
if (field.isOptional()) {
body.addStatement(f("if (n.%s.isPresent()) {" + " tmp = n.%s.get().accept(this, arg);" + " %s" + "}", getter, getter, resultCheck));
} else {
body.addStatement(f("{ tmp = n.%s.accept(this, arg); %s }", getter, resultCheck));
}
}
}
body.addStatement("return result;");
Arrays.stream(new Class<?>[] { List.class, ArrayList.class }).filter(c -> compilationUnit.getImports().stream().noneMatch(i -> c.getName().equals(i.getName().asString()))).forEach(compilationUnit::addImport);
}
use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class HashCodeVisitorGenerator 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 (propertyMetaModels.isEmpty()) {
builder.append("0");
} else {
for (PropertyMetaModel field : propertyMetaModels) {
final String getter = field.getGetterMethodName() + "()";
// 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 NoCommentEqualsVisitorGenerator 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();
if (!(node.equals(JavaParserMetaModel.lineCommentMetaModel) || node.equals(JavaParserMetaModel.blockCommentMetaModel) || node.equals(JavaParserMetaModel.javadocCommentMetaModel))) {
body.addStatement(f("final %s n2 = (%s) arg;", node.getTypeName(), node.getTypeName()));
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.equals(JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel))
continue;
if (field.getNodeReference().isPresent()) {
if (field.isNodeList()) {
body.addStatement(f("if (!nodesEquals(n.%s, n2.%s)) return false;", getter, getter));
} else {
body.addStatement(f("if (!nodeEquals(n.%s, n2.%s)) return false;", getter, getter));
}
} else {
body.addStatement(f("if (!objEquals(n.%s, n2.%s)) return false;", getter, getter));
}
}
if (body.getStatements().size() == 1) {
// Only the cast line was added, but nothing is using it, so remove it again.
body.getStatements().clear();
}
}
body.addStatement("return true;");
}
use of com.github.javaparser.metamodel.PropertyMetaModel 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;");
}
Aggregations