Bug 545302 - Misleading compile error when using static imports
Summary: Misleading compile error when using static imports
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.10   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2019-03-12 09:23 EDT by Emond Papegaaij CLA
Modified: 2023-05-12 07:38 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Emond Papegaaij CLA 2019-03-12 09:23:24 EDT
When using a static import on .*, Eclipse seems to consider private instance fields for import as well, leading to a very misleading compile error.

For example, take the code below. It gives the following compile error:
'The field A.privateInstanceField is not visible'. Also, the first quick fix is to make the field public, after which the error changes to 'privateInstanceField cannot be resolved'. I would have expected this error in the first place.


package com.mycompany;

public class A {
	private Object privateInstanceField;

	public static void staticMethod() {
	}
}


package com.mycompany;

import static com.mycompany.A.*;

public class B {
	public void method() {
		privateInstanceField.toString();
	}
}
Comment 1 Stephan Herrmann CLA 2019-03-12 09:40:23 EDT
(In reply to Emond Papegaaij from comment #0)
> For example, take the code below. It gives the following compile error:
> 'The field A.privateInstanceField is not visible'. Also, the first quick fix
> is to make the field public, after which the error changes to
> 'privateInstanceField cannot be resolved'. I would have expected this error
> in the first place.

I partly disagree: reporting "is not visible" is much more precise and helpful than the unspecific "cannot be resolved". With the latter users are left wondering, *why* can it not be resolved? A typo? A missing dependency? A missing import? ...

The open questions here are: at what priority is staticness considered during error detection? Is it only the quick fix that should be made smarter, or should the error actually be changed to s.t. like
  "Non-static field privateInstanceField cannot be accessed via a static ondemand import"
Comment 2 Emond Papegaaij CLA 2019-03-12 09:56:46 EDT
For comparison, javac simply reports:

B.java:7: error: cannot find symbol
                privateInstanceField.toString();
                ^
  symbol:   variable privateInstanceField
  location: class B
1 error

I think this is the correct error. A static import only imports visible statics. The current situation causes all kinds of non-related fields to bleed into scope. A private instance field from a class I happen to import static methods from is now in scope of my other class. It is highlighted as if it were a field and the error says it's not visible. I started searching for this field in the superclass hierarchy, but it wasn't there. In my case I didn't want to import this field at all because it's a CDI injection point and cannot be made static.

I agree that an error can have multiple possible fixes, but I would say a quickfix should not change 2 modifiers at once (private to public and add static).
Comment 3 Eclipse Genie CLA 2021-03-02 15:40:07 EST
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 4 Emond Papegaaij CLA 2021-03-03 03:18:34 EST
This bug is still present in 2020-12 (4.18.0). I still get the same, misleading compile error. I also noticed the syntax highlighting sees privateInstanceField as an actual field in the example. When you change the modifier to public (but not static), it no longer sees privateInstanceField as a field.
Comment 5 Eclipse Genie CLA 2023-05-12 07:38:32 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.