Adding method/variable declarations to org.eclipse.jdt.core.dom.CompilationUnit
i am trying to add method or variable declarations to org.eclipse.jdt.core.dom.Co开发者_Python百科mpilationUnit, but I can't figure out how to achieve that.
If I am using CompilationUnit.types().add(...) the element is added as a sibling, not as a child element.
I've really searched a lot now, but I don't believe it's such a big deal.
Thanks for your answers!
heinrich
If you sample code above is correct you are adding your nodes to the list of types not to the type toplevel type.
To add to the first type you should use
AbstractTypeDeclaration type = (AbstractTypeDeclaration)CompilationUnit.types().get(0);
type.add(...);
However you should consider using a rewriter to perform this instead of modifying the AST directly.
For more info see
- http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html
- http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/rewrite/package-summary.html
精彩评论