Search in sources :

Example 16 with BlockStmt

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

the class RemoveMethodGenerator method generateRemoveMethodForAttribute.

private String generateRemoveMethodForAttribute(ClassOrInterfaceDeclaration nodeCoid, BaseNodeMetaModel nodeMetaModel, PropertyMetaModel property) {
    final String methodName = "remove" + capitalize(property.getName());
    final MethodDeclaration removeMethod = (MethodDeclaration) parseBodyDeclaration(f("public %s %s() {}", nodeMetaModel.getTypeName(), methodName));
    final BlockStmt block = removeMethod.getBody().get();
    block.addStatement(f("return %s((%s) null);", property.getSetterMethodName(), property.getTypeNameForSetter()));
    addOrReplaceWhenSameSignature(nodeCoid, removeMethod);
    return methodName;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 17 with BlockStmt

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

the class FieldDeclaration method createGetter.

/**
 * Create a getter for this field, <b>will only work if this field declares only 1 identifier and if this field is
 * already added to a ClassOrInterfaceDeclaration</b>
 *
 * @return the {@link MethodDeclaration} created
 * @throws IllegalStateException if there is more than 1 variable identifier or if this field isn't attached to a
 *             class or enum
 */
public MethodDeclaration createGetter() {
    if (getVariables().size() != 1)
        throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
    ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
    EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
    if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
        throw new IllegalStateException("You can use this only when the field is attached to a class or an enum");
    VariableDeclarator variable = getVariables().get(0);
    String fieldName = variable.getId().getName();
    String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
    final MethodDeclaration getter;
    if (parentClass != null)
        getter = parentClass.addMethod("get" + fieldNameUpper, PUBLIC);
    else
        getter = parentEnum.addMethod("get" + fieldNameUpper, PUBLIC);
    getter.setType(variable.getType());
    BlockStmt blockStmt = new BlockStmt();
    getter.setBody(blockStmt);
    blockStmt.addStatement(new ReturnStmt(name(fieldName)));
    return getter;
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 18 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project checker-framework by typetools.

the class ToIndexFileConverter method visit.

@Override
public Void visit(ConstructorDeclaration decl, AElement elem) {
    List<Parameter> params = decl.getParameters();
    List<AnnotationExpr> rcvrAnnos = decl.getAnnotations();
    BlockStmt body = decl.getBody();
    StringBuilder sb = new StringBuilder("<init>(");
    AClass clazz = (AClass) elem;
    AMethod method;
    // an empty list.
    if (params != null) {
        for (Parameter param : params) {
            Type ptype = param.getType();
            sb.append(getJVML(ptype));
        }
    }
    sb.append(")V");
    method = clazz.methods.vivify(sb.toString());
    visitDecl(decl, method);
    if (params != null) {
        for (int i = 0; i < params.size(); i++) {
            Parameter param = params.get(i);
            AField field = method.parameters.vivify(i);
            visitType(param.getType(), field.type);
        }
    }
    if (rcvrAnnos != null) {
        for (AnnotationExpr expr : rcvrAnnos) {
            Annotation anno = extractAnnotation(expr);
            method.receiver.tlAnnotationsHere.add(anno);
        }
    }
    return body == null ? null : body.accept(this, method);
// return super.visit(decl, elem);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) ReferenceType(com.github.javaparser.ast.type.ReferenceType) VoidType(com.github.javaparser.ast.type.VoidType) ArrayType(com.github.javaparser.ast.type.ArrayType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) WildcardType(com.github.javaparser.ast.type.WildcardType) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) AClass(scenelib.annotations.el.AClass) AMethod(scenelib.annotations.el.AMethod) Annotation(scenelib.annotations.Annotation) AField(scenelib.annotations.el.AField)

Example 19 with BlockStmt

use of com.github.javaparser.ast.stmt.BlockStmt in project checker-framework by typetools.

the class ToIndexFileConverter method visit.

@Override
public Void visit(InitializerDeclaration decl, AElement elem) {
    BlockStmt block = decl.getBody();
    AClass clazz = (AClass) elem;
    block.accept(this, clazz.methods.vivify(decl.isStatic() ? "<clinit>" : "<init>"));
    return null;
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) AClass(scenelib.annotations.el.AClass)

Example 20 with BlockStmt

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

the class FieldDeclaration method createSetter.

/**
 * Create a setter for this field, <b>will only work if this field declares only 1 identifier and if this field is
 * already added to a ClassOrInterfaceDeclaration</b>
 *
 * @return the {@link MethodDeclaration} created
 * @throws IllegalStateException if there is more than 1 variable identifier or if this field isn't attached to a
 *             class or enum
 */
public MethodDeclaration createSetter() {
    if (getVariables().size() != 1)
        throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
    ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
    EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
    if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
        throw new IllegalStateException("You can use this only when the field is attached to a class or an enum");
    VariableDeclarator variable = getVariables().get(0);
    String fieldName = variable.getId().getName();
    String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
    final MethodDeclaration setter;
    if (parentClass != null)
        setter = parentClass.addMethod("set" + fieldNameUpper, PUBLIC);
    else
        setter = parentEnum.addMethod("set" + fieldNameUpper, PUBLIC);
    setter.setType(VOID_TYPE);
    setter.getParameters().add(new Parameter(variable.getType(), new VariableDeclaratorId(fieldName)));
    BlockStmt blockStmt2 = new BlockStmt();
    setter.setBody(blockStmt2);
    blockStmt2.addStatement(new AssignExpr(new NameExpr("this." + fieldName), new NameExpr(fieldName), Operator.assign));
    return setter;
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

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