[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.jdt] Changing Java source in a builder; which mechanism?

I have a builder, MyBuilder, that is meant to add a method to Java source 
files automatically.  MyBuilder is inserted in the build spec prior to the
JavaBuilder.

I have initially implemented this per
http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html
by using the ASWRewrite mechanism.  In the following snippet, I get the 
TextEdit, apply it to the IDocument, and the commit using a TextFileBuffer.

// Apply the edits to the in-memory document
// Get a tree of the edits to be applied to the document
TextEdit edits = rewriteRecorder.rewriteAST(boDocument, 
cu.getJavaProject().getOptions(true));
edits.apply(boDocument);
// commit changes to underlying file
textFileBuffer.commit(monitor, true);

However, I get an exception when invoking the apply() - it apparently needs 
to be executed in the UI thread.  I can wrap it in a 
Display.syncExec(Runnable), but that seems a bit off.

The question is how should I be doing this?

1 - Is ASTRewrite okay to use, or should I just modify the AST directly 
without creating TextEdits?
2 - The article indicated that TexdtFileBuffer should be used; is that the 
case for MyBuilder?  Is it required (such as if someone has the source open 
in an editor)?  The article seemed to be assuming the AST changes are in an 
editor, not a builder.
3 - Should I be using a Working Copy instead?

Any guidance would be helpful!

Thanks!
Brad