Search in sources :

Example 1 with BaseNodeMetaModel

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();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Log(com.github.javaparser.utils.Log) SourceRoot(com.github.javaparser.utils.SourceRoot) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Name(com.github.javaparser.ast.expr.Name) Optional(java.util.Optional) CompilationUnit(com.github.javaparser.ast.CompilationUnit) PUBLIC(com.github.javaparser.ast.Modifier.PUBLIC) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BaseNodeMetaModel(com.github.javaparser.metamodel.BaseNodeMetaModel) JavaParserMetaModel(com.github.javaparser.metamodel.JavaParserMetaModel) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)

Example 2 with BaseNodeMetaModel

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();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BaseNodeMetaModel(com.github.javaparser.metamodel.BaseNodeMetaModel)

Example 3 with BaseNodeMetaModel

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BaseNodeMetaModel(com.github.javaparser.metamodel.BaseNodeMetaModel)

Example 4 with BaseNodeMetaModel

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);
}
Also used : CodeGenerationUtils.f(com.github.javaparser.utils.CodeGenerationUtils.f) Arrays(java.util.Arrays) List(java.util.List) SourceRoot(com.github.javaparser.utils.SourceRoot) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel) VisitorGenerator(com.github.javaparser.generator.VisitorGenerator) BaseNodeMetaModel(com.github.javaparser.metamodel.BaseNodeMetaModel) ArrayList(java.util.ArrayList) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 BaseNodeMetaModel (com.github.javaparser.metamodel.BaseNodeMetaModel)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 SourceRoot (com.github.javaparser.utils.SourceRoot)2 PUBLIC (com.github.javaparser.ast.Modifier.PUBLIC)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 Name (com.github.javaparser.ast.expr.Name)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 VisitorGenerator (com.github.javaparser.generator.VisitorGenerator)1 JavaParserMetaModel (com.github.javaparser.metamodel.JavaParserMetaModel)1 PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)1 CodeGenerationUtils.f (com.github.javaparser.utils.CodeGenerationUtils.f)1 Log (com.github.javaparser.utils.Log)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1