javaintellij-ideaintellij-inspections

Is it possible to disable "Optional used as field or parameter type" inspection in IntelliJ for private methods?


I don't want to disable the warning altogether and I already know why you supposedly shouldn't use Optional as a parameter. However, I don't think it's relevant to my case.

For illustrative purposes, I have code that looks like this:

//IntelliJ reports: 'Optional<ForeignDataTransferObject>' used as type for parameter 'data'
private LocalSystemPerson extractPerson(Optional<ForeignDataTransferObject> data) {
    return data
            .map(data -> data.getPeopleListWrapper())
            .map(peopleListWrapper -> peopleListWrapper.getList())
            .map(listOfForeignPeople -> listOfForeignPeople.stream())
            .orElseGet(Stream::empty)
            .findFirst()
            .map(foreignPerson -> convertToLocalSystemPerson(foreignPerson))
            .orElse(someFallbackValue);
}

It's getting the list of people then the the first item then converting it. Since I'm calling an external API and getting a data transfer object with a bunch of fields any of which can be null, any collections are wrapped in an extra class, and even then getting an empty list is a valid case. So, I end up with a verbose code like that to convert a single value. I squirrelled it into a separate helper method to avoid having all that verbosity in one place.

However IntelliJ is complaining that I'm passing an Optional parameter. That is something I'd normally agree with but I don't see it as a problem if it's a private method that I am using to save another method from getting too long - I might need to grab more than one value off the data transfer object.

All in all, it seems like an unreasonable limitation to always treat that as a problem even if it's not going to be transferring data across systems or across layers, etc.

So, can I disable that generally from the settings for just private methods but nothing else?


Solution

  • For the current moment there is no option to disable the inspection for private methods. Please follow/comment the issue created for your feature request at YouTrack: https://youtrack.jetbrains.com/issue/IDEA-207468 Thank you