views:

91

answers:

1

Hi,

i am trying to add method or variable declarations to org.eclipse.jdt.core.dom.CompilationUnit, 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

A: 

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

iain
Thanks for your answer, I'll try the rewriting. Thanks for the links!
Heinrich