-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSObjectPropertiesMap - proper usage #17
Comments
It seems that I have mistakenly made The intent was to enable a convenient method of setting JSValue _pageContext = jsContext.property("pageContext");
Map<String, JSValue> map = new JSObjectPropertiesMap<>(_pageContext.toObject(), JSValue.class); I recommend using But because the class is not public, this won't work. If you really want to use it, there are two choices: (1) add Class<?> cl = Class.forName("org.liquidplayer.javascript.JSObjectPropertiesMap");
Constructor<?> cons = cl.getConstructor(JSObject.class, Class.class);
Object inst = cons.newInstance(_pageContext.toObject(), JSValue.class);
Map<String, JSValue> map = (Map<String, JSValue>) inst; You'll also need to wrap this in a As an aside, I had intended to just allow I will mark this as a bug and fix. Thanks for reporting. |
Fixed in 8cbe527e66dd7c57acc23f02c44be3d7f9bbdaab. |
Sounds good, for now I will mark class public and run a build. Appreciate it.
…Sent from my iPhone
On Mar 15, 2017, at 4:24 AM, Eric Lange ***@***.***> wrote:
It seems that I have mistakenly made JSObjectPropertiesMap package local. It should be public. I will fix that for the next release. Unfortunately, it will be difficult to use without reflections unless it is public.
The intent was to enable a convenient method of setting JSObject properties using the Map interface. So the correct way to use it should be:
JSValue _pageContext = jsContext.property("pageContext");
Map<String, JSValue> map = new JSObjectPropertiesMap<>(_pageContext.toObject(), JSValue.class);
I recommend using JSValue as a generic type rather than Object because not all objects can be translated into Javascript values. Or, if your object uses only one type, you can specify that type, as in: Map<String, Integer> map = new JSObjectPropertiessMap<>(_pageContext, Integer.class);
But because the class is not public, this won't work. If you really want to use it, there are two choices: (1) add public in front of the class in JSObjectPropertiesMap.java and build the library yourself, or (2) use reflections:
Class<?> cl = Class.forName("org.liquidplayer.javascript.JSObjectPropertiesMap");
Constructor<?> cons = cl.getConstructor(JSObject.class, Class.class);
Object inst = cons.newInstance(_pageContext.toObject(), JSValue.class);
Map<String, JSValue> map = (Map<String, JSValue>) inst;
You'll also need to wrap this in a try/catch block because there are several exceptions that can theoretically occur, although if your parameters are right, it should work fine.
As an aside, I had intended to just allow JSObject to always implement the Map inteface, however, in Java, I learned, an object cannot implement both Map and List!! This is because they share a single method, remove, which has different return types in each implementation! I wanted JSArray (which is also a JSObject) be able to be used as a generic List. And so I couldn't do both. So to still make this possible, JSObjectPropertiesMap is just a shadow object for some JSObject. Just a little trivia for you.
I will mark this as a bug and fix. Thanks for reporting.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
This is fixed in v0.2.1. |
It looks like this is an inner wrapper class to assist with maps. If I set a map in context as follows:
Is this the proper way to then reference that map in the context (after running evaluate script and associated call function):
thx, appreciate the assist.
The text was updated successfully, but these errors were encountered: