Bug 20758 - Spec clarification: Senders of PropertyChangeEvent.getProperty() should use equals, not ==
Summary: Spec clarification: Senders of PropertyChangeEvent.getProperty() should use e...
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 2.0 F4   Edit
Assignee: Nick Edgar CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-06-20 16:48 EDT by Nick Edgar CLA
Modified: 2002-06-24 09:47 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Edgar CLA 2002-06-20 16:48:50 EDT
Build 20020618

The spec for PropertyChangeEvent.getProperty() should warn callers that they 
should use equals, not ==, to compare property names.

From a note to eclipse-dev:

We have recently seen several instances of a programming error whereby 
implementors of IPropertyChangeListener use == to check the value of 
PropertyChangeEvent.getProperty(), rather than using equals().

This is incorrect because there is no guarantee that the property name in the 
event is a constant.
For example, the Workbench / Preferences / Import... action reads the property 
names and values from a file.  It sends out notifications to any activated 
plugins, but the string read from the file for the property name will not be == 
to the corresponding string constant.
(We considered interning the strings in the preferences import, but this is not 
the right answer and would merely hide the problem until some other use case 
reads property names from a file, not to mention encouraging bad programming 
practices.)

Please ensure that all senders of PropertyChangeEvent.getProperty() use equals 
rather than ==.
Although the class ensures that the property name is not null, a safe idiom to 
use is the following:
if (MY_PROPERTY.equals(event.getProperty())) {
  // update accordingly
}

The preferences facility has recently been pushed down to core runtime from 
JFace, but the JFace APIs still exist and are still commonly used.
So you will need to check references to both:
org.eclipse.jface.util.PropertyChangeEvent.getProperty(), and
org.eclipse.core.runtime.Preferences$PropertyChangeEvent.getProperty()

If this is not fixed, preferences will appear to work normally when being 
adjusted manually in the preference pages.
However, when preferences are exported and reimported, any active plugins with 
listeners using == will not update properly.

We highly recommend testing all your preferences with the new import/export 
facility.

There are existing PRs for known occurrences in the SDK.
See http://dev.eclipse.org/bugs/show_bug.cgi?id=20471
Comment 1 Nick Edgar CLA 2002-06-20 16:49:12 EDT
Need to fix up both
org.eclipse.jface.util.PropertyChangeEvent.getProperty(), and
org.eclipse.core.runtime.Preferences$PropertyChangeEvent.getProperty()
Comment 2 Nick Edgar CLA 2002-06-21 10:31:19 EDT
Approved by arch team.
Comment 3 Nick Edgar CLA 2002-06-21 23:06:49 EDT
Added the following to the spec for 
org.eclipse.jface.util.PropertyChangeEvent.getProperty():

 * <p>
 * Warning: there is no guarantee that the property name returned
 * is a constant string.  Callers must compare property names using
 * equals, not ==.
Comment 4 Nick Edgar CLA 2002-06-21 23:08:40 EDT
Released.

Randy and DJ, could you please review the change?

Moving to Core to fix up 
org.eclipse.core.runtime.Preferences$PropertyChangeEvent.getPropertyName().
Sorry about the short notice before F4.
Comment 5 DJ Houghton CLA 2002-06-24 09:37:23 EDT
Verified change in JFace.
Fixed and released same change in Runtime.
Comment 6 Randy Giffen CLA 2002-06-24 09:45:42 EDT
Minor point but the change in JFace is missing the </p> tag
Comment 7 Randy Giffen CLA 2002-06-24 09:47:32 EDT
Added </p> tag