Skip to content

Commit

Permalink
Use Arrays.equals for array type fields. fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwegener committed Apr 14, 2016
1 parent 96a8791 commit 9d57a68
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean run(final Outline outline, final Options options, final ErrorHand

final JDefinedClass implClass = classOutline.implClass;

if (!skipToString && !implClass.isAbstract() && implClass.getMethod("toString",new JType[0]) == null) {
if (!skipToString && !implClass.isAbstract() && implClass.getMethod("toString", new JType[0]) == null) {
generateToStringMethod(model, implClass);
}

Expand All @@ -88,7 +88,7 @@ public boolean run(final Outline outline, final Options options, final ErrorHand
protected void generateToStringMethod(JCodeModel model, JDefinedClass clazz) {
final JMethod toStringMethod = clazz.method(JMod.PUBLIC, String.class,"toString");
toStringMethod.annotate(Override.class);
final JClass objects = model.ref(com.google.common.base.Objects.class);
final JClass objects = model.ref("com.google.common.base.Objects");
final Collection<JFieldVar> superClassInstanceFields = getInstanceFields(getSuperclassFields(clazz));
final Collection<JFieldVar> thisClassInstanceFields = getInstanceFields(clazz.fields().values());

Expand Down Expand Up @@ -122,7 +122,7 @@ protected void generateHashCodeMethod(JCodeModel model, JDefinedClass clazz) {
final JClass objects = model.ref(com.google.common.base.Objects.class);
final Collection<JFieldVar> thisClassInstanceFields = getInstanceFields(clazz.fields().values());
final Collection<JFieldVar> superClassInstanceFields = getInstanceFields(getSuperclassFields(clazz));
// Dont create hashCode for empty classes
// Don't create hashCode for empty classes
if (thisClassInstanceFields.isEmpty() && superClassInstanceFields.isEmpty()) return;


Expand All @@ -147,14 +147,15 @@ protected void generateHashCodeMethod(JCodeModel model, JDefinedClass clazz) {
protected void generateEqualsMethod(JCodeModel model, JDefinedClass clazz) {
final Collection<JFieldVar> superClassInstanceFields = getInstanceFields(getSuperclassFields(clazz));
final Collection<JFieldVar> thisClassInstanceFields = getInstanceFields(clazz.fields().values());
// Dont create hashCode for empty classes
// Don't create equals for empty classes
if (thisClassInstanceFields.isEmpty() && superClassInstanceFields.isEmpty()) return;

final JMethod equalsMethod = clazz.method(JMod.PUBLIC, model.BOOLEAN ,"equals");
equalsMethod.annotate(Override.class);
final JVar other = equalsMethod.param(Object.class,"other");

final JClass objects = model.ref(com.google.common.base.Objects.class);
final JClass arrays = model.ref(java.util.Arrays.class);

final JBlock content = equalsMethod.body();

Expand All @@ -172,14 +173,25 @@ protected void generateEqualsMethod(JCodeModel model, JDefinedClass clazz) {
final JVar otherTypesafe = content.decl(JMod.FINAL, clazz, "o", JExpr.cast(clazz, other));

for (JFieldVar superField : superClassInstanceFields) {
final JInvocation equalsInvocation = objects.staticInvoke("equal");
final JInvocation equalsInvocation;
if (superField.type().isArray()) {
equalsInvocation = arrays.staticInvoke("equals");
} else {
equalsInvocation = objects.staticInvoke("equal");
}

equalsInvocation.arg(JExpr._this().ref(superField));
equalsInvocation.arg(otherTypesafe.ref(superField));
equalsBuilder = equalsBuilder.cand(equalsInvocation);
}

for (JFieldVar thisField : thisClassInstanceFields) {
final JInvocation equalsInvocation = objects.staticInvoke("equal");
final JInvocation equalsInvocation;
if (thisField.type().isArray()) {
equalsInvocation = arrays.staticInvoke("equals");
} else {
equalsInvocation = objects.staticInvoke("equal");
}
equalsInvocation.arg(JExpr._this().ref(thisField));
equalsInvocation.arg(otherTypesafe.ref(thisField));
equalsBuilder = equalsBuilder.cand(equalsInvocation);
Expand Down

0 comments on commit 9d57a68

Please sign in to comment.