From f48f157c11ce02504cd53d349d611282f649d47c Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Fri, 15 May 2015 11:28:27 +0200 Subject: [PATCH] BUG-2976 Generate hash/equals for config generated DTOs The DTOs generated by config subsystem did not have hashCode and equals methods This caused the canReuse instance to return false even if the old module could be reused. Change-Id: I11392c2947f570a05d1a5b84fdaaee9e6b89a717 Signed-off-by: Maros Marsalek --- .../plugin/ftl/TemplateFactory.java | 37 +++++++++++++++++++ .../plugin/ModuleMXBeanEntryPluginTest.java | 6 +-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java index 00454d8acf..1eaa35f781 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java @@ -424,6 +424,43 @@ public class TemplateFactory { methods.add(setter); } + // Add hashCode + final MethodDefinition hashCode = getHash(attrs); + methods.add(hashCode); + + // Add equals + final MethodDefinition equals = getEquals(attrs); + methods.add(equals); + } + + private MethodDefinition getEquals(final Map attrs) { + final StringBuilder equalsBodyBuilder = new StringBuilder( + " if (this == o) return true;\n" + + " if (o == null || getClass() != o.getClass()) return false;\n"); + equalsBodyBuilder.append(String.format( + " final %s that = (%s) o;\n", name, name)); + for (AttributeIfc s : attrs.values()) { + equalsBodyBuilder.append(String.format( + " if(java.util.Objects.equals(%1$s, that.%1$s) == false) {\n" + + " return false;\n" + + " }\n\n", s.getLowerCaseCammelCase())); + } + equalsBodyBuilder.append( + " return true;\n"); + return new MethodDefinition("boolean", "equals", Collections.singletonList(new Field("Object", "o")), + Collections.singletonList(new Annotation("Override", Collections.emptyList())), equalsBodyBuilder.toString()); + } + + private MethodDefinition getHash(final Map attrs) { + final StringBuilder hashBodyBuilder = new StringBuilder( + " return java.util.Objects.hash("); + for (AttributeIfc s : attrs.values()) { + hashBodyBuilder.append(s.getLowerCaseCammelCase()); + hashBodyBuilder.append(", "); + } + hashBodyBuilder.replace(hashBodyBuilder.length() - 2, hashBodyBuilder.length(), ");\n"); + return new MethodDefinition("int", "hashCode", Collections.emptyList(), + Collections.singletonList(new Annotation("Override", Collections.emptyList())), hashBodyBuilder.toString()); } String getType() { diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ModuleMXBeanEntryPluginTest.java b/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ModuleMXBeanEntryPluginTest.java index d9f88643de..ee81f1c1d9 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ModuleMXBeanEntryPluginTest.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ModuleMXBeanEntryPluginTest.java @@ -129,9 +129,9 @@ public class ModuleMXBeanEntryPluginTest extends ModuleMXBeanEntryTest { is(true)); assertThat(peerTO.getFullyQualifiedName(), is(PACKAGE_NAME + ".Peer")); - assertThat(peerTO.getMethods().size(), is(5)); - Method getPort = findFirstMethodByName(peerTO.getMethods(), - "getPort"); + assertThat(peerTO.getMethods().size(), is(5 + 2/*hashCode Equals*/)); + + Method getPort = findFirstMethodByName(peerTO.getMethods(), "getPort"); assertNotNull(getPort); Method setPort = findFirstMethodByName(peerTO.getMethods(), "setPort"); -- 2.36.6