Java 是否可以更改 IntelliJ 的 equals() 和 hashCode() 代码生成模板

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

Is it possible to change IntelliJ's code generation template for equals() and hashCode()

javaintellij-ideacode-generation

提问by Mike Rylander

Is it possible to change the code generation template for equals()and hashCode()?

是否有可能改变代码生成模板equals()hashCode()

I would like the generated code to use the Java 7 Objectsclass for theses methods.

我希望生成的代码Objects对这些方法使用 Java 7类。

采纳答案by datentyp

As of release 14.1of the Ultimate Edition it is possible to customize the Code generation template of equals()/hashCode() without the use of any third party plugin.

从Ultimate Edition 14.1版开始,可以在不使用任何第三方插件的情况下自定义 equals()/hashCode() 的代码生成模板。

Press "Alt + Insert" (Generate...), choose "equals() and hashCode()" and you will be able to select one of the predefined templates or a customized template of your own.

按“Alt + Insert”(生成...),选择“equals() 和 hashCode()”,您将能够选择预定义模板之一或您自己的自定义模板。

"Generate equals() and hashCode()" dialog

“生成 equals() 和 hashCode()”对话框

Edit "equals() and hashCode()" Velocity templates

编辑“equals() 和 hashCode()”速度模板

回答by Mike Rylander

Update: As of version 14.1 this is supported.

更新:从 14.1 版开始,支持此功能。

Apparently this functionality does not exist.

显然这个功能不存在。

There is a request for it though, see: http://youtrack.jetbrains.com/issue/IDEA-56007

但是有一个请求,请参阅:http: //youtrack.jetbrains.com/issue/IDEA-56007

回答by klor

UPDATE: have a look at the answer from @datentyp. Leaving mine for those stuck on the old versions

更新:看看@datentyp 的回答。为那些卡在旧版本上的人留下我的

There are plugins to allow this. Please have a look at this ones:

有插件允许这样做。请看看这些:

http://plugins.jetbrains.com/plugin/6875?pr=idea

http://plugins.jetbrains.com/plugin/7244?pr=idea

http://plugins.jetbrains.com/plugin/6875?pr=idea

http://plugins.jetbrains.com/plugin/7244?pr=idea

They are opensource so you can amend them if you need to.

它们是开源的,因此您可以根据需要对其进行修改。

回答by bigboban

Yes it ispossible, but with some small hack. Locate your idea.jar file in app folder. Look inside and find files like:

是的,这是可能的,但需要一些小技巧。在 app 文件夹中找到您的 idea.jar 文件。查看内部并找到如下文件:

  • apacheEqualsBuilder3.vm
  • apacheHashCodeBuilder3.vm
  • apacheEqualsBuilder3.vm
  • apacheHashCodeBuilder3.vm

and other .vm files..

和其他 .vm 文件..

These template files reuse some macros from equalsHelper.vm.

这些模板文件重用了 equalsHelper.vm 中的一些宏。

Change these files inside JAR carefully as you like restart app and check result in Idea under

当你喜欢重启应用程序并检查 Idea 下的结果时,仔细更改 JAR 中的这些文件

generate->equals() and hashCode()->Template (... three dots on the right)->read only preview on the right side.

generate->equals() 和 hashCode()->Template(...右侧三个点)->右侧的只读预览。

I think installing update of Idea may overwrite this JAR with your custom template changes.

我认为安装 Idea 的更新可能会用您的自定义模板更改覆盖此 JAR。

It works! Checked in Idea 15. Enjoy. :-)

有用!签入想法 15。享受。:-)