Java 如何删除 Checkstyle 信息(导入 org.apache.log4j.Logger 的顺序错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21477348/
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-13 08:53:41  来源:igfitidea点击:

How to remove Checkstyle info (wrong order for import org.apache.log4j.Logger)

javaeclipsecheckstyle

提问by Saikat

I can see a Checkstyle info which says - Wrong order for import, org.apache.log4j.Logger. I could not get much info on why I am getting this. Any help would be appreciated. Below is the code snippet:

我可以看到一个 Checkstyle 信息,上面写着 - Wrong order for import, org.apache.log4j.Logger。我无法获得太多关于为什么我得到这个的信息。任何帮助,将不胜感激。下面是代码片段:

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.log4j.Logger;

import com.company.department.team.test.Configuration;

采纳答案by Zoltán

ctrl+shift+o(organize imports) will make Eclipse order your imports correctly.

ctrl+shift+o(组织导入)将使 Eclipse 正确排序您的导入。

There is a convention according to which imports should be ordered, and checkstyle is telling you that you have not listed your imports in that order.

有一个约定,应根据何种顺序对导入进行排序,而 checkstyle 会告诉您您尚未按该顺序列出您的导入。

You can read more about it in the ImportOrdersection of the documentation:

您可以ImportOrder在文档部分阅读更多相关信息:

Checks the ordering/grouping of imports. Features are:

  • groups imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)
  • adds a separation between groups : ensures that a blank line sit between each group
  • sorts imports inside each group: ensures that imports within each group are in lexicographic order
  • sorts according to case: ensures that the comparison between imports is case sensitive
  • groups static imports: ensures the relative order between regular imports and static imports (see import orders)

检查导入的排序/分组。特点是:

  • 组导入:确保导入组按特定顺序出现(例如,java. 首先出现,javax. 第二次出现,然后是其他所有内容)
  • 在组之间添加分隔:确保每个组之间有一个空行
  • 对每个组内的导入进行排序:确保每个组内的导入按字典顺序排列
  • 根据大小写排序:确保导入之间的比较区分大小写
  • 组静态导入:确保常规导入和静态导入之间的相对顺序(请参阅导入顺序)

回答by greg-449

Look at Preferences > Java > Code Style > Organize Importsto configure the sort order and grouping that the Source > Organize Importscommand uses (Ctrl+Shift+O, on OS X Cmd+Shift+O.

查看Preferences > Java > Code Style > Organize Imports配置Source > Organize Imports命令使用的排序顺序和分组(Ctrl+ Shift+ O,在 OS X Cmd+ Shift+ 上O

回答by rveach

You can also modify your check file to go by what eclipse does by default. You need to change the module "CustomImportOrder" and change "customImportOrderRules".

您还可以修改您的检查文件,以按照 eclipse 的默认操作进行。您需要更改模块“CustomImportOrder”并更改“customImportOrderRules”。

See http://checkstyle.sourceforge.net/config_imports.html#CustomImportOrderon how to customize it more.

请参阅http://checkstyle.sourceforge.net/config_imports.html#CustomImportOrder了解如何对其进行更多自定义。

This is what I am currently using:

这是我目前正在使用的:

<module name="CustomImportOrder">
    <property name="specialImportsRegExp" value="gov." />
    <property name="sortImportsInGroupAlphabetically" value="true" />
    <property name="customImportOrderRules"
        value="STATIC###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE" />
</module>

回答by iammrmehul

For me what fixed this issue was arranging the imports in alphabetical order. For eg., import com.company.department.team.test.Configuration;should be at the top.

对我来说,解决这个问题的是按字母顺序排列导入。例如,import com.company.department.team.test.Configuration;应该在顶部。