Bug 543095 - Differentiate between static and non-static fields in Errors/Warnings for name shadowing
Summary: Differentiate between static and non-static fields in Errors/Warnings for nam...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.10   Edit
Hardware: PC Windows 10
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-31 00:06 EST by Matthias Perktold CLA
Modified: 2019-01-07 10:14 EST (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 Matthias Perktold CLA 2018-12-31 00:06:45 EST
In our project, we would like to avoid non-static fields that hide other non-static fields. Therefore, we tried activating a warning for "Field declaration hides another field or variable".

But then Eclipse checks for both, static and non-static fields. This gives us a lot of warnings for static fields that we would like to ignore. We find that "hiding" static fields is not a problem, because one can always access the "hidden" field by using the class as a qualifier.

To better illustrate the issue, let me give you a concrete example. Our entity classes typically have a static PLURAL_NAME constant which holds a translation key for the plural name of the entity. This constant can be redeclared in extending classes that need a different translation without losing the one of the base class. Here is an example with a class Employee that extends another class Person, where both have their own PLURAL_NAME constants:

  public class Person {
    public static final String PLURAL_NAME = "People";
    // ...
  }

  public class Employee extends Person {
    public static final String PLURAL_NAME = "Employees";
    // ...
  }

  // Usage
  System.out.println(translate(Person.PLURAL_NAME, "de"));
  System.out.println(translate(Employee.PLURAL_NAME, "de"));

These constants can be accessed as Person.PLURAL_NAME or Employee.PLURAL_NAME. They are typically accessed outside the class, so they need a class qualifier anyway. But to avoid a warning, one would need to use different identifiers for both constants, e.g. Person.PERSON_PLURAL_NAME and Employee.EMPLOYEE_PLURAL_NAME. So the above example becomes:

  public class Person {
    public static final String PERSON_PLURAL_NAME = "People";
    // ...
  }

  public class Employee extends Person {
    public static final String EMPLOYEE_PLURAL_NAME = "Employees";
    // ...
  }

  // Usage
  System.out.println(translate(Person.PERSON_PLURAL_NAME, "de"));
  System.out.println(translate(Employee.EMPLOYEE_PLURAL_NAME, "de"));

This quickly becomes verbose and unreadable. In fact, there was nothing wrong with the original approach of using the same identifier. Thus, we must deactivate the warning, which means that we also lose its support for finding hidden non-static fields.

Therefore, I suggest adding an option that allows to differentiate between static and non-static fields for this setting. It could be a flag, like "Include static fields", or it could be a separate setting.