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

Rhino Question

0 views
Skip to first unread message

Waldemar Horwat

unread,
May 22, 2000, 3:00:00 AM5/22/00
to mozill...@mozilla.org
Can anyone help this guy?

Waldemar

>From: "Marshall Cline" <cl...@parashift.com>
>To: <wald...@netscape.com>
>Subject: LiveConnect
>Date: Mon, 22 May 2000 02:48:07 -0500
>X-Priority: 3 (Normal)
>Importance: Normal
>
>Hi,
>
>I'm sure you get a lot of the "please help" emails, and believe me, I know
>what it's like (I'm the original author and maintainer of the C++ FAQ; wrote
>it in 1990/91 and am still maintaining it to this day; everybody and his
>brother wants me to debug their C++ code -- and also do their homework!! :-(
>
>But despite the ubiquity of this request, "please help" :-)
>
>I'm trying to use Rhino to script some Java, and am having trouble with the
>interface between Java and JavaScript. I assume I should be using
>LiveConnect, but I can't seem to get the big picture of how to start.
>
>Here's what I'm trying to accomplish: I'm using various Java classes, some
>provided by jdk1.2 (e.g., java.util.ArrayList) and some we created
>ourselves. I'm implementing Scriptable (it didn't make sense to extend
>ScriptableObject), so my 'get()' and 'put()' methods act as adapters between
>my world and the JS world. So when JS calls my Scriptable's 'get()', do I
>wrap my Java object somehow? Or does LiveConnect do this?
>
>As an example, suppose my 'get()' has to return a List. I've figured out
>how to return the List by *value* (create a JS Array and populate it with
>the List's contents), but then any changes made by the JavaScript would not
>be reflected back in the Java world -- bummer. So do I wrap the List? If
>so, do I create another Scriptable class for List? Same question for Date
>and all the others.
>
>Is LiveConnect the answer? If so, how do I start? E.g., is there some
>explicit pile of code I need to download first? Is there something explicit
>I need to do to communicate with it?
>
>Thanks for any help you can give,
>Marshall

Patrick Beard

unread,
May 26, 2000, 3:00:00 AM5/26/00
to
In article <p04310100b54f46910286@[208.12.38.198]>, wald...@netscape.com
(Waldemar Horwat) wrote:

> >Here's what I'm trying to accomplish: I'm using various Java classes, some
> >provided by jdk1.2 (e.g., java.util.ArrayList) and some we created
> >ourselves. I'm implementing Scriptable (it didn't make sense to extend
> >ScriptableObject), so my 'get()' and 'put()' methods act as adapters
> >between
> >my world and the JS world. So when JS calls my Scriptable's 'get()', do I
> >wrap my Java object somehow? Or does LiveConnect do this?
> >
> >As an example, suppose my 'get()' has to return a List. I've figured out
> >how to return the List by *value* (create a JS Array and populate it with
> >the List's contents), but then any changes made by the JavaScript would
> >not
> >be reflected back in the Java world -- bummer. So do I wrap the List? If
> >so, do I create another Scriptable class for List? Same question for Date
> >and all the others.
> >
> >Is LiveConnect the answer? If so, how do I start? E.g., is there some
> >explicit pile of code I need to download first? Is there something
> >explicit
> >I need to do to communicate with it?

If you're using Rhino, then you have direct access to your Java classes from
within the JavaScript itself. If you want to use an ArrayList, you could write:

js>var al = new java.util.ArrayList();
js>for (m in al) print(m)
iterator
isEmpty
hashCode
ensureCapacity
remove
clone
subList
listIterator
size
indexOf
set
clear
wait
lastIndexOf
containsAll
empty
removeAll
class
notify
toString
contains
trimToSize
addAll
add
notifyAll
toArray
retainAll
get
getClass
equals

(I used the one from the 1.1 compatible collections.jar file since I use Rhino
with MRJ 2.2).

js>al.add(1)
[1.0]
js> al.get(0)
1.0
js> al.add(2)
true
js> al
[1.0, 2.0]

You get the idea. It just works. If your Java class requires that an object must
implement a particular interface, we have a solution for that too. Let's say you
had some interface

package org.foo.util;

public interface Command {
public void do();
}

If you want to implement this interface in JavaScript, you'd write:

var c = new Packages.org.foo.util.Command() {
do : function() { print('just do it.'); }
};

The resulting object c, is for all intents and purposes, an implementation of
the Command interface, but its guts are written in JavaScript.

- Patrick

--
// Patrick C. Beard
// Netscape Communications

0 new messages