如何教 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:55:51  来源:igfitidea点击:

How to teach eclipse to generate compact equals() and hashCode() from the jdk 7 Objects class?

javaeclipseequalshashcode

提问by Aufwind

Some days ago we switched to Java 7 within my Company - finally! Jay \o/ So I found out about the Objectsclass 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.

也是如此equals()这是我从这篇文章中得到的。

Any ideas how to realize this best?

任何想法如何最好地实现这一点?

采纳答案by Pyves

hashCodeand equalsgeneration using the Java 7 Objectsclass 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...工具中新选项的概述:

New option in generation tool

生成工具中的新选项

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.deepHashCodeand Arrays.deepEqualsmethods in a number of cases where it would previously incorrectly prefer the standard Arrays.hashCodeand Arrays.equalsalternatives.

除了这个新功能之外,现在处理数组的方式更加巧妙。一代将在许多情况下使用Arrays.deepHashCodeArrays.deepEquals方法,在这些情况下,它以前会错误地选择标准Arrays.hashCodeArrays.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 -> 不同的选项

The link - https://marketplace.eclipse.org/content/jenerate

链接 - https://marketplace.eclipse.org/content/jenerate