Bug 543258 - [organize imports] Removes comments between package declaration and import section
Summary: [organize imports] Removes comments between package declaration and import se...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.10   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-08 09:04 EST by Lukas Eder CLA
Modified: 2023-04-19 11:24 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lukas Eder CLA 2019-01-08 09:04:34 EST
Consider this code:

---------------------------------
package p;

/* comment */

import java.util.List;

public interface I {}
---------------------------------

Now, invoke the organize import feature, the following code will result

---------------------------------
package p;

public interface I {}
---------------------------------

The comment has gone. The expected output would have been:

---------------------------------
package p;

/* comment */

public interface I {}
---------------------------------
Comment 1 Jay Arthanareeswaran CLA 2019-01-08 10:52:33 EST
I am not an expert in this area, but things works fine with the following code:

package p;

import java.util.List;
/* comment */
import java.sql.*;

public interface I {
	Connection sql = null;
}

which makes me think this may be the expected behavior.

Lukas, can you tell me why you expect the comment to be kept.
Comment 2 Lukas Eder CLA 2019-01-08 12:39:45 EST
(In reply to Jay Arthanareeswaran from comment #1)
> Lukas, can you tell me why you expect the comment to be kept.

I'm using comments as tokens to be used by preprocessors, e.g.

---------------------------------
package p;

/* [start] */

import java.util.List; 

public interface I {}

/* [end] */
---------------------------------

The comment is not related to any specific line of code, e.g. I'm not commenting the import statement. The preprocessor will (in some cases) strip the contents between the above tags to produce still compilable output:

---------------------------------
package p;

/* [start] */





/* [end] */
---------------------------------

With the comment removed, that no longer works. But we don't have to talk about such an esoteric use case like mine. Here's a more probable one:


---------------------------------
package p;

// These import statements are blah blah

import java.util.List;
import java.util.Set; 
import java.util.Collection; 

// These import statements are xyz xyz

import java.io.Serializable;

public interface I extends Serializable, Set<Object>, Collection<Object> {}
---------------------------------

The output after organising imports is now:

---------------------------------
package p;

// These import statements are xyz xyz

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

public interface I extends Serializable, Set<Object>, Collection<Object> {}
---------------------------------

Which seems quite arbitrary. Maybe, this isn't so much of a bug report but a feature request: To implement more sophisticated organize import functionality. When comments are present around the import section, then ordering the imports could be avoided, but unnecessary imports could still be removed...
Comment 3 Dani Megert CLA 2019-04-26 05:37:07 EDT
Duplicate of bug 154559?
Comment 4 Lukas Eder CLA 2019-04-26 06:41:28 EDT
Looks similar indeed
Comment 5 Eclipse Genie CLA 2021-04-16 09:13:59 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 6 Eclipse Genie CLA 2023-04-19 11:24:44 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.