我最近从 Eclipse 切换到 IntelliJ IDEA,我真的很喜欢检查器,发现它们用警告来标记潜在的错误,对我来说非常有用。我遇到了一个我无法解决的问题:
我有一些Java项目在其他项目中用作API,因此它包含未使用的方法,这些方法被标记为这样:未使用警告 https://i.stack.imgur.com/v9xkI.png
我怎样才能抑制 API 方法的这种情况?有没有替代方案@SuppressWarnings("unused")
,因为这也会抑制有关方法内未使用的警告的警告,并且不会让读者清楚该方法是为 API 使用而设计的,而不仅仅是在当前项目中未使用?
@Sebastian 建议让你的类实现一个接口,这可能是从良好设计的角度解决此问题的最佳方法。但当这不切实际时,还有另一种选择......
“未使用的声明”检查允许您配置要忽略的“入口点”(即公共外部 API)。作为其中的一部分,您可以配置“注释”。使用配置的注释进行注释的任何内容都被视为入口点。
只需将检查配置为使用您用来注释公共 API 方法的注释,无论是来自库的注释 - 例如@API守护者 https://github.com/apiguardian-team/apiguardian(由一些开源项目使用,例如 JUnit 5)——或者您创建的项目。使用库当然会让项目之间的事情变得更容易和一致。在下面的例子中我使用了@PublicApi
我创建的注释。注意该方法是not突出显示为未使用。但是foo
字符串仍然突出显示为未使用,这正是您想要的:
As an alternative to opening the Settings dialog, and to limit the impact on your programming flow https://en.wikipedia.org/wiki/Flow_(psychology), you can also use a Quick Fix Intention to add the desired annotation to the "Unused Declaration" settings. After annotating with the desired annotation, place your cursor on the highlighted unused method (or class) name, and invoke the "intention actions and quick-fixes" popup via Alt+Enter or ⌘↩ (or by clicking on the light bulb icon ) and select "Suppress for methods annotated by '{annotation}':
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)