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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:35:56  来源:igfitidea点击:

How to disable "Access can be package-private" message in IntelliJ?

javaintellij-idea

提问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

Move the cursor to public, press Alt+ Enterand choose one of the options:
enter image description here

将光标移至public,按Alt+Enter并选择以下选项之一:
在此处输入图片说明

回答by DimaSan

Go to Settings → Editor → Inspectionsthen Java → Declaration redundancy → Declaration access can be weakerrule.
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统治。
在这里,您可以完全禁用它,也可以选择右侧窗格中的建议选项:

enter image description here

在此处输入图片说明

回答by seekingStillness

To disable on a case by case basis, use

要根据具体情况禁用,请使用

@SuppressWarnings("WeakerAccess")

回答by Ahmet Tavli

Well, one possible reason could be a redundant class-type.

好吧,一个可能的原因可能是冗余的类类型。

For instance:

例如:

enter image description here

在此处输入图片说明

Here, in the above image, "public" keyword is not necessary.

在这里,在上图中,“public”关键字不是必需的。

Removing the "public" keyword:

删除“public”关键字:

enter image description here

在此处输入图片说明

Solves the issue in this simple program.

在这个简单的程序中解决了这个问题。

回答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 告诉您没有从包外部引用该方法,那么您可能需要添加一个从外部调用该方法的测试用例包。换句话说,将警告视为公共方法缺少测试用例的信号。