如何教 Eclipse 从 jdk 7 Objects 类生成紧凑的 equals() 和 hashCode()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17782032/
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
How to teach eclipse to generate compact equals() and hashCode() from the jdk 7 Objects class?
提问by Aufwind
Some days ago we switched to Java 7 within my Company - finally! Jay \o/ So I found out about the Objects
class and was astonished how short the methods hashCode()
and equals()
were realized, reducing a lot of boylerplate code compared to the ones generated by eclipse per default (ALT+SHIFT+S --> H).
几天前,我们在公司内部切换到 Java 7 - 终于!Jay \o/ 所以我发现了这个Objects
类,并惊讶于这些方法hashCode()
和equals()
实现的方法有多短,与默认情况下由 eclipse 生成的代码(ALT + SHIFT + S --> H)相比,减少了大量的黑板代码。
I was wondering if I could change the default behaviour of the eclipse generated hashCode()
and equals()
?
我在想,如果我可以改变所产生的日食的默认行为hashCode()
和equals()
?
I'd love to see this:
我很想看到这个:
@Override
public int hashCode()
{
return Objects.hash(one, two, three, four/*, ...*/);
}
instead of this:
而不是这个:
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((one == null) ? 0 : one.hashCode());
result = prime * result + ((two == null) ? 0 : two.hashCode());
result = prime * result + ((three == null) ? 0 : three.hashCode());
result = prime * result + ((four== null) ? 0 : four.hashCode());
// ...
return result;
}
The same goes for equals()
. This is the articleI got this from.
Any ideas how to realize this best?
任何想法如何最好地实现这一点?
采纳答案by Pyves
hashCode
and equals
generation using the Java 7 Objects
class has now been implemented in Eclipse. I was working on the feature request 424214back in August 2018 and my contributions were merged in the JDT UI codebase shortly afterwards (see commit f543cd6).
hashCode
并且现在已经在 Eclipse 中实现了equals
使用 Java 7Objects
类的生成。早在 2018 年 8 月,我就在处理功能请求424214,不久之后我的贡献就合并到了 JDT UI 代码库中(请参阅提交f543cd6)。
Here's an overview of the new option in the Source > Generate hashCode() and equals...tool:
以下是Source > Generate hashCode() and equals...工具中新选项的概述:
This has been officially released in Eclipse 4.9 in September 2018. Simply download the latest version of Eclipse (downloads can be found here), or install the latest available software with the following update site: http://download.eclipse.org/releases/latest
这已于 2018 年 9 月在 Eclipse 4.9 中正式发布。只需下载最新版本的 Eclipse(可在此处找到下载),或使用以下更新站点安装最新的可用软件:http: //download.eclipse.org/releases /最新的
In addition to this new feature, arrays are now handled more cleverly. The generation will use the Arrays.deepHashCode
and Arrays.deepEquals
methods in a number of cases where it would previously incorrectly prefer the standard Arrays.hashCode
and Arrays.equals
alternatives.
除了这个新功能之外,现在处理数组的方式更加巧妙。一代将在许多情况下使用Arrays.deepHashCode
和Arrays.deepEquals
方法,在这些情况下,它以前会错误地选择标准Arrays.hashCode
和Arrays.equals
替代方案。
回答by reto
In the Eclipse preferences go to Java > Editor > Templates.
在 Eclipse 首选项中,转到 Java > 编辑器 > 模板。
In there you can create a new template. The pattern could look like:
在那里您可以创建一个新模板。该模式可能如下所示:
@Override
public int hashCode()
{
return Objects.hash(one, two, three, four/*, ...*/);
}
I'm not sure if there's a variable that will properly enumerate your fields however.
我不确定是否有一个变量可以正确枚举您的字段。
You might want to look at some further explanations on these templates
您可能想查看有关这些模板的进一步说明
回答by Nirav Pradhan
There is a new plugin available which can generate toString(), hashCode(), equals() methods using java 7 features, apache common lang library, guava library. It has good customizable features. Please find the link below to install plugin. After installation, just right click -> Jenerate -> different options
有一个可用的新插件可以使用 java 7 特性、apache 通用语言库、番石榴库生成 toString()、hashCode()、equals() 方法。它具有良好的可定制功能。请找到下面的链接来安装插件。安装后,只需右键-> Jenerate -> 不同的选项