JDK-8043232.js revision 1029:f2771da9af07
1210284Sjmallett/* 2210284Sjmallett * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3210284Sjmallett * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4210284Sjmallett * 5210284Sjmallett * This code is free software; you can redistribute it and/or modify it 6210284Sjmallett * under the terms of the GNU General Public License version 2 only, as 7210284Sjmallett * published by the Free Software Foundation. 8210284Sjmallett * 9210284Sjmallett * This code is distributed in the hope that it will be useful, but WITHOUT 10210284Sjmallett * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11210284Sjmallett * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12210284Sjmallett * version 2 for more details (a copy is included in the LICENSE file that 13210284Sjmallett * accompanied this code). 14210284Sjmallett * 15210284Sjmallett * You should have received a copy of the GNU General Public License version 16210284Sjmallett * 2 along with this work; if not, write to the Free Software Foundation, 17210284Sjmallett * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18210284Sjmallett * 19210284Sjmallett * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20210284Sjmallett * or visit www.oracle.com if you need additional information or have any 21210284Sjmallett * questions. 22210284Sjmallett */ 23210284Sjmallett 24210284Sjmallett/** 25210284Sjmallett * JDK-8043232: Index selection of overloaded java new constructors 26210284Sjmallett * 27210284Sjmallett * @test 28210284Sjmallett * @run 29210284Sjmallett */ 30210284Sjmallett 31210284Sjmallett// call explicit constructor 32210284Sjmallettprint(new (java.lang["String(char[],int,int)"])(['a','b', 'c', 'd'], 1, 3)); 33210284Sjmallett// print the constructor itself 34210284Sjmallettprint(java.lang["String(char[],int,int)"]); 35210284Sjmallett 36210284Sjmallett// store constructor to call later 37210284Sjmallettvar Color = java.lang["String(char[],int,int)"]; 38210284Sjmallett// call stored constructor 39210284Sjmallettprint(new Color(['r','r', 'e', 'd'], 1, 3)) 40210284Sjmallett 41210284Sjmallett// check if default constructor works 42210284Sjmallettvar obj = new (java.lang["Object()"])(); 43210284Sjmallettif (obj.class != java.lang.Object.class) { 44210284Sjmallett fail("obj is a java.lang.Object"); 45210284Sjmallett} 46210284Sjmallett 47210284Sjmallett// expected failure cases. 48210284Sjmallettfunction checkIt(func) { 49210284Sjmallett try { 50210284Sjmallett func(); 51210284Sjmallett throw new Error("should have thrown TypeError"); 52210284Sjmallett } catch(e) { 53210284Sjmallett if (! (e instanceof TypeError)) { 54210284Sjmallett fail("Expected TypeError, got " + e); 55210284Sjmallett } 56210284Sjmallett print(e); 57210284Sjmallett } 58210284Sjmallett} 59210284Sjmallett 60210284Sjmallett// constructor of a non-existent class 61210284SjmallettcheckIt(function() new (java.lang["NonExistent(String)"])()); 62210284Sjmallett 63210284Sjmallett// non-existent constructor of an existing class 64210284SjmallettcheckIt(function() new (java.lang["Object(String)"])()); 65210284Sjmallett 66210284Sjmallett// garbage signature string 67210284SjmallettcheckIt(function() new (java.lang["Object()xxxxx"])()); 68210284SjmallettcheckIt(function() new (java.lang["Object("])()); 69210284SjmallettcheckIt(function() new (java.lang["Object)"])()); 70210284Sjmallett 71210284Sjmallettvar System = Java.type("java.lang.System"); 72210284Sjmallett// try to do 'new' on static method 73210284SjmallettcheckIt(function() new (System.getProperty)("java.version")); 74210284Sjmallett 75210284Sjmallett// try to do 'new' on an instance method 76210284Sjmallettvar println = System.out.println; 77210284SjmallettcheckIt(function() new println("hello")); 78210284Sjmallett 79210284Sjmallett// call constructor as normal method (without 'new') 80210284SjmallettcheckIt(function() Color()); 81210284Sjmallett 82210284Sjmallett// try constructor on interface 83210284SjmallettcheckIt(function() new java.lang["Runnable()"]); 84210284SjmallettcheckIt(function() new java.lang["Runnable(int)"]); 85210284Sjmallett 86210284Sjmallett// try constructor on abstrace class 87210284Sjmalletttry { 88210284Sjmallett new java.io["InputStream()"]; 89210284Sjmallett throw new Error("should have thrown exception!"); 90210284Sjmallett} catch (e) { 91210284Sjmallett print(e); 92210284Sjmallett} 93210284Sjmallett