Java Code Examples for jdk.nashorn.api.scripting.JSObject#getMember()

The following examples show how to use jdk.nashorn.api.scripting.JSObject#getMember() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestPrintJSON.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {

        Path recordingFile = ExecuteHelper.createProfilingRecording().toAbsolutePath();

        OutputAnalyzer output = ExecuteHelper.run("print", "--json", recordingFile.toString());
        String json = output.getStdout();

        // Parse JSON using Nashorn
        String statement = "var jsonObject = " + json;
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("nashorn");
        engine.eval(statement);
        JSObject o = (JSObject) engine.get("jsonObject");
        JSObject recording = (JSObject) o.getMember("recording");
        JSObject events = (JSObject) recording.getMember("events");

        // Verify events are equal
        try (RecordingFile rf = new RecordingFile(recordingFile)) {
            for (Object jsonEvent : events.values()) {
                RecordedEvent recordedEvent = rf.readEvent();
                double typeId = recordedEvent.getEventType().getId();
                String startTime = recordedEvent.getStartTime().toString();
                String duration = recordedEvent.getDuration().toString();
                Asserts.assertEquals(typeId, ((Number) ((JSObject) jsonEvent).getMember("typeId")).doubleValue());
                Asserts.assertEquals(startTime, ((JSObject) jsonEvent).getMember("startTime"));
                Asserts.assertEquals(duration, ((JSObject) jsonEvent).getMember("duration"));
                assertEquals(jsonEvent, recordedEvent);
            }
            Asserts.assertFalse(rf.hasMoreEvents(), "Incorrect number of events");
        }
    }
 
Example 2
Source File: TestPrintJSON.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws Throwable {

        Path recordingFile = ExecuteHelper.createProfilingRecording().toAbsolutePath();

        OutputAnalyzer output = ExecuteHelper.jfr("print", "--json", "--stack-depth", "999", recordingFile.toString());
        String json = output.getStdout();

        // Parse JSON using Nashorn
        String statement = "var jsonObject = " + json;
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("nashorn");
        engine.eval(statement);
        JSObject o = (JSObject) engine.get("jsonObject");
        JSObject recording = (JSObject) o.getMember("recording");
        JSObject jsonEvents = (JSObject) recording.getMember("events");

        List<RecordedEvent> events = RecordingFile.readAllEvents(recordingFile);
        Collections.sort(events, (e1, e2) -> e1.getEndTime().compareTo(e2.getEndTime()));
        // Verify events are equal
        Iterator<RecordedEvent> it = events.iterator();

        for (Object jsonEvent : jsonEvents.values()) {
            RecordedEvent recordedEvent = it.next();
            String typeName = recordedEvent.getEventType().getName();
            Asserts.assertEquals(typeName, ((JSObject) jsonEvent).getMember("type").toString());
            assertEquals(jsonEvent, recordedEvent);
        }
        Asserts.assertFalse(events.size() != jsonEvents.values().size(), "Incorrect number of events");
    }
 
Example 3
Source File: NativeJSON.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Object getProperty(final Object holder, final Object key) {
    if (holder instanceof ScriptObject) {
        return ((ScriptObject)holder).get(key);
    } else if (holder instanceof JSObject) {
        JSObject jsObj = (JSObject)holder;
        if (key instanceof Integer) {
            return jsObj.getSlot((Integer)key);
        } else {
            return jsObj.getMember(Objects.toString(key));
        }
    } else {
        return new AssertionError("should not reach here");
    }
}
 
Example 4
Source File: TestPrintJSON.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws Throwable {

        Path recordingFile = ExecuteHelper.createProfilingRecording().toAbsolutePath();

        OutputAnalyzer output = ExecuteHelper.jfr("print", "--json", "--stack-depth", "999", recordingFile.toString());
        String json = output.getStdout();

        // Parse JSON using Nashorn
        String statement = "var jsonObject = " + json;
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("nashorn");
        engine.eval(statement);
        JSObject o = (JSObject) engine.get("jsonObject");
        JSObject recording = (JSObject) o.getMember("recording");
        JSObject jsonEvents = (JSObject) recording.getMember("events");

        List<RecordedEvent> events = RecordingFile.readAllEvents(recordingFile);
        Collections.sort(events, (e1, e2) -> e1.getEndTime().compareTo(e2.getEndTime()));
        // Verify events are equal
        Iterator<RecordedEvent> it = events.iterator();

        for (Object jsonEvent : jsonEvents.values()) {
            RecordedEvent recordedEvent = it.next();
            String typeName = recordedEvent.getEventType().getName();
            Asserts.assertEquals(typeName, ((JSObject) jsonEvent).getMember("type").toString());
            assertEquals(jsonEvent, recordedEvent);
        }
        Asserts.assertFalse(events.size() != jsonEvents.values().size(), "Incorrect number of events");
    }
 
Example 5
Source File: NativeJSON.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Object getProperty(final Object holder, final Object key) {
    if (holder instanceof ScriptObject) {
        return ((ScriptObject)holder).get(key);
    } else if (holder instanceof JSObject) {
        JSObject jsObj = (JSObject)holder;
        if (key instanceof Integer) {
            return jsObj.getSlot((Integer)key);
        } else {
            return jsObj.getMember(Objects.toString(key));
        }
    } else {
        return new AssertionError("should not reach here");
    }
}
 
Example 6
Source File: NativeJSON.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static Object getProperty(final Object holder, final Object key) {
    if (holder instanceof ScriptObject) {
        return ((ScriptObject)holder).get(key);
    } else if (holder instanceof JSObject) {
        JSObject jsObj = (JSObject)holder;
        if (key instanceof Integer) {
            return jsObj.getSlot((Integer)key);
        } else {
            return jsObj.getMember(Objects.toString(key));
        }
    } else {
        return new AssertionError("should not reach here");
    }
}
 
Example 7
Source File: NativeJSON.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static Object getProperty(final Object holder, final Object key) {
    if (holder instanceof ScriptObject) {
        return ((ScriptObject)holder).get(key);
    } else if (holder instanceof JSObject) {
        final JSObject jsObj = (JSObject)holder;
        if (key instanceof Integer) {
            return jsObj.getSlot((Integer)key);
        } else {
            return jsObj.getMember(Objects.toString(key));
        }
    } else {
        return new AssertionError("should not reach here");
    }
}
 
Example 8
Source File: NativeJSON.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static Object getProperty(final Object holder, final Object key) {
    if (holder instanceof ScriptObject) {
        return ((ScriptObject)holder).get(key);
    } else if (holder instanceof JSObject) {
        JSObject jsObj = (JSObject)holder;
        if (key instanceof Integer) {
            return jsObj.getSlot((Integer)key);
        } else {
            return jsObj.getMember(Objects.toString(key));
        }
    } else {
        return new AssertionError("should not reach here");
    }
}
 
Example 9
Source File: JsMagmaScriptEvaluator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates magmascript bindings for a given Entity.
 *
 * @param entity the entity to bind to the magmascript $ function
 * @param depth maximum depth to follow references when creating the entity value map
 * @return Bindings with $ function bound to the entity
 */
private Bindings createBindings(Entity entity, int depth) {
  Bindings bindings = new SimpleBindings();
  JSObject global = (JSObject) magmaBindings.get("nashorn.global");
  JSObject magmaScript = (JSObject) global.getMember(KEY_MAGMA_SCRIPT);
  JSObject dollarFunction = (JSObject) magmaScript.getMember(KEY_DOLLAR);
  JSObject bindFunction = (JSObject) dollarFunction.getMember(BIND);
  Object boundDollar = bindFunction.call(dollarFunction, toScriptEngineValueMap(entity, depth));
  bindings.put(KEY_DOLLAR, boundDollar);
  bindings.put(KEY_NEW_VALUE, magmaScript.getMember(KEY_NEW_VALUE));
  bindings.put(KEY_IS_NULL, magmaScript.getMember(KEY_IS_NULL));
  return bindings;
}
 
Example 10
Source File: Window.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}
 
Example 11
Source File: Window.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}
 
Example 12
Source File: Window.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}
 
Example 13
Source File: Window.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}
 
Example 14
Source File: Window.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}
 
Example 15
Source File: Window.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}
 
Example 16
Source File: Window.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
public static Object funcJSObject(final JSObject jsobj) {
    return jsobj.getMember("foo");
}