JDK-7041136 : Use Objects.equals in JDK platform classes
  • Type: Bug
  • Component: other-libs
  • Sub-Component: other
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-05-02
  • Updated: 2019-08-27
  • Resolved: 2011-06-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b142Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
The java.util.Objects class includs a two-argument static equals method (6797535).  This method should replace existing analagous private methods in the JDK codebase.

Comments
EVALUATION A fine idea.
02-05-2011

SUGGESTED FIX # HG changeset patch # User darcy # Date 1304361586 25200 # Node ID bd1ffb167be055056c12bc0e6c17cd3580755d55 # Parent fa17f2b9a6d52567924ba62bb4a2503d3f1000d3 7041136: Use Objects.equals in JDK platform classes Reviewed-by: alanb, mduigou --- a/src/share/classes/java/beans/DefaultPersistenceDelegate.java Mon May 02 11:42:52 2011 -0700 +++ b/src/share/classes/java/beans/DefaultPersistenceDelegate.java Mon May 02 11:39:46 2011 -0700 @@ -26,6 +26,7 @@ package java.beans; import java.util.*; import java.lang.reflect.*; +import java.util.Objects; import sun.reflect.misc.*; @@ -181,10 +182,6 @@ public class DefaultPersistenceDelegate return method; } - private static boolean equals(Object o1, Object o2) { - return (o1 == null) ? (o2 == null) : o1.equals(o2); - } - private void doProperty(Class type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out) throws Exception { Method getter = pd.getReadMethod(); Method setter = pd.getWriteMethod(); @@ -195,7 +192,7 @@ public class DefaultPersistenceDelegate Object oldValue = oldGetExp.getValue(); Object newValue = newGetExp.getValue(); out.writeExpression(oldGetExp); - if (!equals(newValue, out.get(oldValue))) { + if (!Objects.equals(newValue, out.get(oldValue))) { // Search for a static constant with this value; Object e = (Object[])pd.getValue("enumerationValues"); if (e instanceof Object[] && Array.getLength(e) % 3 == 0) { @@ -233,7 +230,7 @@ public class DefaultPersistenceDelegate Object oldValue = oldGetExp.getValue(); Object newValue = newGetExp.getValue(); out.writeExpression(oldGetExp); - if (!equals(newValue, out.get(oldValue))) { + if (!Objects.equals(newValue, out.get(oldValue))) { out.writeStatement(new Statement(field, "set", new Object[] { oldInstance, oldValue })); } } --- a/src/share/classes/java/beans/MetaData.java Mon May 02 11:42:52 2011 -0700 +++ b/src/share/classes/java/beans/MetaData.java Mon May 02 11:39:46 2011 -0700 @@ -56,6 +56,8 @@ import javax.swing.plaf.ColorUIResource; import sun.swing.PrintColorUIResource; +import java.util.Objects; + /* * Like the <code>Intropector</code>, the <code>MetaData</code> class * contains <em>meta</em> objects that describe the way @@ -134,7 +136,7 @@ class ArrayPersistenceDelegate extends P Object oldValue = oldGetExp.getValue(); Object newValue = newGetExp.getValue(); out.writeExpression(oldGetExp); - if (!MetaData.equals(newValue, out.get(oldValue))) { + if (!Objects.equals(newValue, out.get(oldValue))) { // System.out.println("Not equal: " + newGetExp + " != " + actualGetExp); // invokeStatement(Array.class, "set", new Object[]{oldInstance, index, oldValue}, out); DefaultPersistenceDelegate.invokeStatement(oldInstance, "set", new Object[]{index, oldValue}, out); @@ -635,7 +637,7 @@ class java_util_List_PersistenceDelegate Object oldValue = oldGetExp.getValue(); Object newValue = newGetExp.getValue(); out.writeExpression(oldGetExp); - if (!MetaData.equals(newValue, out.get(oldValue))) { + if (!Objects.equals(newValue, out.get(oldValue))) { invokeStatement(oldInstance, "set", new Object[]{index, oldValue}, out); } } @@ -675,7 +677,7 @@ class java_util_Map_PersistenceDelegate Object oldValue = oldGetExp.getValue(); Object newValue = newGetExp.getValue(); out.writeExpression(oldGetExp); - if (!MetaData.equals(newValue, out.get(oldValue))) { + if (!Objects.equals(newValue, out.get(oldValue))) { invokeStatement(oldInstance, "put", new Object[]{oldKey, oldValue}, out); } else if ((newValue == null) && !newMap.containsKey(oldKey)) { // put oldValue(=null?) if oldKey is absent in newMap @@ -899,17 +901,17 @@ class java_awt_Component_PersistenceDele if (!(oldInstance instanceof java.awt.Window)) { Object oldBackground = c.isBackgroundSet() ? c.getBackground() : null; Object newBackground = c2.isBackgroundSet() ? c2.getBackground() : null; - if (!MetaData.equals(oldBackground, newBackground)) { + if (!Objects.equals(oldBackground, newBackground)) { invokeStatement(oldInstance, "setBackground", new Object[] { oldBackground }, out); } Object oldForeground = c.isForegroundSet() ? c.getForeground() : null; Object newForeground = c2.isForegroundSet() ? c2.getForeground() : null; - if (!MetaData.equals(oldForeground, newForeground)) { + if (!Objects.equals(oldForeground, newForeground)) { invokeStatement(oldInstance, "setForeground", new Object[] { oldForeground }, out); } Object oldFont = c.isFontSet() ? c.getFont() : null; Object newFont = c2.isFontSet() ? c2.getFont() : null; - if (!MetaData.equals(oldFont, newFont)) { + if (!Objects.equals(oldFont, newFont)) { invokeStatement(oldInstance, "setFont", new Object[] { oldFont }, out); } } @@ -1304,10 +1306,6 @@ class MetaData { internalPersistenceDelegates.put("java.util.JumboEnumSet", new java_util_EnumSet_PersistenceDelegate()); internalPersistenceDelegates.put("java.util.RegularEnumSet", new java_util_EnumSet_PersistenceDelegate()); - } - - /*pp*/ static boolean equals(Object o1, Object o2) { - return (o1 == null) ? (o2 == null) : o1.equals(o2); } public synchronized static PersistenceDelegate getPersistenceDelegate(Class type) { --- a/src/share/classes/java/net/HttpCookie.java Mon May 02 11:42:52 2011 -0700 +++ b/src/share/classes/java/net/HttpCookie.java Mon May 02 11:39:46 2011 -0700 @@ -34,6 +34,7 @@ import java.util.Date; import java.lang.NullPointerException; // for javadoc import java.util.Locale; +import java.util.Objects; /** * An HttpCookie object represents an http cookie, which carries state @@ -817,7 +818,7 @@ public final class HttpCookie implements // 3. and have same path (case-sensitive). return equalsIgnoreCase(getName(), other.getName()) && equalsIgnoreCase(getDomain(), other.getDomain()) && - equals(getPath(), other.getPath()); + Objects.equals(getPath(), other.getPath()); } @@ -1162,14 +1163,6 @@ public final class HttpCookie implements return false; } - private static boolean equals(String s, String t) { - if (s == t) return true; - if ((s != null) && (t != null)) { - return s.equals(t); - } - return false; - } - private static boolean startsWithIgnoreCase(String s, String start) { if (s == null || start == null) return false;
02-05-2011

PUBLIC COMMENTS See http://hg.openjdk.java.net/jdk7/tl/jdk/rev/bd1ffb167be0
02-05-2011