class A{
int x = 10;
}
This is A.java
I want to get NewA.java
class NewA{
int x = 10;
Sting text = "B";
}
I want to add a variable using javaparser.
class A{
int x = 10;
}
This is A.java
I want to get NewA.java
class NewA{
int x = 10;
Sting text = "B";
}
I want to add a variable using javaparser.
You need to do this:
The first point is trivial, just use the JavaParser.parse method. You will get a CompilationUnit. In the example you shown you are adding a field in a class declaration, so you need first to get that class declaration. Call getTypes and look into that list for the declaration you want or just call getClassByName.
Once you have your class declaration you can call addMember on it. In your example you are adding a field so you need to instantiate a FieldDeclaration.
Once you are done you take your CompilationUnit and call toString. You will get back the modified source code.
Source: I am a JavaParser committer