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

NativeJavaObject limits.

430 views
Skip to first unread message

Ruland Kevin-BHP637

unread,
Jul 22, 2008, 8:34:52 AM7/22/08
to dev-tech-js-...@lists.mozilla.org
Hi all,

I would like to have NativeJavaObject restrict the methods that it
expses to JavaScript. In particular, I would like to not expose the
java.lang.Object methods which are associated with every object. Also,
I would like the methods to not be enumerable.

Short of rewriting JavaMembers is there any alternative?

Thanks

Kevin Ruland

Charles Lowell

unread,
Jul 22, 2008, 9:59:55 AM7/22/08
to
On Jul 22, 8:34 am, "Ruland Kevin-BHP637" <KRul...@Motorola.com>
wrote:

Kevin,

In my quest to solve this problem (http://groups.google.com/group/
mozilla.dev.tech.js-engine.rhino/browse_thread/thread/
9131211041437639#), I came up with one solution which was to implement
my own wrap factory which wrapped native java objects with scriptables
having the standard NativeJavaObject as its prototype. This allows you
to add and remove properties from java objects, while keeping the
delegation to the native methods intact.


import org.mozilla.javascript.*;


public class ScriptableNativeJavaObject extends ScriptableObject {

NativeJavaObject prototype;


public ScriptableNativeJavaObject(Scriptable scope, Object
javaObject, Class staticType) {
super(scope, new NativeJavaObject(scope, javaObject, staticType));
this.prototype = (NativeJavaObject) this.getPrototype();
}


public String getClassName() {
return prototype.unwrap().getClass().getName();
}

public static class ScriptableNativeContextFactory extends
ContextFactory {
protected Context makeContext() {
Context cx = super.makeContext();
cx.setWrapFactory(new ScriptableNativeWrapFactory());
return cx;
}
}

public static class ScriptableNativeWrapFactory extends WrapFactory {
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
Object javaObject, Class staticType) {
return new ScriptableNativeJavaObject(scope, javaObject,
staticType);
}
}


public static void main(String[] args) {
new ScriptableNativeContextFactory().call(new ContextAction() {
public Object run(Context cx) {
return cx.evaluateString(cx.initStandardObjects(),
"var o = new java.lang.Object(); " +
"o.name = 'bar'; " +
"java.lang.System.out.println(o.name + ': ' + o.hashCode())",
"str", 1, null);
}
});
}
}


prints:

bar: 1579795854

so you can see that the java.lang.Object instance has both hashCode()
as well as the dynamic property 'name'

There's no reason you couldn't do the same technique but instead of
delegating to NativeJavaObject, just return instances of an extended
NativeJavaObject to override the get() method to specifically exclude
the methods you want to hide.

Not sure if this is ideal for you, but it's a thought.

Ruland Kevin-BHP637

unread,
Jul 22, 2008, 10:34:17 AM7/22/08
to Charles Lowell, dev-tech-js-...@lists.mozilla.org
Charles,

This is a great thought. I still have the benefit of the Rhino supplied
reflection cache. Of course it would be more efficient to simply remove
the unwanted items from the JavaMemebers.members.

In order to prevent enumeration of the functions, would overriding
getIds() be adequate?

Kevin

Kevin,


import org.mozilla.javascript.*;

NativeJavaObject prototype;


prints:

bar: 1579795854

_______________________________________________
dev-tech-js-engine-rhino mailing list
dev-tech-js-...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

0 new messages