Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 529335

Summary: Organize import with constant on package private class triggers compilation error
Product: [Eclipse Project] JDT Reporter: Jean-Noel Rouvignac <djanoiup>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact: Manoj N Palat <manoj.palat>
Severity: normal    
Priority: P3 CC: jarthana, manoj.palat
Version: 4.7.2   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard: stalebug
Attachments:
Description Flags
Project showing the problem none

Description Jean-Noel Rouvignac CLA 2018-01-01 17:42:41 EST
Created attachment 272089 [details]
Project showing the problem

Calling organize import with a static import on a package private constant triggers a compilation error.

OrganizeImportTriggersCompileError.java:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package pkg.my;

import static pkg.other.Public.PUBLIC_CONSTANT;

public class OrganizeImportTriggersCompileError {

    public static void main(String[] args) {
        System.out.println(PUBLIC_CONSTANT);
    }

}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

PackagePrivate.java:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package pkg.other;

class PackagePrivate {
    public static final String PUBLIC_CONSTANT = "public constant";
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Public.java:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package pkg.other;

public class Public extends PackagePrivate {
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Runing organize import on OrganizeImportTriggersCompileError.java ends up with this code:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package pkg.my;

import static pkg.other.PackagePrivate.PUBLIC_CONSTANT;

public class OrganizeImportTriggersCompileError {

    public static void main(String[] args) {
        System.out.println(PUBLIC_CONSTANT);
    }

}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

which gives a compilation error because PackagePrivate is not accessible by OrganizeImportTriggersCompileError.
Comment 1 Jean-Noel Rouvignac CLA 2018-01-04 08:45:31 EST
The problem lies in `org.eclipse.jdt.core.dom.rewrite.ImportRewrite.addStaticImport(IBinding)` which blindly adds the import to `addedImports` collection as "fullyQualifiedNameOfDeclaringClass.fieldName" without checking whether the declaring class (`PackagePrivate`) is accessible from the current code (`OrganizeImportTriggersCompileError`).
Comment 2 Jay Arthanareeswaran CLA 2018-01-12 00:14:50 EST
(In reply to Jean-Noel Rouvignac from comment #1)
> The problem lies in
> `org.eclipse.jdt.core.dom.rewrite.ImportRewrite.addStaticImport(IBinding)`

I am not an expert in this area, but the analysis reasoning sounds plausible. Would you be able to put that in patch, preferably with a test case? TIA!
Comment 3 Eclipse Genie CLA 2020-04-17 13:27: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. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. 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.