Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ASTRequestor to remove MethodDeclaration nodes
ASTRequestor to remove MethodDeclaration nodes [message #648645] Thu, 13 January 2011 18:50 Go to next message
Christophe Bismuth is currently offline Christophe BismuthFriend
Messages: 67
Registered: July 2009
Location: Paris, France
Member

Hi,

I've started to develop a JDT plugin to sort methods in Java files.
I've read the tutorial "Unleashing the Power of Refactoring" from
"Eclipse Corner Articles".

Everything is set up in my project (same layout as the sample code
provided in the tutorial). The first step I'm trying to climb is to
remove method nodes one by one in a file, this is not the final strategy
I plan, just to play with nodes.

I succeed in finding node, removing them from the ASTRewrite, creating
TextFileChange. But the refactored source code remains not modified.

Here is my requestor below, does anyone can see a mistake in my code?

Thank you,
Chris

// The "source" variable is an ICompilationUnit instance
final Document document = new Document(source.getSource());

// The "node" variable is a CompilationUnit instance
final AST ast = node.getAST();

final ASTRewrite rewriter = ASTRewrite.create(ast);
final TextEditGroup group = new TextEditGroup(SortClassMembersRefactoring.class.getName());

final MethodDeclarationFinder finder = new MethodDeclarationFinder();
final Collection<MethodDeclaration> methodDeclarationNodes = finder.getMethodDeclarations(node);

for (final MethodDeclaration methodDeclarationNode : methodDeclarationNodes) {
      // I enter here
      rewriter.remove(methodDeclarationNode, group);
}

if (methodDeclarationNodes.size() > 0) {
      // I enter here
      final TextEdit edit = rewriter.rewriteAST(document, null);
      edit.apply(document);
      TextFileChange change = SortClassMembersRefactoring.this._changes.get(source);
      if (change == null) {
          // I enter here
          change = new TextFileChange(source.getElementName(), (IFile) source.getResource());
          change.setTextType("java");
          change.setEdit(edit);
      } else {
          change.getEdit().addChild(edit);
      }
      SortClassMembersRefactoring.this._changes.put(source, change);
}

[Updated on: Mon, 17 January 2011 11:02]

Report message to a moderator

Re: ASTRequestor to remove MethodDeclaration nodes [message #649030 is a reply to message #648645] Mon, 17 January 2011 10:57 Go to previous message
Christophe Bismuth is currently offline Christophe BismuthFriend
Messages: 67
Registered: July 2009
Location: Paris, France
Member

The problem wasn't in the requestor, but rather in the way I used the JDT refactoring infrastructure.

Chris
Previous Topic:Retrieve superclass or interface which declares a method
Next Topic:How to remove unecessary "import" statements? Opposite of ctrl+shift+O ?
Goto Forum:
  


Current Time: Fri Nov 28 23:28:43 GMT 2014

Powered by FUDForum. Page generated in 0.03118 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software