Search in sources :

Example 11 with PropertyMetaModel

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);
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) SeparatedItemStringBuilder(com.github.javaparser.utils.SeparatedItemStringBuilder) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) TokenRange(com.github.javaparser.TokenRange) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 12 with PropertyMetaModel

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);
}
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)

Example 13 with PropertyMetaModel

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()));
}
Also used : SeparatedItemStringBuilder(com.github.javaparser.utils.SeparatedItemStringBuilder) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 14 with PropertyMetaModel

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;");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 15 with PropertyMetaModel

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;");
}
Also used : SeparatedItemStringBuilder(com.github.javaparser.utils.SeparatedItemStringBuilder) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Aggregations

PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)17 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 SeparatedItemStringBuilder (com.github.javaparser.utils.SeparatedItemStringBuilder)5 Node (com.github.javaparser.ast.Node)4 NodeList (com.github.javaparser.ast.NodeList)4 NodeMetaModel (com.github.javaparser.metamodel.NodeMetaModel)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)3 ArrayList (java.util.ArrayList)2 TokenRange (com.github.javaparser.TokenRange)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 VisitorGenerator (com.github.javaparser.generator.VisitorGenerator)1 BaseNodeMetaModel (com.github.javaparser.metamodel.BaseNodeMetaModel)1 CodeGenerationUtils.f (com.github.javaparser.utils.CodeGenerationUtils.f)1 SourceRoot (com.github.javaparser.utils.SourceRoot)1 Arrays (java.util.Arrays)1 List (java.util.List)1