JDK-8049086.js revision 928:3ec6924f7b57
1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * JDK-8049086: Minor API convenience functions on "Java" object
26 *
27 * @test
28 * @run
29 */
30
31var System = Java.type("java.lang.System");
32var out = System.out;
33var println = out.println;
34var getProperty = System.getProperty;
35var File = Java.type("java.io.File")["(String)"];
36
37print("println is java method? " + Java.isJavaMethod(println));
38print("println is script function? " + Java.isScriptFunction(println));
39print("getProperty is java method? " + Java.isJavaMethod(getProperty));
40print("getProperty is script function? " + Java.isScriptFunction(getProperty));
41print("File is java method? " + Java.isJavaMethod(File));
42print("File is script function? " + Java.isScriptFunction(File));
43
44print("eval is script function? " + Java.isScriptFunction(eval));
45print("eval is java method? " + Java.isJavaMethod(eval));
46function hello() {}
47print("hello is script function? " + Java.isScriptFunction(hello));
48print("hello is java method? " + Java.isJavaMethod(hello));
49
50print("out is script object? " + Java.isScriptObject(out));
51print("System is script object? " + Java.isScriptObject(System));
52print("Object is script object? " + Java.isScriptObject(Object));
53print("{} is script object? " + Java.isScriptObject({}));
54print("/foo/ is script object? " + Java.isScriptObject(/foo/));
55
56// Java function is anything whose 'typeof' is 'function' but it is not
57// a script function! This includes:
58// (a) Java methods (b) Java classes (as these respond to new)
59// (c) FunctionalInterface objects (d) JSObjects that are 'functions'
60
61print("java.awt.Color is java function? " + Java.isJavaFunction(java.awt.Color));
62print("java.lang.Runnable instance is java function? "
63    + Java.isJavaFunction(new java.lang.Runnable(function() {})));
64print("eval is java function? " + Java.isJavaFunction(eval));
65print("println is java function? " + Java.isJavaFunction(println));
66print("getProperty is java function? " + Java.isJavaFunction(getProperty));
67
68var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject");
69print("callable JSObject is function? " +
70    Java.isJavaFunction(new JSObject() {
71        isFunction: function() true,
72        call: function() {}
73    })
74);
75
76print("Non callable JSObject is function? " +
77    Java.isJavaFunction(new JSObject() {
78        isFunction: function() false,
79    })
80);
81
82// synchronized function
83var lock = new java.lang.Object();
84
85print("lock is java object? " + Java.isJavaObject(lock));
86print("eval is java object? " + Java.isJavaObject(eval));
87print("{} is java object? " + Java.isJavaObject({}));
88print("/foo/ is java object? " + Java.isJavaObject(/foo/));
89print("[] is java object? " + Java.isJavaObject([]));
90print("java.io.File is java object? " + Java.isJavaObject(java.io.File));
91
92// synchornized function checks
93Java.synchronized(function() {
94    var th = new java.lang.Thread(Java.synchronized(function() {
95        print("new thread");
96        print("notifying..");
97        lock.notifyAll();
98    }, lock));
99    th.start();
100    print("about to wait..");
101    lock.wait();
102    th.join();
103    print("done waiting!");
104}, lock)();
105
106// try Mozilla "sync" as well
107load("nashorn:mozilla_compat.js");
108sync(function() {
109    var th = new java.lang.Thread(sync(function() {
110        print("new thread");
111        print("notifying..");
112        lock.notifyAll();
113    }, lock));
114    th.start();
115    print("about to wait..");
116    lock.wait();
117    th.join();
118    print("done waiting!");
119}, lock)();
120
121function expectTypeError(func) {
122    try {
123        func();
124        throw new Error("should have thrown TypeError");
125    } catch (e) {
126        if (! (e instanceof TypeError)) {
127            fail("Expected TypeError, got " +e);
128        }
129        print(e);
130    }
131}
132
133expectTypeError(function() Java.synchronized(232));
134expectTypeError(function() sync(232));
135expectTypeError(function() Java.synchronized({}));
136expectTypeError(function() sync({}));
137expectTypeError(function() Java.synchronized([]));
138expectTypeError(function() sync([]));
139expectTypeError(function() Java.synchronized("hello"));
140expectTypeError(function() sync("hello"));
141expectTypeError(function() Java.synchronized(null));
142expectTypeError(function() sync(null));
143expectTypeError(function() Java.synchronized(undefined));
144expectTypeError(function() sync(undefined));
145