Bug 293558

Summary: [quick assist] "Invert if statement" fails when comment follows
Product: [Eclipse Project] JDT Reporter: Olaf Kock <eclipse-bugzilla>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, markus.kell.r, Olivier_Thomann
Version: 3.5.1   
Target Milestone: 3.6 M7   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Proposed fix + regression tests none

Description Olaf Kock CLA 2009-10-28 07:31:00 EDT
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("<");
  }
Comment 1 Dani Megert CLA 2009-10-29 04:05:49 EDT
Can reproduce using I20091027-0100.

Deepak, please investigate.
Comment 2 Deepak Azad CLA 2009-10-30 08:17:45 EDT
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
Comment 3 Olivier Thomann CLA 2009-11-26 13:16:02 EST
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.
Comment 4 Olaf Kock CLA 2009-11-27 05:02:22 EST
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'.
Comment 5 Olivier Thomann CLA 2010-03-17 12:18:16 EDT
Created attachment 162313 [details]
Proposed fix + regression tests
Comment 6 Olivier Thomann CLA 2010-03-17 12:18:57 EDT
Released for 3.6M7.
Regression test added in:
org.eclipse.jdt.core.tests.rewrite.describing.ASTRewritingStatementsTest#testIfStatementReplaceElse5
Comment 7 Frederic Fusier CLA 2010-04-26 12:37:07 EDT
Verified for 3.6M7 using I20100425-2000.