8168998: Incorrect implementation of equals in Encoding and Type in JavaSound
authorserb
Wed, 02 Nov 2016 17:14:57 +0300
changeset 42186 7092af944dca
parent 42185 1f7a0a6eb773
child 42187 b2f0bdbfd4f2
8168998: Incorrect implementation of equals in Encoding and Type in JavaSound Reviewed-by: prr, amenkov
jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java
jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java
jdk/test/javax/sound/sampled/AudioFileFormat/TypeEqualsToNull.java
jdk/test/javax/sound/sampled/AudioFormat/EncodingEqualsToNull.java
--- a/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java	Wed Nov 02 08:46:41 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java	Wed Nov 02 17:14:57 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 /**
  * An instance of the {@code AudioFileFormat} class describes an audio file,
@@ -344,7 +345,7 @@
          * @param  extension the string that commonly marks the file type
          *         without leading dot
          */
-        public Type(String name, String extension) {
+        public Type(final String name, final String extension) {
             this.name = name;
             this.extension = extension;
         }
@@ -353,14 +354,14 @@
          * Finalizes the equals method.
          */
         @Override
-        public final boolean equals(Object obj) {
-            if (toString() == null) {
-                return (obj != null) && (obj.toString() == null);
+        public final boolean equals(final Object obj) {
+            if (this == obj) {
+                return true;
             }
-            if (obj instanceof Type) {
-                return toString().equals(obj.toString());
+            if (!(obj instanceof Type)) {
+                return false;
             }
-            return false;
+            return Objects.equals(name, ((Type) obj).name);
         }
 
         /**
@@ -368,10 +369,7 @@
          */
         @Override
         public final int hashCode() {
-            if (toString() == null) {
-                return 0;
-            }
-            return toString().hashCode();
+            return name != null ? name.hashCode() : 0;
         }
 
         /**
--- a/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java	Wed Nov 02 08:46:41 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java	Wed Nov 02 17:14:57 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 /**
  * {@code AudioFormat} is the class that specifies a particular arrangement of
@@ -599,7 +600,7 @@
          *
          * @param  name the name of the new type of encoding
          */
-        public Encoding(String name) {
+        public Encoding(final String name) {
             this.name = name;
         }
 
@@ -607,14 +608,14 @@
          * Finalizes the equals method.
          */
         @Override
-        public final boolean equals(Object obj) {
-            if (toString() == null) {
-                return (obj != null) && (obj.toString() == null);
+        public final boolean equals(final Object obj) {
+            if (this == obj) {
+                return true;
             }
-            if (obj instanceof Encoding) {
-                return toString().equals(obj.toString());
+            if (!(obj instanceof Encoding)) {
+                return false;
             }
-            return false;
+            return Objects.equals(name, ((Encoding) obj).name);
         }
 
         /**
@@ -622,10 +623,7 @@
          */
         @Override
         public final int hashCode() {
-            if (toString() == null) {
-                return 0;
-            }
-            return toString().hashCode();
+            return name != null ? name.hashCode() : 0;
         }
 
         /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/sound/sampled/AudioFileFormat/TypeEqualsToNull.java	Wed Nov 02 17:14:57 2016 +0300
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import javax.sound.sampled.AudioFileFormat;
+
+/**
+ * @test
+ * @bug 8168998
+ */
+public final class TypeEqualsToNull {
+
+    public static void main(final String[] args) {
+        final AudioFileFormat.Type type;
+        try {
+            type = new AudioFileFormat.Type(null, null);
+        } catch (final Exception ignored) {
+            // behaviour of null is not specified so ignore possible exceptions
+            return;
+        }
+        final Object stub = new Object() {
+            @Override
+            public String toString() {
+                return null;
+            }
+        };
+        if (stub.equals(type) || type.equals(stub)) {
+            throw new RuntimeException("Should not be equal");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/sound/sampled/AudioFormat/EncodingEqualsToNull.java	Wed Nov 02 17:14:57 2016 +0300
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import javax.sound.sampled.AudioFormat;
+
+/**
+ * @test
+ * @bug 8168998
+ */
+public final class EncodingEqualsToNull {
+
+    public static void main(final String[] args) {
+        final AudioFormat.Encoding enc;
+        try {
+            enc = new AudioFormat.Encoding(null);
+        } catch (final Exception ignored) {
+            // behaviour of null is not specified so ignore possible exceptions
+            return;
+        }
+        final Object stub = new Object() {
+            @Override
+            public String toString() {
+                return null;
+            }
+        };
+        if (stub.equals(enc) || enc.equals(stub)) {
+            throw new RuntimeException("Should not be equal");
+        }
+    }
+}