Hi,
I had similar problem, and tried to solve it by using the method
Context.evaluateScript(javascript_source) which in some cases returns
a NativeObject, which is a ScriptableObject ( not
NativeJavaObject ).In the example below - I define a prototype of
JavaScript object with a given name ( like "ExampleJSClass") and a
constructor with a String argument (type of: new ExampleJSClass
(String) ), and in the next line I add a function "toString()" to the
prototype - which displays all enties "key=property" of any instance
of ExampleJSClass. I am not sure if you can add wrapped Java objects
and methods as a property of the ExampleJSClass instance, but who
knows ?
Here is the code:
///////////////////////////
public static void main(String[] args) {
Context cx = Context.enter();
try {
ScriptableObject scope = cx.initStandardObjects();
String source = prototypeSource("ExampleJSClass");
String sourceName = "source of ExampleJSClass";
cx.evaluateString(scope, source, sourceName, 1, null);
NativeObject prototype = (NativeObject)cx.evaluateString
(scope,
"ExampleJSClass.prototype;", sourceName, 1, null);
// perhaps we can add the function "toString()" to the
above prototype,
// but i do not know how to do it , so I am using
Javascript :
String functionSource = toStringSource("ExampleJSClass");
cx.evaluateString(scope, functionSource, sourceName, 1,
null);
// finally:
NativeObject myObj = (NativeObject) cx.evaluateString
(scope, "new ExampleJSClass(\"Program_Name\")", sourceName, 1, null);
ScriptableObject.putProperty(scope, "myObj", myObj);
String returnStr = (String) cx.evaluateString(
scope, "myObj.toString()", sourceName, 1, null);
System.out.println(returnStr);
} catch (Exception ex) { ex.printStackTrace();}
finally { Context.exit(); } }
static String prototypeSource(String className) {
String ProgID = "ProgID";
StringBuffer buf = new StringBuffer();
char ln = '\n';
buf.append("function ").append(className).append('(').append
(ProgID).append("){");
buf.append(ln).append("this[\"").append(ProgID).append("\"]
=").append(ProgID).append(';');
buf.append(ln).append("var className=\"").append
(className).append("\";}");
return buf.toString();
}
static String toStringSource(String className) {
StringBuffer buf = new StringBuffer();
char ln = '\n';
buf.append(className).append(".prototype.toString = function()
{");
buf.append(ln).append("str =\"").append(" \";");
buf.append(ln).append("for(var prop in this ){");
buf.append(ln).append("var valu = this[prop];");
buf.append(ln).append("if(valu instanceof Function){");
buf.append(ln).append("}else{");//display only properties,
which are not functions
buf.append(ln).append("str+=prop+\"=\"+valu+\";\\n\"");
buf.append(ln).append("}}");
buf.append(ln).append("return str;");
buf.append(ln).append("};");
return buf.toString();
}
//////////////////////////
On 26 Lut, 00:31, "David Parks" <[email protected]> wrote:
> I spoke a bit soon.
> I'm having some trouble defining properties on an object that I added using
> javaToJS.
>
> The code shown below works now. But I also want to add some properties to my
> object that make calls to getter and setter methods of the java object (e.g.
> the same functionality as with the jsGet_XXX and jsSet_XXX methods when
> using defineClass).
>
> I see that ScriptableObject has some defineProperties() methods that appear
> that they do what I want. However, the object I get back from
> Context.javaToJS() is of type NativeJavaObject, which implements Scriptable,
> but does not extend ScriptableObject, so I don't see where to get a
> reference to make use of these methods.
>
> How do I handle a case like this?
>
> Working code:
> ---------------
> ExampleJSClass myObj = new ExampleJSClass();
> Object wrappedOut = Context.javaToJS(myObj, instanceScope);
> ScriptableObject.putProperty(instanceScope, "myObj", wrappedOut);
> cx.evaluateString(instanceScope, "myObj.function1();", "MyTest", 1,
> null);
>
> Thanks!
> David
>
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino