Java Code Examples for jdk.nashorn.internal.runtime.ScriptObject#entrySet()

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject#entrySet() . 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: ContextTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 2
Source File: ContextTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 3
Source File: ContextTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 4
Source File: ContextTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 5
Source File: ContextTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 6
Source File: ContextTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 7
Source File: ContextTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}