Increase sensitivity of Unused Declarations inspection.

Answered

I'm trying to find unused classes in my project. I have a bunch of endpoints that take POJOs as arguments, and they are annotated with @PostMapping, which I have configured as an entry point. This works to make the methods not show up as unused declarations, but the classes being passed in as arguments will still show up as unused declarations because there is no usage of its constructor (because they are created by Jackson serialization).

How can I run an inspection to find classes that truly are not referenced in any way by any other class?

I don't care if I have to manually run it multiple times deleting unused classes as I go to find all the unused classes in a hierarchy.

8 comments
Comment actions Permalink

Hello,

Can you please provide a little code sample showing how are the classes (that should be marked as used) used as arguments. Thanks 

0
Comment actions Permalink
@PostMapping("/")
public ResponseEntity<CommonResponse<List<MyPojoResponse>>> myEndpoint(
@RequestBody @Valid MyPojoRequest webRequest) {
...
}

public class MyPojoRequest {
@ApiModelProperty
MyPojoFilter filter;
}

With no entry points registered, everything is marked as unused.
With @PostMapping registered as Entry Point, MyPojoRequest and MyPojoFilter are listed as unused
With @PostMapping, @RequestBody, and @ApiModelProperty registered as Entry Point, MyPojoFilter is listed as unused.

0
Comment actions Permalink

Hello,

Please clarify, so what you do expect is to mark MyPojoFilter class as used in case the field of this type is annotated by annotation included in the entry point list, right? 

0
Comment actions Permalink

Yes, I would expect MyPojoResponse, MyPojoRequest and MyPofoFilter would all be marked as used due to the entry point annotations.

 

But I would also expect that MyPojoFilter would be marked as used even without @ApiModelPropery marked as EntryPoint, since it is used by MyPojoRequest, which should be marked as used by @RequestBody being an entry point. (I should probably clarify that MyPojoRequest has constructors generated by lombok)

0
Comment actions Permalink

Hello Lars,

In case the field of particular type is annotated by the annotation added in the entry point list it doesn't follow that the class of this type starts being reported as unused. Also in case the class is annotated with an entry point annotation it doesn't make all members of the class as used. 

Sorry but the behaviour that you describe will contradict the way the entry points supported in general.

0
Comment actions Permalink

No you have it backwards.

The class is currently reported as unused, and it continues being reported as unused even if I add the annotation as an entry point.

I want it to be reported as used.

 

So "the field of particular type is annotated by the annotation added in the entry point list it DOES follow that the class of this type starts being reported as USED."

@EntryPoint

class MyClass {

  @EntryPoint

  MyField field;

}

So why is the MyField class marked as unused?

 

0
Comment actions Permalink

But I don't even really want to be messing around with entry points. I just want to know all classes that have absolutely 0 references from any other class in any way. So I can delete the class and not have to edit any other files. This seems possible, since such classes have their name grayed out when you open it, but I don't see a way to get a list of all such classes.

Its really annoying to search for unused classes and have to run "find usages" on each one, only to find it is actually still referenced by 16 other classes. I don't want classes that IntelliJ has used advanced logic to determine might be unused, I want to see classes that 100% definitely are unused.

0
Comment actions Permalink

Lars,

Please follow the discussion in the created issue at YouTrack:

https://youtrack.jetbrains.com/issue/IDEA-250439

Thank you 

0

Please sign in to leave a comment.