Artium
Artium

Reputation: 5329

How to find out if any changes were recorded in an ASTRewrite

Assuming that I have an instance of ASTRewrite of which I can not make assumptions (ie returned by a overridable method). I would like to test if the rewrite actually stores any modifications to the compilation unit or otherwise it is merely the "identity" change. How is it possible do this?

I went over the methods of ASTRewrite but was not able to find anything that would help. So I guess the correct way to do it is not straightforward.

Upvotes: 1

Views: 89

Answers (1)

Unni Kris
Unni Kris

Reputation: 3095

You can determine the edits applied on the AST using TextEdit.

 TextEdit edits = rewrite.rewriteAST(document, compUnit.getJavaProject().getOptions(true));

The edits will be empty if no changes are done on the AST. Else it will contain the aditions and deletions to the AST with offset.

You can check whether the TextEdit is empty using the getLength() method.

// If edits available, write them to the file.
if (edits.getLength() != 0) {
     // do something
}

More help can be found from the below link :

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_manip.htm

Upvotes: 1

Related Questions