nashorn/test/script/basic/JDK-8043232.js
author sundar
Wed, 02 Jul 2014 18:10:31 +0530
changeset 25252 e8bfc909db53
child 25258 325380d7c38c
permissions -rw-r--r--
8043232: Index selection of overloaded java new constructors Reviewed-by: attila, hannesw, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25252
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     1
/*
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     4
 *
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     8
 *
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    13
 * accompanied this code).
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    14
 *
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    18
 *
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    21
 * questions.
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    22
 */
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    23
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    24
/**
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    25
 * JDK-8043232: Index selection of overloaded java new constructors
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    26
 *
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    27
 * @test
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    28
 * @run
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    29
 */
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    30
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    31
// call explicit constructor
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    32
print(new (java.awt["Color(int,int,int)"])(255,0,255));
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    33
// print the constructor itself
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    34
print(java.awt["Color(int,int,int)"]);
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    35
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    36
// store constructor to call later
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    37
var Color = java.awt["Color(int,int,int)"];
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    38
// call stored constructor
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    39
print(new Color(33, 233, 2))
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    40
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    41
// check if default constructor works
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    42
var obj = new (java.lang["Object()"])();
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    43
if (obj.class != java.lang.Object.class) {
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    44
    fail("obj is a java.lang.Object");
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    45
}
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    46
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    47
// expected failure cases.
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    48
function checkIt(func) {
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    49
    try {
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    50
        func();
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    51
        throw new Error("should have thrown TypeError");
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    52
    } catch(e) {
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    53
        if (! (e instanceof TypeError)) {
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    54
            fail("Expected TypeError, got " + e);
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    55
        }
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    56
        print(e);
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    57
    }
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    58
}
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    59
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    60
// constructor of a non-existent class
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    61
checkIt(function() new (java.lang["NonExistent(String)"])());  
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    62
 
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    63
// non-existent constructor of an existing class
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    64
checkIt(function() new (java.lang["Object(String)"])());
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    65
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    66
// garbage signature string
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    67
checkIt(function() new (java.lang["Object()xxxxx"])());
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    68
checkIt(function() new (java.lang["Object("])());
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    69
checkIt(function() new (java.lang["Object)"])());
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    70
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    71
var System = Java.type("java.lang.System");
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    72
// try to do 'new' on static method
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    73
checkIt(function() new (System.getProperty)("java.version"));
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    74
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    75
// try to do 'new' on an instance method
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    76
var println = System.out.println;
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    77
checkIt(function() new println("hello"));
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    78
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    79
// call constructor as normal method (without 'new')
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    80
checkIt(function() Color());