IntelliJ中最常用的字符串

时间:2019-03-25 11:18:45

标签: java string intellij-idea

有没有一种方法可以检测整个项目中是否有重复超过 x 次的硬编码字符串?

比方说,我有一个包含多个文件夹和类的项目,并且我有一个名为Constants的单独的类,我在其中存储了所有项目常量。由于项目越来越大,因此有可能检测到项目中的重复字符串会很好。当然,它也可以手动完成,但可能已经存在于IntelliJ或其他地方。

示例:

代替:

class A {
    String dog = "dog";
    String cat = "cat";
    String animals = dog + cat;
}

class B {
    String dog = "dog";
    String horse = "horse";
    String animals = dog + horse;
}

我正在努力做到:

class A {
    String cat = "cat";
    String animals = DOG + cat;
}

class B {
    String horse = "horse";
    String animals = DOG + horse;
}

class Constant {
    static final String DOG = "dog";
}

这太过简单了,但我希望这一点很清楚。

1 个答案:

答案 0 :(得分:3)

搜索部分可以通过四种方式完成。
但是,只有第二部分可以自动提取新常量。

  • 使用查找路径对话框,并使用与以下类似的配置。
    所有条目将列在预览下。

enter image description here


  • 使用提取常量功能(仅限于当前文件)。

enter image description here

将出现一个新的弹出窗口,并带有选项替换所有事件

enter image description here

使用指定的快捷方式(例如Ctrl+Alt+CEnter,将出现另一个对话框,您将可以看到事件计数。

enter image description here


  • 使用特定检查,即重复的字符串文字
    您将可以通过Analyze > Run inspection by name运行它。

enter image description here

将出现一个新对话框,使您可以自定义搜索条件

enter image description here


  • 结构搜索(docs)。
    不幸的是,我对此一无所知。