Search in sources :

Example 36 with BlockStmt

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

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

Example 37 with BlockStmt

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

Example 38 with BlockStmt

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

the class PropertyGenerator method generateSetter.

private void generateSetter(BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
    final String name = property.getName();
    // Fill body
    final String observableName = camelCaseToScreaming(name.startsWith("is") ? name.substring(2) : name);
    declaredProperties.put(observableName, property);
    if (property == JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel) {
        // Node.comment has a very specific setter that we shouldn't overwrite.
        return;
    }
    final MethodDeclaration setter = new MethodDeclaration(EnumSet.of(PUBLIC), parseType(property.getContainingNodeMetaModel().getTypeNameGenerified()), property.getSetterMethodName());
    if (property.getContainingNodeMetaModel().hasWildcard()) {
        setter.setType(parseType("T"));
    }
    setter.addAndGetParameter(property.getTypeNameForSetter(), property.getName()).addModifier(FINAL);
    final BlockStmt body = setter.getBody().get();
    body.getStatements().clear();
    if (property.isRequired()) {
        Class<?> type = property.getType();
        if (property.isNonEmpty() && property.isSingular()) {
            body.addStatement(f("assertNonEmpty(%s);", name));
        } else if (type != boolean.class && type != int.class) {
            body.addStatement(f("assertNotNull(%s);", name));
        }
    }
    body.addStatement(f("if (%s == this.%s) { return (%s) this; }", name, name, setter.getType()));
    body.addStatement(f("notifyPropertyChange(ObservableProperty.%s, this.%s, %s);", observableName, name, name));
    if (property.isNode()) {
        body.addStatement(f("if (this.%s != null) this.%s.setParentNode(null);", name, name));
    }
    body.addStatement(f("this.%s = %s;", name, name));
    if (property.isNode()) {
        body.addStatement(f("setAsParentNodeOf(%s);", name));
    }
    if (property.getContainingNodeMetaModel().hasWildcard()) {
        body.addStatement(f("return (T) this;"));
    } else {
        body.addStatement(f("return this;"));
    }
    replaceWhenSameSignature(nodeCoid, setter);
    if (property.getContainingNodeMetaModel().hasWildcard()) {
        annotateSuppressWarnings(setter);
    }
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 39 with BlockStmt

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

the class ReplaceMethodGenerator method generateNode.

@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
    MethodDeclaration replaceNodeMethod = (MethodDeclaration) parseBodyDeclaration("public boolean replace(Node node, Node replacementNode) {}");
    nodeCu.addImport(Node.class);
    nodeMetaModel.getSuperNodeMetaModel().ifPresent(s -> annotateOverridden(replaceNodeMethod));
    final BlockStmt body = replaceNodeMethod.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 {
            check = attributeCheck(property, property.getSetterMethodName());
        }
        if (property.isOptional()) {
            check = f("if (%s != null) { %s }", property.getName(), check);
        }
        body.addStatement(check);
    }
    if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
        body.addStatement("return super.replace(node, replacementNode);");
    } else {
        body.addStatement("return false;");
    }
    addOrReplaceWhenSameSignature(nodeCoid, replaceNodeMethod);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) 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