Search in sources :

Example 6 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class GenericVisitorAdapterGenerator 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("R result;");
    final String resultCheck = "if (result != null) return result;";
    for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
        final String getter = field.getGetterMethodName() + "()";
        if (field.getNodeReference().isPresent()) {
            if (field.isOptional()) {
                body.addStatement(f("if (n.%s.isPresent()) {" + "   result = n.%s.get().accept(this, arg);" + "   %s" + "}", getter, getter, resultCheck));
            } else {
                body.addStatement(f("{ result = n.%s.accept(this, arg); %s }", getter, resultCheck));
            }
        }
    }
    body.addStatement("return null;");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 7 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class GenericVisitorWithDefaultsGenerator 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("return defaultAction(n, arg);");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 8 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class PropertyGenerator method generateGetter.

private void generateGetter(BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
    final MethodDeclaration getter = new MethodDeclaration(EnumSet.of(PUBLIC), parseType(property.getTypeNameForGetter()), property.getGetterMethodName());
    final BlockStmt body = getter.getBody().get();
    body.getStatements().clear();
    if (property.isOptional()) {
        body.addStatement(f("return Optional.ofNullable(%s);", property.getName()));
    } else {
        body.addStatement(f("return %s;", property.getName()));
    }
    replaceWhenSameSignature(nodeCoid, getter);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 9 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt 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 10 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.

the class ObjectIdentityHashCodeVisitorGenerator 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();
    body.addStatement("return n.hashCode();");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Aggregations

BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)39 PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)9 SeparatedItemStringBuilder (com.github.javaparser.utils.SeparatedItemStringBuilder)5 Then (org.jbehave.core.annotations.Then)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)3 VoidType (com.github.javaparser.ast.type.VoidType)3 AClass (scenelib.annotations.el.AClass)3 InitializerDeclaration (com.github.javaparser.ast.body.InitializerDeclaration)2 ReceiverParameter (com.github.javaparser.ast.body.ReceiverParameter)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 Statement (com.github.javaparser.ast.stmt.Statement)2 ArrayType (com.github.javaparser.ast.type.ArrayType)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 Type (com.github.javaparser.ast.type.Type)2