Skip to content

Commit a9822b8

Browse files
committed
[NotifyDescriptor] Refactor return values to not rely on Integer identity.
- return values must mimic Integer instances and their equals contracts - unit test used the wrong return value (int instead of Object)
1 parent ce77b5c commit a9822b8

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

platform/openide.dialogs/nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
is.autoload=true
1919
javac.compilerargs=-Xlint:unchecked
20-
javac.source=1.8
20+
javac.release=17
2121
#javadoc.main.page=org/openide/doc-files/api.html
2222
javadoc.arch=${basedir}/arch.xml
2323
javadoc.apichanges=${basedir}/apichanges.xml

platform/openide.dialogs/src/org/openide/NotifyDescriptor.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.beans.PropertyChangeEvent;
2828
import java.beans.PropertyChangeListener;
2929
import java.beans.PropertyChangeSupport;
30+
import java.io.Serializable;
3031
import java.lang.reflect.InvocationTargetException;
3132
import java.util.ArrayList;
3233
import java.util.Collections;
@@ -129,24 +130,50 @@ public class NotifyDescriptor extends Object {
129130
*/
130131
public static final String PROP_INFO_NOTIFICATION = "infoNotification"; // NOI18N
131132

133+
/**
134+
* Used to be a <code>new Integer(JOptionPane.FOO_OPTION)</code> instance.
135+
*
136+
* For compatibility reasons the public Object constants must have distinct
137+
* identity, but equals must behave like Integer's equals and compare only
138+
* the wrapped int like a Record.
139+
*/
140+
private record ReturnValue(int option) implements Serializable, Comparable<ReturnValue> {
141+
142+
@Override
143+
public String toString() {
144+
return String.valueOf(option);
145+
}
146+
147+
@Override
148+
public int hashCode() {
149+
return Integer.hashCode(option);
150+
}
151+
152+
@Override
153+
public int compareTo(ReturnValue other) {
154+
return Integer.compare(option, other.option);
155+
}
156+
157+
}
158+
132159
//
133160
// Return values
134161
//
135162

136163
/** Return value if YES is chosen. */
137-
public static final Object YES_OPTION = new Integer(JOptionPane.YES_OPTION);
164+
public static final Object YES_OPTION = new ReturnValue(JOptionPane.YES_OPTION);
138165

139166
/** Return value if NO is chosen. */
140-
public static final Object NO_OPTION = new Integer(JOptionPane.NO_OPTION);
167+
public static final Object NO_OPTION = new ReturnValue(JOptionPane.NO_OPTION);
141168

142169
/** Return value if CANCEL is chosen. */
143-
public static final Object CANCEL_OPTION = new Integer(JOptionPane.CANCEL_OPTION);
170+
public static final Object CANCEL_OPTION = new ReturnValue(JOptionPane.CANCEL_OPTION);
144171

145172
/** Return value if OK is chosen. */
146-
public static final Object OK_OPTION = new Integer(JOptionPane.OK_OPTION);
173+
public static final Object OK_OPTION = new ReturnValue(JOptionPane.OK_OPTION);
147174

148175
/** Return value if user closes the window without pressing any button. */
149-
public static final Object CLOSED_OPTION = new Integer(JOptionPane.CLOSED_OPTION);
176+
public static final Object CLOSED_OPTION = new ReturnValue(JOptionPane.CLOSED_OPTION);
150177

151178
//
152179
// Option types

platform/openide.dialogs/test/unit/src/org/openide/NotifyDescriptorTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
import java.awt.GraphicsEnvironment;
2222
import java.beans.PropertyChangeEvent;
2323
import java.util.ArrayList;
24+
import java.util.Collections;
25+
import java.util.IdentityHashMap;
2426
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Objects;
29+
import java.util.Set;
2530
import javax.swing.*;
2631
import javax.swing.text.BadLocationException;
2732
import junit.framework.Test;
@@ -134,4 +139,31 @@ JTextField createTextField() {
134139

135140
events.clear();
136141
}
142+
143+
// post migration from boxed type constructors
144+
public void testVerifyReturnOptionCompatibility() {
145+
// used to be new Integer(int)
146+
Object YES_OPTION = NotifyDescriptor.YES_OPTION;
147+
Object NO_OPTION = NotifyDescriptor.NO_OPTION;
148+
Object CANCEL_OPTION = NotifyDescriptor.CANCEL_OPTION;
149+
Object OK_OPTION = NotifyDescriptor.OK_OPTION;
150+
Object CLOSED_OPTION = NotifyDescriptor.CLOSED_OPTION;
151+
152+
// all distinct instances
153+
Set<Object> identitySet = Collections.newSetFromMap(new IdentityHashMap<>());
154+
identitySet.add(YES_OPTION);
155+
identitySet.add(NO_OPTION);
156+
identitySet.add(CANCEL_OPTION);
157+
identitySet.add(OK_OPTION);
158+
identitySet.add(CLOSED_OPTION);
159+
assertEquals(5, identitySet.size());
160+
161+
// same int value
162+
assertEquals(YES_OPTION, OK_OPTION);
163+
164+
// distinct int value
165+
assertNotSame(YES_OPTION, NO_OPTION);
166+
assertNotSame(YES_OPTION, CANCEL_OPTION);
167+
assertNotSame(YES_OPTION, CLOSED_OPTION);
168+
}
137169
}

platform/openide.loaders/test/unit/src/org/openide/text/ExternalChangeOfModifiedFileTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void testModifyTheFileAndThenPreventItToBeSavedOnFileDisappear() throws E
132132
assertNotNull("Panes are still open", arr);
133133
assertTrue("Document is remains modified", edit.isModified());
134134

135-
DD.toReturn.push(DialogDescriptor.YES_NO_OPTION);
135+
DD.toReturn.push(DialogDescriptor.YES_OPTION);
136136

137137
SaveCookie sc = obj.getLookup().lookup(SaveCookie.class);
138138
assertNotNull("File is modified and has save cookie", sc);

0 commit comments

Comments
 (0)