-
Notifications
You must be signed in to change notification settings - Fork 148
Use null safe compare in Platform as values can be null #2319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Test Results 1 926 files - 27 1 926 suites - 27 1h 24m 37s ⏱️ - 6m 27s For more details on these errors, see this check. Results for commit 26a8b12. ± Comparison against base commit 2f623a3. ♻️ This comment has been updated with latest results. |
|
This pull request changes some projects for the first time in this development cycle. An additional commit containing all the necessary changes was pushed to the top of this PR's branch. To obtain these changes (for example if you want to push more changes) either fetch from your fork or apply the git patch. Git patchFurther information are available in Common Build Issues - Missing version increments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a NullPointerException in Platform.OS.is() by replacing direct equality comparison with null-safe Objects.equals(). When Platform.getOS() returns null in some cases, the original implementation would throw NPE, violating the method's contract that claims to return false "in all other cases".
Key Changes
- Replaced
Platform.getOS().equals(osString)withObjects.equals(Platform.getOS(), osString)for null-safe comparison - Added import for
java.util.Objects
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public static boolean is(String osString) { | ||
| return Platform.getOS().equals(osString); | ||
| return Objects.equals(Platform.getOS(), osString); | ||
| } |
Copilot
AI
Dec 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this change correctly fixes the NPE issue, there's no test coverage for the case where Platform.getOS() returns null. Consider adding a test case to verify that OS.is() returns false when Platform.getOS() returns null, ensuring the documented behavior 'false in all other cases' is maintained even when the OS cannot be determined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that the Platform class doesn't work stand alone and that while we can fix symptoms of that, it will produce poor results so maybe better it fail dismally than just fail to work correctly.
For the reported problem it would be better to use SWT.getPlatform() as suggested in:
eclipse-platform/eclipse.platform.ui#3604 (comment)
We seem many uses of that:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that the Platform class doesn't work stand alone and that while we can fix symptoms of that, it will produce poor results so maybe better it fail dismally than just fail to work correctly.
I'm not sure what you mean by "standalone" the class works perfectly fine and uses some kind of delegation model that ultimately falls back to using system properties. In any case I think using a defensive approach here is better than throwing an NPE or any other exception as it does not really helps the user here.
For the reported problem it would be better to use SWT.getPlatform() as suggested in:
Independent from that the Platform class needs to work (independent from SWT).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Independent from that the Platform class needs to work
This can not be correct. Please see Platform class documentation - it requires Platform to be in Running state to be usable. Instead of returning strange values, it might be better to throw an explicit exception and refer clients to class documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can not be correct.
What specifically
Instead of returning strange values,
Why is it strange to say it is not windows if we can not detect it is windows?
it requires Platform to be in Running state to be usable
This is not particular true... the javadoc only says (emphasis by me)
A runtime exception might be thrown or incorrect result might be returned if a method from this class is called while Platform is not running.
So it perfectly aligns with the contracts of the class.
In some cases the return value of Platform.getOS() can be return null but its API doc states it return Platform.OS_UNKNOWN is the OS can not be found. Fix eclipse-platform/eclipse.platform.ui#3604
da489b4 to
ade30da
Compare
|
I now fixed the |
|
It does seem a bit bizarre than one can include the specific SWT jar for a particular platform, yet SWT (or more specifically, the Forms API) can't tell what platform it's running on. That must raise a few eyebrows, and eyebrow elevation is not going to help with adoption levels. SWT is a good technology and good for standalone apps, but if people get counter-intuitive problems like this it can make people give up on SWT. Just my feeling - I'm an SWT fan. If SWT and Forms work without knowing the OS in these cases, that seems fine so long as everything still looks ok. It did notice that the style of Forms Section headers has changed. I think it looked better before. Is that configurable, or is it just like that now? |
This is Eclipse Platform. It can run without SWT or Forms. It can run without any graphical interfcae. Wirhout any graphical subsystem, on a headless hardware. It can be launched without binary executable from a raw Java command, without OS dependent code. Do not confuse it with PlatformUI from Eclipse RCP. |
In some cases the return value of
Platform.getOS()can benullresulting in NPE being thrown but the method itself claims to return false "in all other cases".This now uses null safe
Objects.equals(..)to account for that and ensure the API claim.Fix eclipse-platform/eclipse.platform.ui#3604