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

VLAZ :

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 hold Optional 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 an Optional already, so I'd have to unwrap it then wrap it back with something like myMethod(data.orElse(null)) and then do Optiona.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?

Olga Klisho :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=106138&siteId=1