-1

I want use the Java Parser to add an inner class to an existing class.

I've tried

ClassOrInterfaceDeclaration classOrInterfaceType= cu.addClass("MyInnerClass", Modifier.PUBLIC,Modifier.STATIC);

but this adds a new class to the Java class i.e. not as an inner class.

Any help would be appreciated.

I want to insert a Builder into an existing class like

public class BuilderDemo {

public static class Builder{

}

}

I would like the node hierarchy look as follows:-

ClassOrInterface Nodes

Any help would be appreciated.

1 Answers1

0

You should first find the AST node of the class in which you want to add the inner class. What you are doing is adding the class in cu which I bet is a CompilationUnit, i.e., the whole file. So you are adding a class directly in the file.

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26