When creating object from static method: Cannot find default value for object

43 views
Skip to first unread message

Matt Bateman

unread,
May 1, 2015, 7:58:09 PM5/1/15
to mozill...@googlegroups.com
I'm using Rhino 1.7.6 with JDK 8. I want to create an object in javascript using a static method on another object. When I try to call any functions on the created object, I get "Cannot find default value for object".

The javascript:

var user = UserService.getUser(123);
java.lang.System.out.println(user);
java.lang.System.out.println(user.getEmail());

Calling "println" with the "user" object correctly calls the "toString" method on the object. Calling "getEmail" throws the above mentioned error.

If I do this:

var user = new User();
java
.lang.System.out.println(user.getEmail());

it works just fine.

So it seems to me that I'm not doing something right when creating the "user" object via the static method on UserService. Any suggestions?

The bootstrap code:

String source = "sdi.js";

Context ctx = Context.enter();
try (Reader in = new FileReader(source)) {
    ctx.setLanguageVersion(Context.VERSION_1_8);
    Scriptable scope = ctx.initStandardObjects();
    ScriptableObject.defineClass(scope, UserService.class);
    ScriptableObject.defineClass(scope, User.class);
    Object ob = ctx.evaluateReader(scope, in, source, 1, null);
}
finally {
    Context.exit();
}

The User code:

public class User extends ScriptableObject {

    private int mId;
    private String mEmail;
    private String mUsername;

    @JSConstructor
    public User() {}

    @JSConstructor
    public User(int id, String email, String username) {
        mId = id;
        mEmail = email;
        mUsername = username;
    }

    @Override
    public String getClassName() {
        return "User";
    }

    @JSFunction
    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }

    @JSFunction
    public String getEmail() {
        return mEmail;
    }

    public void setEmail(String email) {
        mEmail = email;
    }

    @JSFunction
    public String getUsername() {
        return mUsername;
    }

    public void setUsername(String name) {
        mUsername = name;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append("[ id = '").append(mId).append("', email = '").append(mEmail).append("', user = '").append(mUsername).append("' ]");
        return buf.toString();
    }

}

The "UserService" code:

public class UserService extends ScriptableObject {

    @JSConstructor
    public UserService() {}

    @Override
    public String getClassName() {
        return "UserService";
    }

    @JSStaticFunction
    public static User getUser(int id) {
        return new User(id, "f...@bar.com", "matt");
    }

}


Evgeny Shepelyuk

unread,
May 2, 2015, 2:05:21 AM5/2/15
to mozill...@googlegroups.com
You should create new instance using Context.newObject.
See http://www-archive.mozilla.org/rhino/apidocs/org/mozilla/javascript/Context.html#newObject(org.mozilla.javascript.Scriptable, java.lang.String, java.lang.Object[])

Matt Bateman

unread,
May 4, 2015, 6:44:40 PM5/4/15
to mozill...@googlegroups.com
It took some time for me to figure out what you suggested but I did and it works. For anyone else that finds this post the solution was to create the new object using the Context instance inside the java code. I changed the "UserService" code to the below and it worked nicely.

Scriptable s = mCtx.newObject(mScope, "User");
User user = (User) s;
user.setId(id);
user.setEmail("f...@bar.com");
user.setUsername("matt");
return user;

Evgeny Shepelyuk

unread,
May 5, 2015, 2:52:39 AM5/5/15
to mozill...@googlegroups.com
In fact, I believe you could achieve the same by no implementing Rhino Host objects, i.e. without extending Scriptable and annotating method.
But instead try to create simple java classes and use them directly in your script.

вівторок, 5 травня 2015 р. 01:44:40 UTC+3 користувач Matt Bateman написав:
Reply all
Reply to author
Forward
0 new messages