Search in sources :

Example 1 with SeparatedItemStringBuilder

use of com.github.javaparser.utils.SeparatedItemStringBuilder 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 2 with SeparatedItemStringBuilder

use of com.github.javaparser.utils.SeparatedItemStringBuilder in project javaparser by javaparser.

the class ModifierValidator method visit.

@Override
public void visit(MethodDeclaration n, ProblemReporter reporter) {
    if (n.isAbstract()) {
        final SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("Cannot be 'abstract' and also '", "', '", "'.");
        for (Modifier m : asList(PRIVATE, STATIC, FINAL, NATIVE, STRICTFP, SYNCHRONIZED)) {
            if (n.getModifiers().contains(m)) {
                builder.append(m.asString());
            }
        }
        if (builder.hasItems()) {
            reporter.report(n, builder.toString());
        }
    }
    if (n.getParentNode().isPresent()) {
        if (n.getParentNode().get() instanceof ClassOrInterfaceDeclaration) {
            if (((ClassOrInterfaceDeclaration) n.getParentNode().get()).isInterface()) {
                if (hasDefaultAndStaticInterfaceMethods) {
                    if (hasPrivateInterfaceMethods) {
                        validateModifiers(n, reporter, interfaceWithStaticAndDefaultAndPrivate);
                    } else {
                        validateModifiers(n, reporter, interfaceWithStaticAndDefault);
                    }
                } else {
                    validateModifiers(n, reporter, interfaceWithNothingSpecial);
                }
            } else {
                validateModifiers(n, reporter, PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP);
            }
        }
    }
    n.getParameters().forEach(p -> validateModifiers(p, reporter, FINAL));
    super.visit(n, reporter);
}
Also used : SeparatedItemStringBuilder(com.github.javaparser.utils.SeparatedItemStringBuilder) Modifier(com.github.javaparser.ast.Modifier)

Example 3 with SeparatedItemStringBuilder

use of com.github.javaparser.utils.SeparatedItemStringBuilder 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 4 with SeparatedItemStringBuilder

use of com.github.javaparser.utils.SeparatedItemStringBuilder in project javaparser by javaparser.

the class ModifierValidator method validateAtMostOneOf.

private <T extends NodeWithModifiers<?> & NodeWithTokenRange<?>> void validateAtMostOneOf(T t, ProblemReporter reporter, Modifier... modifiers) {
    List<Modifier> foundModifiers = new ArrayList<>();
    for (Modifier m : modifiers) {
        if (t.getModifiers().contains(m)) {
            foundModifiers.add(m);
        }
    }
    if (foundModifiers.size() > 1) {
        SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("Can have only one of '", "', '", "'.");
        for (Modifier m : foundModifiers) {
            builder.append(m.asString());
        }
        reporter.report(t, builder.toString());
    }
}
Also used : SeparatedItemStringBuilder(com.github.javaparser.utils.SeparatedItemStringBuilder) ArrayList(java.util.ArrayList) Modifier(com.github.javaparser.ast.Modifier)

Example 5 with SeparatedItemStringBuilder

use of com.github.javaparser.utils.SeparatedItemStringBuilder 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)

Aggregations

SeparatedItemStringBuilder (com.github.javaparser.utils.SeparatedItemStringBuilder)7 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)5 PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)5 Modifier (com.github.javaparser.ast.Modifier)2 TokenRange (com.github.javaparser.TokenRange)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 ArrayList (java.util.ArrayList)1