有没有一种方法可以强制使用制表符代替Java中的空格?

| CheckStyle提供了检查空间是否一致使用的功能,但遗憾的是缺少相反的想法:强制源代码使用制表符。有什么方法可以添加此功能?它不必是CheckStyle,也可以使用其他工具。 与这个问题相同,但适用于Java。 编辑 我不需要代码美化器,因为代码库的正常状态将是所有选项卡。我只需要一个可以报告替代缩进的工具。这样,我可以设置一个新的连续构建配置,当引入空格时该配置将失败。     
已邀请:
尽管Checkstyle对此没有内置检查,但是您可以使用“ 0”检查强制仅制表符的缩进。请注意,这仅检查哪个字符用于缩进,而不检查正确的缩进级别。 从Hibernate OGM来源无耻地被盗:
<module name=\"RegexpSinglelineJava\">
    <property name=\"format\" value=\"^\\t* +\\t*\\S\"/>
    <property name=\"message\" value=\"Line has leading space characters; indentation should be performed with tabs only.\"/>
    <property name=\"ignoreComments\" value=\"true\"/>
</module>
    
首选使用空格而不是制表符缩进,因为它可在所有编辑器/查看器中提供一致的布局。 但是,如果您仍然想要它,则始终可以对checkstyle或自定义maven插件/ ant任务进行自定义检查。 逻辑也不应该很难实现-所有您需要检查任何行上的前导空格是否大于制表符长度。 编辑:包括一个蚂蚁的例子。 自您发布以来已经有两个星期了,您仍然不开心,我还有一些空闲时间:) 因此,我为您准备了一个小蚂蚁自定义任务解决方案。 蚂蚁任务
public class SpaceDetectorTask extends Task {
    public static final String REGEX = \"^[ ]+\";
    public static final Pattern p = Pattern.compile(REGEX);

    private FileSet fileSet;
    private boolean failOnDetection;

    // Usual getters/setters

    public void addFileSet(FileSet fileSet) {
        this.fileSet = fileSet;
    }

    public void execute() {
        DirectoryScanner ds = fileSet.getDirectoryScanner();
        String[] files = ds.getIncludedFiles();
        for (int x = 0; x <= files.length -1; x++) {
            process(ds.getBasedir(), files[x]);
        }
    }

    public void process(File dir, String file) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(dir, file)));
            String line;
            int linecount = 0;
            System.out.println(\"File: \" + file);
            boolean ignore = false;
            while((line = reader.readLine()) != null) {
                linecount++;

                // exclude comment blocks
                if (line.contains(\"/**\") || line.contains(\"*/\")) {
                    ignore = !ignore;
                    continue;
                }

                if (!ignore) {
                    if (p.matcher(line).find()) {
                        int spcCount = line.length() - (line.replaceAll(REGEX, \"\")).length();
                        if (spcCount >= 4) { // break whenever 4 leading spaces are detected. Configure as you need.
                            String msg = \"File: \"+ file + \" is using spaces as indentation.\";
                            if (failOnDetection) {
                                throw new BuildException(msg);
                            } else {
                                getProject().log(msg);
                            }
                        }
                    }
                    reader.close();
                }
            }
        } catch (IOException e) {
            if (failOnDetection) {
                throw new BuildException(e);
            } else {
                getProject().log(\"File: \" + file + \"\\n\" + e.getMessage());
            }
        }
    }
在ant build.xml中 首先编译任务 声明它
<taskdef name=\"detect-spaces\"
         classname=\"com.blah.blah.build.tools.SpaceDetectorTask\">
        <classpath>
            <pathelement path=\"${dir.classes}\"/>
            <fileset dir=\"C:/apache-ant-1.7.1/lib\">
                <include name=\"**/*.jar\"/>
            </fileset>
        </classpath>
</taskdef>
用它
<target name=\"rules.spaces\">
    <detect-spaces
        failOnDetection=\"true\">
        <fileset dir=\"${dir.src.java}\">
            <include name=\"**/*.java\"/>
        </fileset>
    </detect-spaces>
</target>
编写maven / checkstyle插件也不是一件容易的事。     
这种方法比其他建议的方法简单得多。 在“其他”组下创建一个Regexp测试,并设置 格式:
^[ \\t]*
消息:缩进必须是制表符 非法模式:(已选中)     
使用以下命令搜索前导空格:
grep -e \"^\\t* \" `find . -name *.java`
然后翻转退出状态。     
Jalopy可能是您想要的。它已经有一段时间没有更新了,但仍然可以正常工作。 Triemax也提供商业版本。     
我们的Java Formatter将直接执行此操作。 JavaFormatter prettyprints源文本(对于prettyprint:-的一些定义)和缩进块在逻辑上每个嵌套块固定数量的空格(例如,indent = 3 [作为默认值])。 可以强制Java格式化程序在空白处的左边距使用TAB字符,并且它将使用TAB跳到与用户指定的制表位列号相对应的下一列。如果您将制表位定义为与缩进距离相同,则只需格式化代码,每个缩进将获得一个制表符。如果您定义了indent = 8,则制表符停靠每8列(1,9,17,...),您将倾向于获得可与许多编辑器一起使用的制表符,因为制表符的默认解释是“每8列”。     
在IntelliJ中,我让它在签入时执行代码格式化为标准格式。这样,您可以在输入时使用空格/制表符,但是代码会定期更改为标准格式,并且在版本控制中始终正确(必须共享) 如果没有IntelliJ,则必须执行类似的操作,例如预检脚本。 在80年代,使用制表符代替空格是个好主意,但您真的认为在10年代它很重要吗?这肯定与磁盘空间无关!     

要回复问题请先登录注册