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