Skip to content

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

@sedovmik

Description

@sedovmik

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

  • DMI_INVOKING_TOSTRING_ON_ARRAY

(The code invokes toString on an array, which will generate a fairly useless result such as [C@16f0472. Consider using Arrays.toString to convert the array into a readable String that gives the contents of the array.)

  • EC_BAD_ARRAY_COMPARE

(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 ==.)

  • DMI_INVOKING_HASHCODE_ON_ARRAY

(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();
    }
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions