Subj : Re: Rhino Cannot find a Property Defined in a Java Class To : Ben From : Igor Bukanov Date : Thu Feb 26 2004 08:37 am Ben wrote: > Can someone tell me what is wrong with the following code? For some > reason the property IsSampleCompany cannot be found. What am I doing > wrong? Thanks for anyones help. 2 issues here: 1. You did not call ScriptableObject.defineClass for your QBPreferences object. When you extend from ScriptableObject, ScriptableObject.defineClass(scope, SomeClass) creates a JavaScript constructor in the scope with the name SomeClass.getClassName() that contains prototype object with special wrappers for your Java getters, setters and functions. 2. In the Create method you returned new QBPreferences() which does not give a chance for Rhino to initialize properly parent scope and prototype chain for your object. When script calls prefsObj.IsSampleCompany, Rhino will look for IsSampleCompany in your object, will not see the property there, then look IsSampleCompany in the prototype. If it is properly initialized, then Rhino will see the getter for IsSampleCompany there and then call the getter on the original object. You should replace: > public Object jsFunction_Create(String object) { > return new QBPreferences(); > } with at least public Object jsFunction_Create(String object) { return Context.toObject("QBPreferences", getParentScope()); } or if you need more control for creation of QBPreferences instances: public Object jsFunction_Create(String object) { QBPreferences p = new QBPreferences(); Scriptable scope = getParentScope(); p.setParentScope(scope); p.setPrototype(ScriptableObject.getClassPrototype(scope, "QBPreferences")); return p; } Regards, Igor > > The Java Script: > ---------------- > something > > > > > > The Java Code (ActiveXObject.java): > ----------------------------------- > public class ActiveXObject extends ScriptableObject { > > protected String object; > > public ActiveXObject() { > } > > public ActiveXObject( String object ) { > this.object = object; > } > > public static Scriptable jsConstructor(Context cx, Object[] args, > Function ctorObj, boolean inNewExpr) { > return new ActiveXObject( (String)args[0] ); > } > > public Object jsFunction_Create(String object) { > return new QBPreferences(); > } > > public String getClassName() { > return "ActiveXObject"; > } > } > > The Java Code (QBPreferences.java): > ----------------------------------- > public class QBPreferences extends ScriptableObject { > > public QBPreferences() { > } > > public void jsConstructor() { > } > > public boolean jsGet_IsSampleCompany() { // This method is not > found > return true; > } > > public String getClassName() { > return "QBPreferences"; > } > } > > The Java Code > ------------- > // In the main function I call this > ScriptableObject.defineClass( scope, ActiveXObject.class ); > > > ------------------------------------------------------------------------------- > > Any ideas why the method jsGet_IsSampleCompany() is not being called? > I debugged and found that the get method in QBPreferences parent > (ScriptableObject) returns NOT_FOUND. Any help is greatly appreciated. > > Thanks, > Ben .