nashorn/test/script/basic/JDK-8043232.js
author hannesw
Fri, 03 Feb 2017 13:28:00 +0100
changeset 43555 1bb10bccf057
parent 26652 56623a8db943
permissions -rw-r--r--
8173888: Test for JDK-8169481 causes stack overflows in parser tests Reviewed-by: jlaskey, sundar
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
26652
56623a8db943 8057035: Some tests failed using java.awt.Color on Solaris without X11 libraries
yan
parents: 26067
diff changeset
    32
print(new (java.lang["String(char[],int,int)"])(['a','b', 'c', 'd'], 1, 3));
25252
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    33
// print the constructor itself
26652
56623a8db943 8057035: Some tests failed using java.awt.Color on Solaris without X11 libraries
yan
parents: 26067
diff changeset
    34
print(java.lang["String(char[],int,int)"]);
25252
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
26652
56623a8db943 8057035: Some tests failed using java.awt.Color on Solaris without X11 libraries
yan
parents: 26067
diff changeset
    37
var Color = java.lang["String(char[],int,int)"];
25252
e8bfc909db53 8043232: Index selection of overloaded java new constructors
sundar
parents:
diff changeset
    38
// call stored constructor
26652
56623a8db943 8057035: Some tests failed using java.awt.Color on Solaris without X11 libraries
yan
parents: 26067
diff changeset
    39
print(new Color(['r','r', 'e', 'd'], 1, 3))
25252
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
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25258
diff changeset
    61
checkIt(function() new (java.lang["NonExistent(String)"])());
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 25258
diff changeset
    62
25252
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());
25258
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    81
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    82
// try constructor on interface
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    83
checkIt(function() new java.lang["Runnable()"]);
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    84
checkIt(function() new java.lang["Runnable(int)"]);
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    85
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    86
// try constructor on abstrace class
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    87
try {
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    88
    new java.io["InputStream()"];
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    89
    throw new Error("should have thrown exception!");
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    90
} catch (e) {
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    91
    print(e);
325380d7c38c 8049242: Explicit constructor overload selection should work with StaticClass as well
sundar
parents: 25252
diff changeset
    92
}