Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8319982

Override toString() for ZipFile

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      Implementing the toString() method for an existing JDK class should likely only pose a compatibility risk if an application is depending on the previous return value. This is highly unlikely as the previous return value was the default hash code value (since the method had not been previously implemented by this class).
      Show
      Implementing the toString() method for an existing JDK class should likely only pose a compatibility risk if an application is depending on the previous return value. This is highly unlikely as the previous return value was the default hash code value (since the method had not been previously implemented by this class).
    • Java API
    • SE

      Summary

      Override and provide an implementation for the toString() method for java.util.zip.ZipFile.

      Problem

      Code that currently prints an instance of java.util.zip.ZipFile or java.util.jar.JarFile (which extends ZipFile) can benefit from ZipFile implementing toString() so that an informative representation is returned rather than the default hexadecimal representation of the hash code.

      This is beneficial for any instances of these classes that are passed to println, printf, the string concatenation operator, or assert.

      Additionally, many debuggers (such as IntelliJ) invoke toString() when representing the state of an object.

      Solution

      Override and provide an implementation of toString() in ZipFile, which JarFile can simply inherit.

      The value returned by toString() is the base name of the underlying ZIP file followed by a hash code value (to distinguish ZipFiles that may be backed by the same underlying file). For example, a ZIP file backed by the underlying file: "/Users/foo/bar/quux.zip" would be represented as something like: "quux.zip@6f0b9af5".

      There is an intentional lack of specification so users will not rely on any indicated format, and only treat it as a debug string.

      Specification

      -    private final String name;     // zip file name
      +    private final String filePath;     // zip file path
      +    private final String fileName;     // name of the file
           private volatile boolean closeRequested;
      
      @@ -245,7 +246,8 @@ public ZipFile(File file, int mode, Charset charset) throws IOException
      -        this.name = name;
      +        this.filePath = name;
      +        this.fileName = file.getName();
               long t0 = System.nanoTime();
      
      @@ -483,7 +485,16 @@ public int available() throws IOException {
            * @return the path name of the ZIP file
            */
           public String getName() {
      -        return name;
      +        return filePath;
      +    }
      +
      +    /**
      +     * {@return a string identifying this {@code ZipFile}, for debugging}
      +     */
      +    @Override
      +    public String toString() {
      +        return this.fileName
      +                + "@" + Integer.toHexString(System.identityHashCode(this));
           }

            jlu Justin Lu
            coffeys Sean Coffey
            Jaikiran Pai, Lance Andersen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: