Skip to content

feat: add getUI() to ComponentEvent#24205

Merged
mcollovati merged 2 commits intomainfrom
issues/18818-event_ui_getter
May 5, 2026
Merged

feat: add getUI() to ComponentEvent#24205
mcollovati merged 2 commits intomainfrom
issues/18818-event_ui_getter

Conversation

@mcollovati
Copy link
Copy Markdown
Collaborator

ComponentEvent exposes the source component but offers no direct way to reach its UI. Listeners that need the UI must dereference an Optional via event.getSource().getUI().ifPresent(...), which is verbose and silently swallows the case where the source is detached.

Add ComponentEvent.getUI() returning UI directly. When the source is itself a UI, it is returned as-is; otherwise the method returns the resolved UI or throws IllegalStateException with a message pointing to getSource().getUI() for code that must handle a detached source. This matches the convention already used by AbstractAttachDetachEvent, UIInitEvent, BeforeEvent and similar event types.

Fixes #18818

`ComponentEvent` exposes the source component but offers no direct way
to reach its UI. Listeners that need the UI must dereference an
`Optional` via `event.getSource().getUI().ifPresent(...)`, which is
verbose and silently swallows the case where the source is detached.

Add `ComponentEvent.getUI()` returning `UI` directly. When the source
is itself a `UI`, it is returned as-is; otherwise the method returns
the resolved UI or throws `IllegalStateException` with a message
pointing to `getSource().getUI()` for code that must handle a
detached source. This matches the convention already used by
`AbstractAttachDetachEvent`, `UIInitEvent`, `BeforeEvent` and similar
event types.

Fixes #18818
@mcollovati
Copy link
Copy Markdown
Collaborator Author

mcollovati commented Apr 27, 2026

Adding public UI getUI() to ComponentEvent is binary-compatible: already-compiled code (including third-party add-ons) keeps working.

It is source-incompatible in one narrow case: a subclass of ComponentEvent that declares its own getUI() with a return type that is not assignable to UI. In practice this means a user subclass with public Optional<UI> getUI() will fail to recompile against this release.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 27, 2026

Test Results

 1 395 files  +1   1 395 suites  +1   1h 17m 49s ⏱️ +36s
10 068 tests +3   9 998 ✅ +3  70 💤 ±0  0 ❌ ±0 
10 543 runs  +3  10 464 ✅ +3  79 💤 ±0  0 ❌ ±0 

Results for commit f15fc85. ± Comparison against base commit 8450563.

♻️ This comment has been updated with latest results.

@mcollovati mcollovati marked this pull request as ready for review April 29, 2026 14:24
@sonarqubecloud
Copy link
Copy Markdown

@mcollovati mcollovati added this pull request to the merge queue May 5, 2026
Merged via the queue into main with commit 569612b May 5, 2026
31 checks passed
@mcollovati mcollovati deleted the issues/18818-event_ui_getter branch May 5, 2026 06:48
@mstahv
Copy link
Copy Markdown
Member

mstahv commented May 5, 2026

I wonder if the implementation is now a similar trap as the Component.getUI currently is? So if I start some call sequence that completes in a separate thread, calling getUI may be unsafe again IIRC🤔 Not 100% sure, but this is the reason why I didn't proceed with a PR. My guess originally was that UI instance should be saved when creating an event instance.

@mcollovati
Copy link
Copy Markdown
Collaborator Author

Saving the UI instance in the event is the best approach, indeed. But this needs a refactoring of all code that creates event instances, since there's not a single point in the framework where you can do it.
But I guess we can safely add a new constructor and then start updating the existing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComponentEvent should expose UI

3 participants