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 

-----Original Message-----
From:
[EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
lla.org] On Behalf Of Charles Lowell
Sent: Tuesday, July 22, 2008 9:00 AM
To: [email protected]
Subject: Re: NativeJavaObject limits.

On Jul 22, 8:34 am, "Ruland Kevin-BHP637" <[EMAIL PROTECTED]>
wrote:
> 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

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.
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to