Home > Enterprise >  Is there a way to cast the vaadin UI class to some class inheriting from it?
Is there a way to cast the vaadin UI class to some class inheriting from it?

Time:07-14

My problem is that when I call (SsoUI) UI.getCurrent(), where SsoUI inherits from UI, I get a ClassCastException at runtime saying that UI cannot be cast to SsoUI.

Just to give some context, we implemented SSO between a suite of applications in Vaadin 7 using Hazelcast, and the Hazelcast instance reference is accessed through this class SsoUI. So at some point in our code there is this line:

HazelcastInstance hi = SsoUI.getCurrentSsoUI().getHazelcastInstance();

where

public static SsoUI getSsoUI() {
    return (SsoUI) UI.getCurrent();
}

and everything works fine.

So now we are developing new applications on vaadin 14 and I tried to replicate the same structure to handle single sign on, but now I get the error I wrote at the beginning, a ClassCastException.

Am I missing something or are there differences between UI in vaadin 7 and UI in vaadin 14 which render this approach unusable?

Edit: to clarify, SSO stands for Single Sign On and SsoUI is my custom class which extends UI.

CodePudding user response:

Am I missing something or are there differences between UI in vaadin 7 and UI in vaadin 14 which render this approach unusable?

Yes, there is a difference. You're not expected to extend the UI class in Vaadin 10 or later.

To quote Leif Åstrand of Vaadin Ltd.:

Using a custom UI class has been deprecated but still functioning since Vaadin 10. We would like to encourage application developers to move away from that pattern so that we could eventually simplify the internal implementation. …

ComponentUtil

If you need to store some object so that it's accessible where the current UI object is available, you can use the ComponentUtil class to map values to the current UI instance.

  • Related