use of com.github.javaparser.metamodel.BaseNodeMetaModel in project javaparser by javaparser.
the class VisitorGenerator method generate.
public final void generate() throws Exception {
Log.info("Running %s", getClass().getSimpleName());
final CompilationUnit compilationUnit = sourceRoot.tryToParse(pkg, visitorClassName + ".java").getResult().get();
Optional<ClassOrInterfaceDeclaration> visitorClassOptional = compilationUnit.getClassByName(visitorClassName);
if (!visitorClassOptional.isPresent()) {
visitorClassOptional = compilationUnit.getInterfaceByName(visitorClassName);
}
final ClassOrInterfaceDeclaration visitorClass = visitorClassOptional.get();
JavaParserMetaModel.getNodeMetaModels().stream().filter((baseNodeMetaModel) -> !baseNodeMetaModel.isAbstract()).forEach(node -> generateVisitMethodForNode(node, visitorClass, compilationUnit));
after();
}
use of com.github.javaparser.metamodel.BaseNodeMetaModel in project javaparser by javaparser.
the class NodeGenerator method generate.
public final void generate() throws Exception {
Log.info("Running %s", getClass().getSimpleName());
for (BaseNodeMetaModel nodeMetaModel : JavaParserMetaModel.getNodeMetaModels()) {
Pair<CompilationUnit, ClassOrInterfaceDeclaration> result = parseNode(nodeMetaModel);
generateNode(nodeMetaModel, result.a, result.b);
}
after();
}
use of com.github.javaparser.metamodel.BaseNodeMetaModel in project javaparser by javaparser.
the class TypeCastingGenerator method generateNode.
@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) throws Exception {
Pair<CompilationUnit, ClassOrInterfaceDeclaration> baseCode = null;
for (BaseNodeMetaModel baseNode : baseNodes) {
if (nodeMetaModel == baseNode) {
// so we don't do anything when we *are* the base model.
return;
}
if (nodeMetaModel.isInstanceOfMetaModel(baseNode)) {
baseCode = parseNode(baseNode);
}
}
if (baseCode == null) {
// Node is not a child of one of the base nodes, so we don't want to generate this method for it.
return;
}
final String typeName = nodeMetaModel.getTypeName();
final ClassOrInterfaceDeclaration baseCoid = baseCode.b;
final CompilationUnit baseCu = baseCode.a;
generateIsType(baseCu, nodeCoid, baseCoid, typeName);
generateAsType(baseCu, nodeCoid, baseCoid, typeName);
generateToType(nodeCu, baseCu, nodeCoid, baseCoid, typeName);
generateIfType(nodeCu, baseCu, nodeCoid, baseCoid, typeName);
}
use of com.github.javaparser.metamodel.BaseNodeMetaModel 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);
}
Aggregations