IntelliJ IDEA highlights some unused methods, but not all

In IntelliJ IDEA 2017.2.6 I have the following Java class.

public class EinzUnregisterResponseMessageBody extends EinzMessageBody { private final String username, reason; public EinzUnregisterResponseMessageBody(String username, String reason){ this.username = username; this.reason = reason; } /** * @return the body as JSONobject, ready to be included as "body":{this returned Object} in a message */ @Override public JSONObject toJSON() throws JSONException { return new JSONObject("{\"username\":\""+this.username+"\"," + "\"reason\":\""+this.reason+"\"}"); } public String getReason() { return reason; } public String getUsername() { return username; } } 

IntelliJ typically displays unused properties in light gray. In this class, it only stands out getReason() . It makes sense that it does not sed constructor or fields. The toJSON() method is defined in the abstract superclass EinzMessageBody and therefore must exist, therefore it is correct that this was also not EinzMessageBody out. getReason() never used and thus inactive. But why is getUsername() not greyed out? When I right-click it and select "Search", IntelliJ reports that no usages were found.

I know this question , but run Analyze> Inspect Code and Analyze> Run Inspection By Name> Unused properties do not seem to affect this. Does not restart the IDE.

Invalid caches and reboots first reset the display so that nothing is crossed out, but as soon as it finishes indexing, the backlight will be the same again.

Here, as it is displayed in IntelliJ How it looks to me

The superclass EinzMessageBody specifies only toJSON() and neither getReason() nor getUsername() .

What is the difference between getReason() and getUsername() ?

Regarding the comment by @HonzaZidek:

  • Removing extends EinzMessageBody and extends EinzMessageBody annotations @Override n't change anything.

  • Create a completely empty project and insert only this class, and EinzMessageBody makes everything gray except for the toJSON() method. This is as expected, because now I am not creating an instance of the class, so the constructor is grayed out and both getReason() and getUsername() behave the same (both are grayed out).

    • This still behaves as expected when I move classes to different subpackages, for example, they are in my actual project setup.

    • This still behaves as expected when I instantiate the class: the constructor is no longer gray, getReason() and getBody() are still gray.

I just put on my laptop (unlike a desktop PC), and the same behavior happens there.

I also looked for any use of getUsername() in other subclasses of EinzMessageBody and found it. Removing this parameter did not change the value of getUsername() in EinzUnregisterResponseMessageBody to be gray.

This answer suggests running Analyze> Run Inspection By Name> Unused Declaration, because IntelliJ cannot validate commonly used names for performance reasons. This displays both getReason() and getUsername() as unused, but the display style remains the same.

+5
source share
1 answer

I am afraid that this is really, as you suspect, an optimization built into IntelliJ Idea. You can report this as an error or feature request in JetBrains Issue Tracker - at least it would be nice to have an option to turn off optimization.

Following the existing answers from fooobar.com/questions/1229162 / ... and fooobar.com/questions/1273512 / ... , I add a link to the IntelliJ Idea Description in the Settings | Editor | Inspections | Java | Declaration Redundancy | Unused declaration :

Some unused items may not be displayed during selection in the editor. Due to performance reasons, the non-personal member is only checked when its name is rarely found in the project.

enter image description here

+1
source

Source: https://habr.com/ru/post/1273510/


All Articles