Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is possible to use "import static" like java 5/6?

148 views
Skip to first unread message

manoelhc

unread,
Jun 10, 2010, 11:19:38 AM6/10/10
to
Is possible to use "import static" like java 5/6 on Rhino or do
something like?

Cameron McCormack

unread,
Jun 10, 2010, 8:34:18 PM6/10/10
to
On 11/06/2010 1:19 AM, manoelhc wrote:
> Is possible to use "import static" like java 5/6 on Rhino or do
> something like?

No, but you could hack something like it. For simple constants, just do:

const EXIT_ON_CLOSE =
Packages.javax.swing.WindowConstants.EXIT_ON_CLOSE;

in the scope that you want it. If it's a static variable, you might
want to define a getter so that you can always get the current value of
it. I don't think you can get access to the current activation object
to make a local variable be a property with a getter. But if you're
happy with it on the global object, you can do:

__defineGetter_
("out", function() { return Packages.java.lang.System.out });

(I don't know that System.out would ever change value, but you get the
idea.)

For static methods, then again just assigning the value to a variable
should work:

const exit = Packages.java.lang.System.exit;


So if you're happy with these all being defined on the global object
(like Rhino's importClass and importPackage do), then it shouldn't be
hard to write an importStatic function yourself.

0 new messages