Bug 566317 - Consider adding a static method to check if a widget is null and not disposed
Summary: Consider adding a static method to check if a widget is null and not disposed
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.14   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 4.18 M1   Edit
Assignee: Lars Vogel CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
Depends on:
Blocks:
 
Reported: 2020-08-24 05:37 EDT by Lars Vogel CLA
Modified: 2020-10-05 04:54 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lars Vogel CLA 2020-08-24 05:37:44 EDT
In RCP client code I frequently see if(mywidget!=null && !mywidget.isDisposed()) {// do stuff}, especially in asynchronous calls.

Maye we should add a static util method to JFace to check this, for example boolean isAvailable(Widget);
Comment 1 Rolf Theunissen CLA 2020-08-24 06:30:33 EDT
Indeed that construct is used all over the place. From a quick scroll through the code some (non-static) functions that offer similar functionality, with names like 'isValid', 'okToUse' and 'exist' (e.g. menuExist).

Places that already implement the static method (among others?):
org.eclipse.ui.internal.ide.application.IDEApplication.isValidShell(Shell)
org.eclipse.jface.internal.text.link.contentassist.Helper2.okToUse(Widget)
org.eclipse.jface.text.contentassist.Helper.okToUse(Widget)
Comment 2 Lars Vogel CLA 2020-08-24 07:08:50 EDT
Thanks Rolf for the check. 

Which name to you like? I like isValid as it is relatively short. 

I would suggest to put the check into JFace.
Comment 3 Eclipse Genie CLA 2020-09-02 02:12:27 EDT
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/168625
Comment 4 Eclipse Genie CLA 2020-09-08 09:07:12 EDT
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/169007
Comment 6 Lars Vogel CLA 2020-09-11 11:49:12 EDT
Rolf, shall we use this bug to replace some of the old usages or shall we do this via a new bug?
Comment 7 Rolf Theunissen CLA 2020-09-11 11:59:27 EDT
(In reply to Lars Vogel from comment #6)
> Rolf, shall we use this bug to replace some of the old usages or shall we do
> this via a new bug?

I don't have a preference. A quick search on the regexp 'null.*isDisposed()' returned 458 matches, which gives an indication for the amount of work. If replacing all cases is desired, it will be probably too much for a single bug report.
Comment 8 Alexander Kurtakov CLA 2020-09-14 01:43:39 EDT
This created build warning due do javadoc error:
../../../eclipse.platform.ui/bundles/org.eclipse.jface/src/org/eclipse/jface/util/Util.java:701: warning: no description for @param
	 * @param widget
	   ^
1 warning
Comment 9 Eclipse Genie CLA 2020-09-14 01:58:54 EDT
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/169272
Comment 10 Eclipse Genie CLA 2020-09-14 02:09:45 EDT
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/169275
Comment 13 Eclipse Genie CLA 2020-09-15 03:10:26 EDT
New Gerrit change created: https://git.eclipse.org/r/c/www.eclipse.org/eclipse/news/+/169341
Comment 15 Lars Vogel CLA 2020-10-05 04:54:43 EDT
I also this as we have now this method and it is used in our code. The remaining places can be adjusted with bug reference, if someone find the time and has the interest of doing this.