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.
- I still want to avoid creating public methods that accept
Optional
or fields that holdOptional
but I want to disable the inspection for private methods. - I'd prefer to not have to annotate each method with
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
as it quickly gets annoying. I can have multiple of these methods in various classes that accept some foreign DTO. - Similarly, I also don't want to pass in a non-
Optional
parameter only to wrap it inside the method. Especially since In some cases I only have anOptional
already, so I'd have to unwrap it then wrap it back with something likemyMethod(data.orElse(null))
and then doOptiona.ofNullable(parameter)
in the method.
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?
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