Bug: Sharing objects across browser windows. ClassCastException. JSNI.

46 views
Skip to first unread message

pmbh

unread,
Dec 4, 2006, 11:04:58 AM12/4/06
to Google Web Toolkit
GWT just allows to share Java objects across browser windows if the
Java object is used with native JavaScript (JSNI).

If the same is tried using Java, GWT throws a ClassCastException.
Although you use exactly the same method call with JavaScript native
code and Java code. The Java method call throws an exception but the
JavaScript native method call doesn't.

Example:

I have a Java object called "ClientSession". The module loaded in the
main window has the following code:

public class Main implements EntryPoint {

private static ClientSession clientSession = new ClientSession();
...
...

public Main() { }

public void onModuleLoad() {
exportClientSession();
...
(more code)
...
...

}

// JSNI to export clientSession object in "window.top".
private static native void exportClientSession() /*-{
$wnd.top.clientSession =
@GWTApplication.client.Main::clientSession;
}-*/;
}

The Main module has a button that opens the Second module in a new
browser window.
This second module has this code:

public class Second implements EntryPoint {

public Second() { }

public void onModuleLoad() {
// Make a new button that does something when you click it.
Button b = new Button("Show Main ID!", new ClickListener() {
public void onClick(Widget sender) {

showSession(); //
Don't throw ClassCastException.

ClientSession session = getSession(); // Throw
ClassCastException.
Window.alert(session.getId());

} });
RootPanel.get().add(b);
}

// This function works
private native void showSession() /*-{
var session = $wnd.opener.top.clientSession;
alert(session.@GWTApplication.client.ClientSession::getId()());
}-*/;

// This function throws an exception
private native ClientSession getSession() /*-{
var session = $wnd.opener.top.clientSession;
return session;
}-*/;
}

It's a nonsense.

I use native sintax to call a java method using JavaScript (JSNI):

session.@GWTApplication.client.ClientSession::getId()()

And it works, but if I try the same method call from Java it doesn't
work:

ClientSession session = getSession(); // throw ClassCastException
Window.alert(session.getId());

Is this a GWT bug?

mP

unread,
Dec 4, 2006, 3:06:08 PM12/4/06
to Google Web Toolkit
This prolly fails because instances (of a class with the same name)
from the two windows have different typeId's. Rather than storing the
className with each object a typeId is used. This value is used by the
runtime to figure out type stuff like instanceof tests, casting and so
on.

> alert(sessi...@GWTApplication.client.ClientSession::getId()());


> }-*/;
>
> // This function throws an exception
> private native ClientSession getSession() /*-{
> var session = $wnd.opener.top.clientSession;
> return session;
> }-*/;
>
> }It's a nonsense.
>
> I use native sintax to call a java method using JavaScript (JSNI):
>

> sessi...@GWTApplication.client.ClientSession::getId()()

pmbh

unread,
Dec 5, 2006, 3:43:04 AM12/5/06
to Google Web Toolkit
OK, thank you for your answer.

But how I implements communication across browser windows? Even the
open method of the GWT Window object doesn't return anything.

I tried to open a new window with "$wnd.open" in a JSNI native method
to get a reference to the new window and then pass my object reference
to this new window, but I get the same exception.

I've been looking for how implement communication across windows, I
didn't find anything but use JavaScript, and then GWT loses part of its
usefulness.

And I wonder why I can use the same instance using JSNI sintax to
access Java Object, but I cannot use Java code.

Is there anything to communicate windows and still using GWT? I have an
AJAX application that I want convert in a GWT application, but it's
essential windows communication because I implement a "browser session
pattern" shared across all windows, what is an important advantage of
AJAX.

Is there any way to use the same typeId for the same class in two
diferents windows?

Thank you.

mP ha escrito:

pmbh

unread,
Dec 5, 2006, 4:23:00 AM12/5/06
to Google Web Toolkit
I forgot something. I tried the same windows communication using JRE
Emulation Library objects (Vector, HashMap, etc) and it works well, the
exception is only thrown with custom classes.

What have JRE classes got to work well across windows? What must my
classes have to work well? If anyone has a solution, please tell me.

pmbh

unread,
Dec 15, 2006, 7:25:51 AM12/15/06
to Google Web Toolkit
I don't know if there is someone interested in this pattern to share
Java Objects between browser windows.

But we finally found out that it works well in Web Mode and fails just
in Hosted Mode.

Therefore it isn't a problem about the GWT Compiler. It's just a
problem about the embedded browser (GWTShell).

Although it's work well in Web Mode, it's difficult to test the code if
we can't use the Hosted Mode.

I hope someone find out how to patch the GWTShell and then allow us to
do in Hosted Mode what we can do in Web Mode.

Thanks

Dan Morrill

unread,
Dec 15, 2006, 11:19:53 AM12/15/06
to Google-We...@googlegroups.com
Hello, pmbh!

A feature that works in web mode but not hosted mode is probably a bug.  If you'd like to enter it, you can do so here:
http://code.google.com/p/google-web-toolkit/issues/list

Thanks!

- Dan Morrill

timmys

unread,
Dec 20, 2006, 4:06:17 AM12/20/06
to Google Web Toolkit
Hi!

Was this issue ever posted on the issues list?
I'm having exactly the same problem now...

pmbh

unread,
Jan 9, 2007, 7:28:52 AM1/9/07
to Google Web Toolkit
Hi!

I'm sorry but I've been absent a long time and I couldn't pursue this
subject.

I don't know if someone has already posted it on the inssues list so
I'll post it today.

Thank you

Reply all
Reply to author
Forward
0 new messages