Skip to main content

create objects in applet codebase

10 replies [Last post]
tkram01
Offline
Joined: 2005-12-14
Points: 0

Is it possible to call a static method in the applet codebase to create an object to pass to an applet method? Using liveconnect Packages.com.... doesn't seem to see the applet classes (only standard java packages).

So if I have a class

MyClass {
public static String getString();
}

in the codebase of an applet (but MyClass is not an applet itself) can I call getString() without providing a proxy method on the applet?

Thanks,

Trevor

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kbr
Offline
Joined: 2003-06-16
Points: 0

No. The reason is as follows. Each JavaScript -> Java call needs to be conceptually scoped within a particular applet's context. (You might have multiple applets on the same page.) A static Java method invocation from JavaScript, even in the next-generation Java Plug-In, is basically scoped within an arbitrarily chosen applet on the web page, or in the case of the Firefox browser, within a dummy applet on the web page not associated with any particular codebase.

We could add a feature to the next-generation Java Plug-In to support this. For example, if you had an applet whose ID was "myApplet", I think we could support the syntax "myApplet.Packages.com.foo.Bar.baz()". Would you like me to file an RFE about this? Do you have a particular motivating use case in mind?

tkram01
Offline
Joined: 2005-12-14
Points: 0

The syntax "myApplet.Packages.com.foo.Bar.baz()" would work for us. I would appreciate an RFE being filed for this. Our use case is being able to pass Enums and complex objects (either created with new or through static factory methods) to the Applet methods to provide an API for javascript calls. Currently we are forced to provide wrapper generator classes through calls on the applet to create these objects which complicates the API.

Thanks,

Trevor

kbr
Offline
Joined: 2003-06-16
Points: 0

This has been filed as RFE 6648488, which should show up in the Sun Bug Database in a day or so.

kbr
Offline
Joined: 2003-06-16
Points: 0

This fix will be present in the new Java Plug-In in the forthcoming 6u10 build 11, due out roughly at the beginning of February.

The new syntax supported will be:

applet1.Packages.com.company1.HelperClass.staticField
applet1.Packages.com.company1.HelperClass.staticMethod()
new applet1.Packages.com.company1.HelperClass()

in both IE and Firefox 3 (it goes without saying -- basically any browser supporting the new Java Plug-In), where "applet1" is the ID of the applet on the page. Please try the new functionality when it's available and let us know how it works for you. Thanks for the suggestion to incorporate this.

tkram01
Offline
Joined: 2005-12-14
Points: 0

wow - thanks! this is fantastic.

Trevor

tkram01
Offline
Joined: 2005-12-14
Points: 0

I tried this out with build 12 and I get a

Error: uncaught exception: Error calling method on NPObject! [plugin exception: java.lang.reflect.InvocationTargetException].

when I try and pass an object returned with a Applet.Packages.staticMethod call to non-static method in the applet.

Trevor

kbr
Offline
Joined: 2003-06-16
Points: 0

Please provide a test case. You can email me at kenneth dot russell at sun dot com.

tkram01
Offline
Joined: 2005-12-14
Points: 0

I just emailed you the test case.

tkram01
Offline
Joined: 2005-12-14
Points: 0

Is there a trick to get this working with Enums? I get a NPObject Ambiguous Member class instead of the enum object when addressing it

Message was edited by: tkram01

tkram01
Offline
Joined: 2005-12-14
Points: 0

To answer my own question - my enum value was Equals - which must be a reserved word as when I changed the name it worked.