@Test public void testProcessEqualsAndHashCodeNoFields() { MethodSpec.Builder equalsBuilder = MethodSpec.methodBuilder("equals") .addModifiers(Modifier.PUBLIC) .returns(boolean.class) .addAnnotation(Override.class) .addParameter(Object.class, "o"); MethodSpec.Builder hashCodeBuilder = MethodSpec.methodBuilder("hashCode") .addModifiers(Modifier.PUBLIC) .returns(int.class) .addAnnotation(Override.class); MetaInfo metaInfo = new MetaInfo("Test", "com.flipkart.lyrics"); CommonsLang3Style style = new CommonsLang3Style(); style.processEqualsAndHashCode(equalsBuilder, hashCodeBuilder, new ArrayList<>(), metaInfo, true); }
@Override public void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality) { String className = metaInfo.getClassName(); equalsBuilder.addStatement("if (this == o) return true"); equalsBuilder.addStatement("if (!(o instanceof $L)) return false", className); equalsBuilder.addCode("\n"); equalsBuilder.addStatement("$L that = ($L) o", className, className); equalsBuilder.addCode("\n"); ClassName equalsBuilderClass = ClassName.get(EqualsBuilder.class); equalsBuilder.addCode("return new $T()\n", equalsBuilderClass); if (testSuperEquality) { equalsBuilder.addCode("\t\t.appendSuper(super.equals(that))\n"); } ClassName hashCodeBuilderClass = ClassName.get(HashCodeBuilder.class); hashCodeBuilder.addCode("return new $T()\n", hashCodeBuilderClass); if (testSuperEquality) { hashCodeBuilder.addCode("\t\t.appendSuper(super.hashCode())\n"); } for (String field : nonStaticFields) { equalsBuilder.addCode("\t\t.append($L, that.$L)\n", field, field); hashCodeBuilder.addCode("\t\t.append($L)\n", field); } equalsBuilder.addCode("\t\t.isEquals();\n"); hashCodeBuilder.addCode("\t\t.toHashCode();\n"); }
CommonsLang3Style extends ObjectMethodsStyle { @Override public void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality) { String className = metaInfo.getClassName(); equalsBuilder.addStatement("if (this == o) return true"); equalsBuilder.addStatement("if (!(o instanceof $L)) return false", className); equalsBuilder.addCode("\n"); equalsBuilder.addStatement("$L that = ($L) o", className, className); equalsBuilder.addCode("\n"); ClassName equalsBuilderClass = ClassName.get(EqualsBuilder.class); equalsBuilder.addCode("return new $T()\n", equalsBuilderClass); if (testSuperEquality) { equalsBuilder.addCode("\t\t.appendSuper(super.equals(that))\n"); } ClassName hashCodeBuilderClass = ClassName.get(HashCodeBuilder.class); hashCodeBuilder.addCode("return new $T()\n", hashCodeBuilderClass); if (testSuperEquality) { hashCodeBuilder.addCode("\t\t.appendSuper(super.hashCode())\n"); } for (String field : nonStaticFields) { equalsBuilder.addCode("\t\t.append($L, that.$L)\n", field, field); hashCodeBuilder.addCode("\t\t.append($L)\n", field); } equalsBuilder.addCode("\t\t.isEquals();\n"); hashCodeBuilder.addCode("\t\t.toHashCode();\n"); } }
CommonsLang3Style extends ObjectMethodsStyle { @Override public void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality) { String className = metaInfo.getClassName(); equalsBuilder.addStatement("if (this == o) return true"); equalsBuilder.addStatement("if (!(o instanceof $L)) return false", className); equalsBuilder.addCode("\n"); equalsBuilder.addStatement("$L that = ($L) o", className, className); equalsBuilder.addCode("\n"); ClassName equalsBuilderClass = ClassName.get(EqualsBuilder.class); equalsBuilder.addCode("return new $T()\n", equalsBuilderClass); if (testSuperEquality) { equalsBuilder.addCode("\t\t.appendSuper(super.equals(that))\n"); } ClassName hashCodeBuilderClass = ClassName.get(HashCodeBuilder.class); hashCodeBuilder.addCode("return new $T()\n", hashCodeBuilderClass); if (testSuperEquality) { hashCodeBuilder.addCode("\t\t.appendSuper(super.hashCode())\n"); } for (String field : nonStaticFields) { equalsBuilder.addCode("\t\t.append($L, that.$L)\n", field, field); hashCodeBuilder.addCode("\t\t.append($L)\n", field); } equalsBuilder.addCode("\t\t.isEquals();\n"); hashCodeBuilder.addCode("\t\t.toHashCode();\n"); }  }
CommonsLang3Style extends ObjectMethodsStyle { @Override public void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality) { String className = metaInfo.getClassName(); equalsBuilder.addStatement("if (this == o) return true"); equalsBuilder.addStatement("if (!(o instanceof $L)) return false", className); equalsBuilder.addCode("\n"); equalsBuilder.addStatement("$L that = ($L) o", className, className); equalsBuilder.addCode("\n"); ClassName equalsBuilderClass = ClassName.get(EqualsBuilder.class); equalsBuilder.addCode("return new $T()\n", equalsBuilderClass); if (testSuperEquality) { equalsBuilder.addCode("\t\t.appendSuper(super.equals(that))\n"); } ClassName hashCodeBuilderClass = ClassName.get(HashCodeBuilder.class); hashCodeBuilder.addCode("return new $T()\n", hashCodeBuilderClass); if (testSuperEquality) { hashCodeBuilder.addCode("\t\t.appendSuper(super.hashCode())\n"); } for (String field : nonStaticFields) { equalsBuilder.addCode("\t\t.append($L, that.$L)\n", field, field); hashCodeBuilder.addCode("\t\t.append($L)\n", field); } equalsBuilder.addCode("\t\t.isEquals();\n"); hashCodeBuilder.addCode("\t\t.toHashCode();\n"); }  @Override void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo); @Override void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality); }
CommonsLang3Style extends ObjectMethodsStyle { @Override public void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality) { String className = metaInfo.getClassName(); equalsBuilder.addStatement("if (this == o) return true"); equalsBuilder.addStatement("if (!(o instanceof $L)) return false", className); equalsBuilder.addCode("\n"); equalsBuilder.addStatement("$L that = ($L) o", className, className); equalsBuilder.addCode("\n"); ClassName equalsBuilderClass = ClassName.get(EqualsBuilder.class); equalsBuilder.addCode("return new $T()\n", equalsBuilderClass); if (testSuperEquality) { equalsBuilder.addCode("\t\t.appendSuper(super.equals(that))\n"); } ClassName hashCodeBuilderClass = ClassName.get(HashCodeBuilder.class); hashCodeBuilder.addCode("return new $T()\n", hashCodeBuilderClass); if (testSuperEquality) { hashCodeBuilder.addCode("\t\t.appendSuper(super.hashCode())\n"); } for (String field : nonStaticFields) { equalsBuilder.addCode("\t\t.append($L, that.$L)\n", field, field); hashCodeBuilder.addCode("\t\t.append($L)\n", field); } equalsBuilder.addCode("\t\t.isEquals();\n"); hashCodeBuilder.addCode("\t\t.toHashCode();\n"); }  @Override void processToString(MethodSpec.Builder toStringBuilder, List<String> nonStaticFields, MetaInfo metaInfo); @Override void processEqualsAndHashCode(MethodSpec.Builder equalsBuilder, MethodSpec.Builder hashCodeBuilder, List<String> nonStaticFields, MetaInfo metaInfo, boolean testSuperEquality);  }