|
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 |
|
31 var System = Java.type("java.lang.System"); |
|
32 var out = System.out; |
|
33 var println = out.println; |
|
34 var getProperty = System.getProperty; |
|
35 var File = Java.type("java.io.File")["(String)"]; |
|
36 |
|
37 print("println is java method? " + Java.isJavaMethod(println)); |
|
38 print("println is script function? " + Java.isScriptFunction(println)); |
|
39 print("getProperty is java method? " + Java.isJavaMethod(getProperty)); |
|
40 print("getProperty is script function? " + Java.isScriptFunction(getProperty)); |
|
41 print("File is java method? " + Java.isJavaMethod(File)); |
|
42 print("File is script function? " + Java.isScriptFunction(File)); |
|
43 |
|
44 print("eval is script function? " + Java.isScriptFunction(eval)); |
|
45 print("eval is java method? " + Java.isJavaMethod(eval)); |
|
46 function hello() {} |
|
47 print("hello is script function? " + Java.isScriptFunction(hello)); |
|
48 print("hello is java method? " + Java.isJavaMethod(hello)); |
|
49 |
|
50 print("out is script object? " + Java.isScriptObject(out)); |
|
51 print("System is script object? " + Java.isScriptObject(System)); |
|
52 print("Object is script object? " + Java.isScriptObject(Object)); |
|
53 print("{} is script object? " + Java.isScriptObject({})); |
|
54 print("/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 |
|
61 print("java.lang.String is java function? " + Java.isJavaFunction(java.lang.String)); |
|
62 print("java.lang.Runnable instance is java function? " |
|
63 + Java.isJavaFunction(new java.lang.Runnable(function() {}))); |
|
64 print("eval is java function? " + Java.isJavaFunction(eval)); |
|
65 print("println is java function? " + Java.isJavaFunction(println)); |
|
66 print("getProperty is java function? " + Java.isJavaFunction(getProperty)); |
|
67 |
|
68 var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject"); |
|
69 print("callable JSObject is function? " + |
|
70 Java.isJavaFunction(new JSObject() { |
|
71 isFunction: function() true, |
|
72 call: function() {} |
|
73 }) |
|
74 ); |
|
75 |
|
76 print("Non callable JSObject is function? " + |
|
77 Java.isJavaFunction(new JSObject() { |
|
78 isFunction: function() false, |
|
79 }) |
|
80 ); |
|
81 |
|
82 // synchronized function |
|
83 var lock = new java.lang.Object(); |
|
84 |
|
85 print("lock is java object? " + Java.isJavaObject(lock)); |
|
86 print("eval is java object? " + Java.isJavaObject(eval)); |
|
87 print("{} is java object? " + Java.isJavaObject({})); |
|
88 print("/foo/ is java object? " + Java.isJavaObject(/foo/)); |
|
89 print("[] is java object? " + Java.isJavaObject([])); |
|
90 print("java.io.File is java object? " + Java.isJavaObject(java.io.File)); |
|
91 |
|
92 // synchornized function checks |
|
93 Java.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 |
|
107 load("nashorn:mozilla_compat.js"); |
|
108 sync(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 |
|
121 function 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 |
|
133 expectTypeError(function() Java.synchronized(232)); |
|
134 expectTypeError(function() sync(232)); |
|
135 expectTypeError(function() Java.synchronized({})); |
|
136 expectTypeError(function() sync({})); |
|
137 expectTypeError(function() Java.synchronized([])); |
|
138 expectTypeError(function() sync([])); |
|
139 expectTypeError(function() Java.synchronized("hello")); |
|
140 expectTypeError(function() sync("hello")); |
|
141 expectTypeError(function() Java.synchronized(null)); |
|
142 expectTypeError(function() sync(null)); |
|
143 expectTypeError(function() Java.synchronized(undefined)); |
|
144 expectTypeError(function() sync(undefined)); |