Wed, 05 Sep 2018 01:21:35 -0700
Added tag jdk8u192-b09 for changeset 456c0d45c43b
sundar@964 | 1 | /* |
sundar@964 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
sundar@964 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
sundar@964 | 4 | * |
sundar@964 | 5 | * This code is free software; you can redistribute it and/or modify it |
sundar@964 | 6 | * under the terms of the GNU General Public License version 2 only, as |
sundar@964 | 7 | * published by the Free Software Foundation. |
sundar@964 | 8 | * |
sundar@964 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
sundar@964 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
sundar@964 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
sundar@964 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
sundar@964 | 13 | * accompanied this code). |
sundar@964 | 14 | * |
sundar@964 | 15 | * You should have received a copy of the GNU General Public License version |
sundar@964 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
sundar@964 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
sundar@964 | 18 | * |
sundar@964 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
sundar@964 | 20 | * or visit www.oracle.com if you need additional information or have any |
sundar@964 | 21 | * questions. |
sundar@964 | 22 | */ |
sundar@964 | 23 | |
sundar@964 | 24 | /** |
sundar@964 | 25 | * ClassFilter to filter out java classes in a script engine. |
sundar@964 | 26 | * |
sundar@964 | 27 | * @test |
sundar@964 | 28 | * @run |
sundar@964 | 29 | */ |
sundar@964 | 30 | |
sundar@964 | 31 | var NashornScriptEngineFactory = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory"); |
sundar@964 | 32 | |
sundar@964 | 33 | var fac = new NashornScriptEngineFactory(); |
sundar@964 | 34 | // allow only "java.*" classes to be accessed |
sundar@964 | 35 | var e = fac.getScriptEngine( |
sundar@964 | 36 | function(name) name.startsWith("java.")); |
sundar@964 | 37 | |
sundar@964 | 38 | function evalIt(str) { |
sundar@964 | 39 | print(str + " evalutes to " + e.eval(str)); |
sundar@964 | 40 | } |
sundar@964 | 41 | |
sundar@964 | 42 | function evalExpectError(str) { |
sundar@964 | 43 | try { |
sundar@964 | 44 | print(e.eval(str)); |
sundar@964 | 45 | fail("expected error for: " + str); |
sundar@964 | 46 | } catch(exp) { |
sundar@964 | 47 | print(str + " throws " + exp); |
sundar@964 | 48 | } |
sundar@964 | 49 | } |
sundar@964 | 50 | |
sundar@964 | 51 | evalIt("typeof javax.script.ScriptContext"); |
sundar@964 | 52 | evalIt("typeof javax.script.ScriptEngine"); |
sundar@964 | 53 | evalIt("typeof java.util.Vector"); |
sundar@964 | 54 | evalIt("typeof java.util.Map"); |
sundar@964 | 55 | evalIt("typeof java.util.HashMap"); |
sundar@964 | 56 | // should be able to call methods, create objects of java.* classes |
sundar@964 | 57 | evalIt("var m = new java.util.HashMap(); m.put('foo', 42); m"); |
sundar@964 | 58 | evalIt("java.lang.System.out.println"); |
sundar@964 | 59 | evalIt("java.lang.System.exit"); |
sundar@964 | 60 | |
sundar@964 | 61 | evalExpectError("new javax.script.SimpleBindings"); |
sundar@964 | 62 | evalExpectError("Java.type('javax.script.ScriptContext')"); |
sundar@964 | 63 | evalExpectError("java.lang.Class.forName('javax.script.ScriptContext')"); |
sundar@964 | 64 | |
sundar@964 | 65 | try { |
sundar@964 | 66 | fac["getScriptEngine(ClassFilter)"](null); |
sundar@964 | 67 | fail("should have thrown NPE"); |
sundar@964 | 68 | } catch (e) { |
sundar@964 | 69 | if (! (e instanceof java.lang.NullPointerException)) { |
sundar@964 | 70 | fail("NPE expected, got " + e); |
sundar@964 | 71 | } |
sundar@964 | 72 | } |