Bug 250142 - [ast rewrite] convert control statement removes comments by mistake
Summary: [ast rewrite] convert control statement removes comments by mistake
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Sarika Sinha CLA
QA Contact:
URL:
Whiteboard: stalebug fix candidate
Keywords:
: 153264 261207 378139 385478 429079 453692 491720 501860 535440 (view as bug list)
Depends on:
Blocks: 200680
  Show dependency tree
 
Reported: 2008-10-08 13:41 EDT by Daniel Felix Ferber CLA
Modified: 2024-04-26 00:12 EDT (History)
15 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Felix Ferber CLA 2008-10-08 13:41:05 EDT
Consider following code in the Java text editor:
  if (param.getValue().equals("")) //$NON-NLS-1$
     throw new ValidationException(
         Messages.ErrorMessage);

Consider following save actions enabled:
 - Convert control statement bodies to block except for single return or throw statement
 - Remove unused imports
 - Add missing '@Override' annotations
 - Add missing '@Deprecated' annotations
 - Remove unnecessary '$NON-NLS$' tags
 - Remove trailing white spaces on all lines
 - Correct indentation

When indentation is corrected, the $NON-NLS-1$ is also removed, producing following code produced non externalizes string error:
  if (param.getValue().equals(""))
     throw new ValidationException(
         Messages.ErrorMessage);

However, this happens only if the "Convert control statement bodies to block" is set to "except for single return or throw statement".

Eclipse SDK
Version: 3.4.0
Build id: I20080617-2000

Eclipse Java Development Tools
Version: 3.4.0.v20080603-2000-7o7tEAXEFpPqqoXxgaBhhhq
Build id: I20080617-2000
Comment 1 Dani Megert CLA 2008-10-09 03:48:32 EDT
Simpler test case:
1. have an if-statement that has a comment on the same line, e.g.:
   if (true) // this is a test
2. run clean up with only 'Convert control statement bodies to block'
==> comment lost
Comment 2 Dani Megert CLA 2008-10-09 04:06:12 EDT
This is a bug in org.eclipse.jdt.core.dom.rewrite.ASTRewrite.set(ASTNode, StructuralPropertyDescriptor, Object, TextEditGroup).

I understand that the general problem of handling/loosing comments is very hard to solve in the ASTRewrite but I guess it should not be to hard to handle this concrete problem (i.e. EndOfLineComment after if).
Comment 3 Benno Baumgartner CLA 2008-10-09 05:52:57 EDT
*** Bug 153264 has been marked as a duplicate of this bug. ***
Comment 4 Benno Baumgartner CLA 2008-10-09 05:56:22 EDT
This bug has been reported several times now. Martin always refused to fix it but I think we should try to do something at least for this special case if possible.
Comment 5 Markus Keller CLA 2008-10-09 06:03:57 EDT
Even easier to reproduce with quick fixes that add/remove blocks:

                if (true) // quick fix change to block
                        System.out.println(-42);
                if (true) { // quick fix change to statement
                        System.out.println(42);
                }

CompilationUnit#getExtended*(ASTNode) should probably include these comments (map them to the statement on the next line).

Bug 153264 is a similar problem (but not exactly the same).
Comment 6 Dani Megert CLA 2008-10-09 06:07:22 EDT
>Bug 153264 is a similar problem (but not exactly the same).
Right, bug 153264 contains more (complex) samples/dups of comment problems. Please don't defer this one just because there are too many cases. We should try to at least solve the scenario where the EndOfLine comment is on the same line as the if and the else (if):

if (xxx) // comment
    ...
else if (xxx) // comment
else // comment

e.g.
if (xxx) // comment
    ...
==>
if (xxx) // comment { 
    ...
}
or (maybe?) better:
if (xxx) {  // comment
    ...
}
But we need to be careful that this doesn't become:
if (xxx) 
{  // comment
    ...
}
if such formatter prefs are used.


Comment 7 Markus Keller CLA 2012-08-03 11:52:28 EDT
*** Bug 378139 has been marked as a duplicate of this bug. ***
Comment 8 Markus Keller CLA 2012-08-03 11:52:39 EDT
*** Bug 385478 has been marked as a duplicate of this bug. ***
Comment 9 Markus Keller CLA 2012-08-03 11:53:05 EDT
*** Bug 261207 has been marked as a duplicate of this bug. ***
Comment 10 Martin Mathew CLA 2014-02-25 19:23:41 EST
*** Bug 429079 has been marked as a duplicate of this bug. ***
Comment 11 Noopur Gupta CLA 2014-12-01 03:38:45 EST
*** Bug 453692 has been marked as a duplicate of this bug. ***
Comment 12 Markus Keller CLA 2014-12-08 09:51:13 EST
The best solution for such problems is usually to call ASTRewrite#setTargetSourceRangeComputer(TargetSourceRangeComputer) and set a NoCommentSourceRangeComputer or a properly configured TightSourceRangeComputer.
Comment 13 Noopur Gupta CLA 2016-04-14 13:48:56 EDT
*** Bug 491720 has been marked as a duplicate of this bug. ***
Comment 14 Noopur Gupta CLA 2016-09-22 09:33:18 EDT
*** Bug 501860 has been marked as a duplicate of this bug. ***
Comment 15 Noopur Gupta CLA 2018-06-05 04:11:11 EDT
*** Bug 535440 has been marked as a duplicate of this bug. ***
Comment 16 Andrey Loskutov CLA 2019-01-17 07:08:13 EST
Still in 4.11... Just hit it on mass-cleanup of PDE code...
Comment 17 Eclipse Genie CLA 2022-05-06 13:33:25 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 18 Eclipse Genie CLA 2024-04-26 00:12:22 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.