Skip to content

Commit 887d96c

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 887d96c

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,38 @@ protected void addTypedListener (EventListener listener, int... eventTypes) {
507507
}
508508
}
509509

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

524+
/**
525+
* Same as {code _removeListener(int, Listener)} but intentionally declared as
526+
* {@code final} so subclasses may safely call it in their constructors.
527+
*
528+
* @see <a href=
529+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
530+
* @since 3.132
531+
* @noreference This method is not intended to be referenced by clients.
532+
*/
533+
protected final void _removeListenerFinal (int eventType, Listener listener) {
534+
if (eventTable == null) return;
535+
eventTable.unhook (eventType, listener);
536+
}
537+
538+
void _addListener (int eventType, Listener listener) {
539+
_addListenerFinal(eventType, listener);
540+
}
541+
515542
/**
516543
* Adds the listener to the collection of listeners who will
517544
* 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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,38 @@ public Widget (Widget parent, int style) {
297297
notifyCreationTracker();
298298
}
299299

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

314+
/**
315+
* Same as {@code _removeListener(int, Listener)} but intentionally declared as
316+
* {@code final} so subclasses may safely call it in their constructors.
317+
*
318+
* @see <a href=
319+
* "https://github.com/eclipse-platform/eclipse.platform.swt/issues/2733">#2733</a>
320+
* @since 3.132
321+
* @noreference This method is not intended to be referenced by clients.
322+
*/
323+
protected final void _removeListenerFinal (int eventType, Listener listener) {
324+
if (eventTable == null) return;
325+
eventTable.unhook (eventType, listener);
326+
}
327+
328+
void _addListener (int eventType, Listener listener) {
329+
_addListenerFinal(eventType, listener);
330+
}
331+
305332
/**
306333
* Adds the listener to the collection of {@link Listener listeners} who will
307334
* 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: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,40 @@ void registerDPIChangeListener() {
196196
}
197197
}
198198

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

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

209235
/**

0 commit comments

Comments
 (0)