Search in sources :

Example 31 with BlockStmt

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

the class ClassCreator method createCU.

/**
 * creates the compilation unit
 */
private static CompilationUnit createCU() {
    CompilationUnit cu = new CompilationUnit();
    // set the package
    cu.setPackageDeclaration(new PackageDeclaration(parseName("java.parser.test")));
    // or a shortcut
    cu.setPackageDeclaration("java.parser.test");
    // create the type declaration
    ClassOrInterfaceDeclaration type = cu.addClass("GeneratedClass");
    // create a method
    EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
    MethodDeclaration method = new MethodDeclaration(modifiers, new VoidType(), "main");
    modifiers.add(Modifier.STATIC);
    method.setModifiers(modifiers);
    type.addMember(method);
    // or a shortcut
    MethodDeclaration main2 = type.addMethod("main2", Modifier.PUBLIC, Modifier.STATIC);
    // add a parameter to the method
    Parameter param = new Parameter(parseClassOrInterfaceType("String"), "args");
    param.setVarArgs(true);
    method.addParameter(param);
    // or a shortcut
    main2.addAndGetParameter(String.class, "args").setVarArgs(true);
    // add a body to the method
    BlockStmt block = new BlockStmt();
    method.setBody(block);
    // add a statement to the method body
    NameExpr clazz = new NameExpr("System");
    FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
    MethodCallExpr call = new MethodCallExpr(field, "println");
    call.addArgument(new StringLiteralExpr("Hello World!"));
    block.addStatement(call);
    return cu;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) VoidType(com.github.javaparser.ast.type.VoidType) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Parameter(com.github.javaparser.ast.body.Parameter) Modifier(com.github.javaparser.ast.Modifier) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 32 with BlockStmt

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

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

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

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

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