spotify / dataenum

Algebraic data types in Java.
Apache License 2.0
166 stars 16 forks source link

Using byte[] as a field results in spotbugs warnings #28

Closed sedovmik closed 5 years ago

sedovmik commented 5 years ago

Generated class for a dataenum_case with a field of type byte[] produces 3 spotbugs warnings:

(This method invokes the .equals(Object o) method on an array. Since arrays do not override the equals method of Object, calling equals on an array is the same as comparing their addresses. To compare the contents of the arrays, use java.util.Arrays.equals(Object[], Object[]). To compare the addresses of the arrays, it would be less confusing to explicitly check pointer equality using ==.)

(The code invokes hashCode on an array. Calling hashCode on an array returns the same value as System.identityHashCode, and ignores the contents and length of the array. If you need a hashCode that depends on the contents of an array a, use java.util.Arrays.hashCode(a).)

Example:

@DataEnum
public interface Credentials_dataenum {
    dataenum_case Token(byte[] blob);
    ...
}
@Generated("com.spotify.dataenum.processor.DataEnumProcessor")
public abstract class Credentials {

  ...

  public static final class Token extends Credentials {
    private final byte[] blob;

    @Override
    public boolean equals(Object other) {
      if (other == this) return true;
      if (!(other instanceof Credentials)) return false;
      Credentials o = (Credentials) other;
      return o.blob.equals(this.blob); <- EC_BAD_ARRAY_COMPARE
    }

    @Override
    public int hashCode() {
      return blob.hashCode(); <-DMI_INVOKING_HASHCODE_ON_ARRAY 
    }

    @Override
    public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("Credentials{blob=").append(blob); <- DMI_INVOKING_TOSTRING_ON_ARRAY 
      return builder.append('}').toString();
    }
   }
}