Community
Participate
Working Groups
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.14) Gecko/2009090216 Ubuntu/8.04 (hardy) Firefox/3.0.14 Build Identifier: 20090920-1017 Using the "invert 'if' statement" quickfix gives invalid code when the statement is followed by a comment. Reproducible: Always Steps to Reproduce: 1. (no special setup required) 2. Enter the following conditional (including the single line comment following the condition) anywhere such code is valid: if(Math.random() < 0.5) { System.out.println("<"); } else { System.out.println(">"); } // some comment 3. Use the "invert 'if' statement" quickfix to invert. This results in the following invalid code: if(Math.random() >= 0.5) { System.out.println(">"); } // some comment else { System.out.println("<"); }
Can reproduce using I20091027-0100. Deepak, please investigate.
The transformation and comment shifting is done using ASTRewrite in JDT core. Also if there is a line gap in the comment(as shown below) the comment is left as it is if(Math.random() < 0.5) { System.out.println("<"); } else { System.out.println(">"); } // some comment
This might be a bug in the extended range for a node. The comment following the '}' should not be included within that range if it is located on the next line.
Thank you for taking care of this issue. In reply to the previous comment I'd argue that it shouldn't result in this behaviour even if the comment was on the same line as the closing '}'. This code: if(Math.random() < 0.5) { System.out.println("<"); } else { System.out.println(">"); } // some comment should also be valid after an "invert 'if' statement" quickfix, otherwise it would be 'invalidated', not 'inverted'.
Created attachment 162313 [details] Proposed fix + regression tests
Released for 3.6M7. Regression test added in: org.eclipse.jdt.core.tests.rewrite.describing.ASTRewritingStatementsTest#testIfStatementReplaceElse5
Verified for 3.6M7 using I20100425-2000.