use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ArrayTypeTest method setMethodDeclarationWithArrays.
@Test
public void setMethodDeclarationWithArrays() {
MethodDeclaration method = parseBodyDeclaration("int[][] a()[][] {}").asMethodDeclaration();
method.setType(new ArrayType(new ArrayType(parseClassOrInterfaceType("Blob"))));
assertEquals("Blob[][] a() {" + EOL + "}", method.toString());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ArrayTypeTest method setParameterWithArrays.
@Test
public void setParameterWithArrays() {
MethodDeclaration method = parseBodyDeclaration("void a(int[][] a[][]) {}").asMethodDeclaration();
method.getParameter(0).setType(new ArrayType(new ArrayType(parseClassOrInterfaceType("Blob"))));
assertEquals("void a(Blob[][] a) {" + EOL + "}", method.toString());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ArrayTypeTest method getMethodDeclarationWithArrays.
@Test
public void getMethodDeclarationWithArrays() {
MethodDeclaration methodDeclaration = parseBodyDeclaration("@C int @A[] a() @B[] {}").asMethodDeclaration();
ArrayType arrayType1 = methodDeclaration.getType().asArrayType();
ArrayType arrayType2 = arrayType1.getComponentType().asArrayType();
Type elementType = arrayType2.getComponentType();
assertThat(elementType).isInstanceOf(PrimitiveType.class);
assertThat(arrayType1.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("A")));
assertThat(arrayType2.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("B")));
assertThat(methodDeclaration.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("C")));
assertThat(methodDeclaration.getType().getParentNode().get()).isSameAs(methodDeclaration);
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class RemoveMethodGenerator method generateNode.
@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
MethodDeclaration removeNodeMethod = (MethodDeclaration) parseBodyDeclaration("public boolean remove(Node node) {}");
nodeCu.addImport(Node.class);
nodeMetaModel.getSuperNodeMetaModel().ifPresent(s -> annotateOverridden(removeNodeMethod));
final BlockStmt body = removeNodeMethod.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 {
if (property.isRequired()) {
continue;
}
String removeAttributeMethodName = generateRemoveMethodForAttribute(nodeCoid, nodeMetaModel, property);
check = attributeCheck(property, removeAttributeMethodName);
}
if (property.isOptional()) {
check = f("if (%s != null) { %s }", property.getName(), check);
}
body.addStatement(check);
}
if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
body.addStatement("return super.remove(node);");
} else {
body.addStatement("return false;");
}
addOrReplaceWhenSameSignature(nodeCoid, removeNodeMethod);
}
use of com.github.javaparser.ast.body.MethodDeclaration 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;
}
Aggregations