Hi guys, I am really digging JavaParser!
I ran into an issue with removing a constructor. I am currently using version 3.2.12, as it is the only version supported at my workplace. I was also having some issues with ClassOrInterfaceDeclaration.remove(MethodDeclaration), but found a way to remove some methods with the following sample code:
cu.getTypes().forEach(typeDeclaration -> {
typeDeclaration.getMethods().forEach(methodDeclaration -> {
if(!methodDeclaration.getType().toString().equals("Some String")){
methodDeclaration.remove();
}
});
});
I was wondering if there is a way to remove a constructor. I would really appreciate some feedback.
Thanks!
Hi guys, I am really digging JavaParser!
Thank you
I am currently using version 3.2.12, as it is the only version supported at my workplace
I wonder why that is the case
Also constructor nodes should have the remove method. You just need to identify the constructor you want to remove. You can do that by looking at the signature typically
Adding to what @ftomassetti said: a ConstructorDeclaration is a BodyDeclaration, and every TypeDeclaration has a list of BodyDeclaration called members where you can find the constructors.
Thanks for the super fast response!
I was able to achieve the goal with the follwing:
classOrInterface.getMembers().removeIf(member-> member.getMetaModel().is(ConstructorDeclaration.class));
Heh, I never thought of using the meta model that way. Nice :-)
(But member probably already has isConstructorDeclaration())
You could have just used instanceof on the member, without the need to pass through the metamodel.
But still, long life to the metamodel :D