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