Search in sources :

Example 6 with PropertyMetaModel

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

Example 7 with PropertyMetaModel

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

Example 8 with PropertyMetaModel

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

Example 9 with PropertyMetaModel

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

Example 10 with PropertyMetaModel

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);
        }
    }
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) 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