[英]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 .我不想完全禁用警告,我已经知道为什么你不应该使用
Optional
作为参数。 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.
由于我正在调用外部 API 并获取带有一堆字段的数据传输对象,其中任何一个都可以为空,因此任何集合都包含在一个额外的类中,即使如此,获取空列表也是一个有效的情况。 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.但是 IntelliJ 抱怨我正在传递一个
Optional
参数。 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.这是我通常同意的事情,但如果它是一种私有方法,我不认为这是一个问题,我正在使用它来保存另一种方法免于过长 - 我可能需要从数据传输中获取多个值目的。
Optional
or fields that hold Optional
but I want to disable the inspection for private methods.Optional
或持有场Optional
,但我想禁用私有方法的检验。@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
as it quickly gets annoying.@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
注释每个方法,因为它很快就会变得烦人。 I can have multiple of these methods in various classes that accept some foreign DTO.Optional
parameter only to wrap it inside the method.Optional
参数只是为了将它包装在方法中。 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.Optional
已经,所以我必须解开它然后用类似myMethod(data.orElse(null))
东西把它包装回去,然后在方法中做Optiona.ofNullable(parameter)
。 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
请在 YouTrack 上关注/评论为您的功能请求创建的问题: https ://youtrack.jetbrains.com/issue/IDEA-207468 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.