Skip to content

Commit 19a9c1a

Browse files
committed
Do not call addListener in constructors of CCombo and StyledText #2733
The method is not final, which may cause issues if a subclass overrides it because it ends up being called upon instantiation, when the object is not fully initialized Since these 2 classes are not in the same package as other subclasses of Widget, a new internal API was used. Fixes #2733
1 parent 9e2bdb5 commit 19a9c1a

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public CCombo (Composite parent, int style) {
173173
}
174174

175175
initAccessible();
176-
addListener(SWT.ZoomChanged, this::handleDPIChange);
176+
_addListenerFinal(SWT.ZoomChanged, this::handleDPIChange);
177177
}
178178
static int checkStyle (int style) {
179179
int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.LEAD | SWT.CENTER | SWT.TRAIL;

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ public StyledText(Composite parent, int style) {
758758
initializeAccessible();
759759
setData("DEFAULT_DROP_TARGET_EFFECT", new StyledTextDropTargetEffect(this));
760760
if (IS_MAC) setData(STYLEDTEXT_KEY);
761-
addListener(SWT.ZoomChanged, this::handleDPIChange);
761+
_addListenerFinal(SWT.ZoomChanged, this::handleDPIChange);
762762
}
763763
/**
764764
* Adds an extended modify listener. An ExtendedModify event is sent by the

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Widget.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,40 @@ protected void addTypedListener (EventListener listener, int... eventTypes) {
507507
}
508508
}
509509

510-
void _addListener (int eventType, Listener listener) {
510+
/**
511+
* Same as {@linkplain #_addListener(int, org.eclipse.swt.widgets.Listener)} but
512+
* intentionally declared as {@code final} so subclasses may safely call it in
513+
* their constructors.
514+
*
515+
* @see <a href=
516+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
517+
* @since 3.132
518+
* @noreference This method is not intended to be referenced by clients.
519+
*/
520+
protected final void _addListenerFinal (int eventType, Listener listener) {
511521
if (eventTable == null) eventTable = new EventTable ();
512522
eventTable.hook (eventType, listener);
513523
}
514524

525+
/**
526+
* Same as {@linkplain #_removeListener(int, org.eclipse.swt.widgets.Listener)}
527+
* but intentionally declared as {@code final} so subclasses may safely call it
528+
* in their constructors.
529+
*
530+
* @see <a href=
531+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
532+
* @since 3.132
533+
* @noreference This method is not intended to be referenced by clients.
534+
*/
535+
protected final void _removeListenerFinal (int eventType, Listener listener) {
536+
if (eventTable == null) return;
537+
eventTable.unhook (eventType, listener);
538+
}
539+
540+
void _addListener (int eventType, Listener listener) {
541+
_addListenerFinal(eventType, listener);
542+
}
543+
515544
/**
516545
* Adds the listener to the collection of listeners who will
517546
* be notified when the widget is disposed. When the widget is

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Widget.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,40 @@ public Widget (Widget parent, int style) {
297297
notifyCreationTracker();
298298
}
299299

300-
void _addListener (int eventType, Listener listener) {
300+
/**
301+
* Same as {@linkplain #_addListener(int, org.eclipse.swt.widgets.Listener)} but
302+
* intentionally declared as {@code final} so subclasses may safely call it in
303+
* their constructors.
304+
*
305+
* @see <a href=
306+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
307+
* @since 3.132
308+
* @noreference This method is not intended to be referenced by clients.
309+
*/
310+
protected final void _addListenerFinal (int eventType, Listener listener) {
301311
if (eventTable == null) eventTable = new EventTable ();
302312
eventTable.hook (eventType, listener);
303313
}
304314

315+
/**
316+
* Same as {@linkplain #_removeListener(int, org.eclipse.swt.widgets.Listener)}
317+
* but intentionally declared as {@code final} so subclasses may safely call it
318+
* in their constructors.
319+
*
320+
* @see <a href=
321+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
322+
* @since 3.132
323+
* @noreference This method is not intended to be referenced by clients.
324+
*/
325+
protected final void _removeListenerFinal (int eventType, Listener listener) {
326+
if (eventTable == null) return;
327+
eventTable.unhook (eventType, listener);
328+
}
329+
330+
void _addListener (int eventType, Listener listener) {
331+
_addListenerFinal(eventType, listener);
332+
}
333+
305334
/**
306335
* Adds the listener to the collection of {@link Listener listeners} who will
307336
* be notified when an event of the given type occurs. When the

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Widget.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,42 @@ void registerDPIChangeListener() {
196196
}
197197
}
198198

199-
void _addListener (int eventType, Listener listener) {
199+
/**
200+
* Same as {@linkplain #_addListener(int, org.eclipse.swt.widgets.Listener)} but
201+
* intentionally declared as {@code final} so subclasses may safely call it in
202+
* their constructors.
203+
*
204+
* @see <a href=
205+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
206+
* @since 3.132
207+
* @noreference This method is not intended to be referenced by clients.
208+
*/
209+
protected final void _addListenerFinal (int eventType, Listener listener) {
200210
if (eventTable == null) eventTable = new EventTable ();
201211
eventTable.hook (eventType, listener);
202212
}
203213

204-
void _removeListener (int eventType, Listener listener) {
214+
/**
215+
* Same as {@linkplain #_removeListener(int, org.eclipse.swt.widgets.Listener)}
216+
* but intentionally declared as {@code final} so subclasses may safely call it
217+
* in their constructors.
218+
*
219+
* @see <a href=
220+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
221+
* @since 3.132
222+
* @noreference This method is not intended to be referenced by clients.
223+
*/
224+
protected final void _removeListenerFinal(int eventType, Listener listener) {
205225
if (eventTable == null) return;
206-
eventTable.unhook (eventType, listener);
226+
eventTable.unhook(eventType, listener);
227+
}
228+
229+
void _addListener (int eventType, Listener listener) {
230+
_addListenerFinal(eventType, listener);
231+
}
232+
233+
void _removeListener (int eventType, Listener listener) {
234+
_removeListenerFinal(eventType, listener);
207235
}
208236

209237
/**

0 commit comments

Comments
 (0)