nashorn replace Java.type with binding -


to invoke java js can use java.type. there way bind java class in bindings?

so replace:

scriptengine.eval("java.type('my.own.awesomeobj')"); 

with like:

bindings bindings = new simplebindings(); bindings.put("awesomeobj", my.own.awesomeobj.class); scriptengine.setbindings(bingings, scriptcontext.global_scope); 

i working on framework want make lot of classes available js scripts, , preferably not use string concatenation , eval. throws exception message: awesomeobj not function, makes sense.

nashorn distinguishes type java.lang.class instance, java (in java language, my.own.awesomeobj type, while my.own.awesomeobj.class instance of java.lang.class. can use type access static members, or constructor. can't use class object purpose. bad news is, can't directly obtain object nashorn uses representing types; it's instance of jdk.internal.dynalink.beans.staticclass , lives in restricted package. however, can evaluate in script with

engine.eval("java.type('my.own.awesomeobj')"); 

and put result of in bindings. incidentally, within nashorn, if put class object bindings under name awesomeobjclass, can use synthetic property .static obtain type, e.g.:

var awesomeobj = awesomeobjclass.static; 

in sense, .static on class object dual of .class on type object (.static doesn't exist in java, types aren't reified runtime objects).

var stringtype = java.type("java.lang.string"); var stringclass = stringtype.class print(stringclass instanceof java.lang.class); // true print(stringtype === stringclass.static); // true 

hope helps.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -