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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations