IntelliJ抑制API方法的未使用警告 [英] IntelliJ suppress unused warning for API methods

查看:467
本文介绍了IntelliJ抑制API方法的未使用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从Eclipse切换到IntelliJ IDEA,我非常喜欢检查器,发现它们用警告标记潜在的错误,对我来说真的很有用.我遇到了他们无法解决的问题:

I recently switched to IntelliJ IDEA from Eclipse and I really like the inspectors and find them marking potential errors with warnings for me really useful. I ran into a problem with them that I am unable to solve:

我有一些Java项目在其他项目中用作API,因此它包含未使用的方法,这些方法被标记为: 未使用的警告

I have some Java projects that are used as APIs in other project, therefore it contains unused methods, which are marked as such: Unused warning

对于API方法,我该如何取消呢? @SuppressWarnings("unused")是否有替代方法,因为它还会抑制方法内部有关未使用警告的警告,并且无法使读者清楚地知道此方法是为API使用而设计的,而不是仅在当前项目中不使用?/p>

How can i suppress this for the API methods? Is there an alternative to @SuppressWarnings("unused"), since this also suppresses warnings about unused warnings inside the method and doesn't make it clear to the reader that this method is designed for API use instead of just not being used in the current project?

推荐答案

@Sebastian建议让您的类实现接口,这可能是从好的设计角度解决此问题的最佳方法.但是,当这不切实际时,还有另一种选择...

@Sebastian's suggestion to have your class implement an interface is probably the best way to solve this issue from a good design standpoint. But when that is impractical, there is an alternative...

通过未使用的声明"检查,您可以配置要忽略的入口点"(即公共外部API).作为其一部分,您可以配置注释".带有已配置注释的任何注释都将被视为入口点.

The "Unused declaration" inspection allows you to configure "entry points" (i.e. a public external API) to ignore. As part of that you can configure "annotations". Anything annotated with the configured annotation is seen as an entry point.

只需将检查配置为使用注释即可,您可以使用注释从库中注释一个公共API方法,例如

Just configure the inspection to use an annotation that you annotate your public API methods with, either one from a library -- such as @API Guardian (used by some open source projects such as JUnit 5) -- or one you create. Using a library will of course make things easier and consistent across projects. In the below example I used a @PublicApi annotation I created. Notice the method is not highlighted as unused. But the foo String still is highlighted as unused, which is what you want:

作为打开设置"对话框的替代方法,并限制对编程的影响),然后选择禁止用' {annotation} ':

As an alternative to opening the Settings dialog, and to limit the impact on your programming ) and select "Suppress for methods annotated by '{annotation}':

这篇关于IntelliJ抑制API方法的未使用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆