Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Rewriting a node swallows a comment line
Rewriting a node swallows a comment line [message #987419] Mon, 26 November 2012 14:31 Go to next message
Artium Nihamkin is currently offline Artium NihamkinFriend
Messages: 2
Registered: November 2012
Junior Member
I was writing simple refactoring and noticed a strange thing. The comment line before the node I am rewriting disappears after refactoring. Also comments after the node in question are transferred inside the node and break the indentation in the new place. This is very strange and I want to ask if it is a bug in jdt or I did something wrong and oblivious.

For example my code suppose to refactor if-else statements in a way that the shortest branch would appear first.
when I try to refactor this:

		// pre
		if(a==6) { 
			a = 5;
			return false;
		} else { 

I get this:

		if (!(a==6)) { 
 else { 
			a = 5;
			return false;

The relevant snippet where the refactoring is done:
protected ASTRewrite createRewrite(CompilationUnit cu, SubProgressMonitor pm) {
		pm.beginTask("Creating rewrite operation...", 1);
		final AST ast = cu.getAST();
		final ASTRewrite rewrite = ASTRewrite.create(ast);
		cu.accept(new ASTVisitor() {
			public boolean visit(IfStatement node) {
				if (node.getStartPosition() > selection.getOffset() + selection.getLength() || node.getStartPosition() < selection.getOffset())
					return true;
				if (node.getElseStatement() == null)
					return true;
				int thenCount = countNodes(node.getThenStatement());
				int elseCount = countNodes(node.getElseStatement());
				if(thenCount <= elseCount)
					return true;
				IfStatement newnode = ast.newIfStatement();	
				PrefixExpression neg = negateExpression(ast, rewrite, node.getExpression());	
				newnode.setThenStatement((org.eclipse.jdt.core.dom.Statement) rewrite.createMoveTarget(node.getElseStatement()));
				newnode.setElseStatement((org.eclipse.jdt.core.dom.Statement) rewrite.createMoveTarget(node.getThenStatement()));	
				rewrite.replace(node, newnode, null);						
				return true;
		return rewrite;
Re: Rewriting a node swallows a comment line [message #997325 is a reply to message #987419] Sat, 05 January 2013 23:15 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1599
Registered: July 2009
Senior Member
You mave have figured this out meanwhile, the behavior can easily be explained if we just assume:
- "//pre" is associated to the if statement as a leading comment; since the if statement has been replaced with a new one (which has no leading comment) this comment is lost.
- "//post" is associated as a trailing comment to the else statement, by moving up the else statement also this comment is moved up.

I didn't check the code, but I believe the above comes close to the truth.

Previous Topic: need help in java!
Next Topic:No Java Perspective
Goto Forum:

Current Time: Wed May 23 20:58:46 GMT 2018

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

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

Back to the top