简体   繁体   English

是否可以在 IntelliJ 中为私有方法禁用“可选用作字段或参数类型”检查?

[英]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.这是我通常同意的事情,但如果它是一种私有方法,我不认为这是一个问题,我正在使用它来保存另一种方法免于过长 - 我可能需要从数据传输中获取多个值目的。

  • 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.我还是要避免创建接受公共方法Optional或持有场Optional ,但我想禁用私有方法的检验。
  • I'd prefer to not have to annotate each method with @SuppressWarnings("OptionalUsedAsFieldOrParameterType") as it quickly gets annoying.我不想@SuppressWarnings("OptionalUsedAsFieldOrParameterType")注释每个方法,因为它很快就会变得烦人。 I can have multiple of these methods in various classes that accept some foreign DTO.我可以在接受一些外国 DTO 的各种类中拥有多个这些方法。
  • Similarly, I also don't want to pass in a non- 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.

相关问题 IntelliJ-Idea 禁用检查:参数的实际值始终为 - IntelliJ-Idea disable inspection: Actual value of parameter is always 禁用IntelliJ IDEA中的“恒定条件和例外”检查字段 - Disable “Constant conditions & exceptions” inspection for field in IntelliJ IDEA 如何在 IntelliJ IDEA 中禁用代码检查 - How to disable code inspection in IntelliJ IDEA Intellij,仅对文件禁用检查/建议 - Intellij, disable inspection/suggestion only for file 未从 Intellij 的设置/检查/Java 列表中找到“从未使用字段 x”,如何获得突出显示而不是鼠标悬停警告? - "Field x is never used" not found from Intellij's Setting/Inspection/Java list, how to get an highlight instead of an onmouseover warning? 方法中的java可选参数 - java optional parameter in methods 是否可以使用 Mockito(不是 PowerMockito?)测试从公共 static 方法使用的 static 私有方法? - Is it possible to test static private methods used from public static methods with Mockito (not PowerMockito!)? Eclipse为什么用“参数隐藏字段”标记私有方法,而不用“公共方法”标记? - Why does Eclipse flag private methods with 'parameter is hiding a field', but not public methods? 键入层次结构+可选字段 - Type hierarchy + optional field 在Eclipse中,是否可以在项目中找到采用某种参数类型的所有方法? - In Eclipse, is it possible to find all methods in project that take a certain parameter type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM