Java 如何在 IntelliJ 中禁用“访问可以是包私有的”消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41716196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable "Access can be package-private" message in IntelliJ?
提问by Thomas Zuberbühler
Developing an API, the warning message "Access can be package-package" in Java classes can be really annoying.
开发 API 时,Java 类中的警告消息“Access can be package-package”真的很烦人。
I've already spent some time going through the settings to find a way to disable this message but without any findings. Any idea how to disable the message?
我已经花了一些时间浏览设置以找到禁用此消息的方法,但没有任何发现。知道如何禁用消息吗?
采纳答案by mboss
The inspection rule is "Declaration access can be weaker" and there are two options which can be disabled "Suggest package-private visibility level..."
检查规则是“声明访问可能更弱”,有两个选项可以禁用“建议包-私有可见性级别...”
回答by JimHawkins
回答by DimaSan
Go to Settings → Editor → Inspections
then Java → Declaration redundancy → Declaration access can be weaker
rule.
Here you can either disable it at all or select the suggestion options on the right pane:
去Settings → Editor → Inspections
然后Java → Declaration redundancy → Declaration access can be weaker
统治。
在这里,您可以完全禁用它,也可以选择右侧窗格中的建议选项:
回答by seekingStillness
To disable on a case by case basis, use
要根据具体情况禁用,请使用
@SuppressWarnings("WeakerAccess")
回答by Ahmet Tavli
回答by Michael Kay
Another solution not mentioned so far: if you have a method that's declared public, and the IDE is telling you that there are no references to the method from outside the package, then perhaps you need to add a test case that calls that method from outside the package. In other words, treat the warning as signalling the absence of a test case for a public method.
到目前为止还没有提到的另一个解决方案:如果您有一个声明为 public 的方法,并且 IDE 告诉您没有从包外部引用该方法,那么您可能需要添加一个从外部调用该方法的测试用例包。换句话说,将警告视为公共方法缺少测试用例的信号。