test/nashorn/script/basic/JDK-8023630.js
author michaelm
Wed, 30 Oct 2019 11:53:07 +0000
branchunixdomainchannels
changeset 58856 b2f0339a4cad
parent 47216 71c04702a3d5
permissions -rw-r--r--
removing unnecessary diffs from mainline
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     1
/*
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 19627
diff changeset
     4
 *
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 19627
diff changeset
     8
 *
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    13
 * accompanied this code).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 19627
diff changeset
    14
 *
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 19627
diff changeset
    18
 *
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    21
 * questions.
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    22
 */
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    23
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    24
/**
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    25
 * JDK-8023630: Implement Java.super() as the preferred way to call super methods
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    26
 *
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    27
 * @test
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    28
 * @run
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    29
 */
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    30
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    31
var CharArray = Java.type("char[]")
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    32
var jString = Java.type("java.lang.String")
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    33
var Character = Java.type("java.lang.Character")
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    34
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    35
function capitalize(s) {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    36
    if(s instanceof CharArray) {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    37
        return new jString(s).toUpperCase()
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    38
    }
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    39
    if(s instanceof jString) {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    40
        return s.toUpperCase()
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    41
    }
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    42
    return Character.toUpperCase(s) // must be int
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    43
}
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    44
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    45
var sw = new (Java.type("java.io.StringWriter"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    46
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    47
var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    48
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    49
var cw = new FilterWriterAdapter(sw) {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    50
    write: function(s, off, len) {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    51
        s = capitalize(s)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    52
        // Must handle overloads by arity
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 19627
diff changeset
    53
        if(off === undefined) {
19627
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    54
            cw_super.write(s, 0, s.length())
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    55
        } else if (typeof s === "string") {
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    56
            cw_super.write(s, off, len)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    57
        }
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    58
    }
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    59
}
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    60
var cw_super = Java.super(cw)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    61
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    62
cw.write("abcd")
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    63
cw.write("e".charAt(0))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    64
cw.write("fgh".toCharArray())
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    65
cw.write("**ijk**", 2, 3)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    66
cw.write("***lmno**".toCharArray(), 3, 4)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    67
cw.flush()
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    68
print(sw)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    69
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    70
// Can invoke super for Object methods
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    71
print("cw_super has hashCode(): " + (typeof cw_super.hashCode === "function"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    72
print("cw_super has super equals(): " + (typeof cw_super.equals === "function"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    73
// Can't invoke super for final methods
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    74
print("cw_super has no getClass(): " + (typeof cw_super.getClass === "undefined"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    75
print("cw_super has no wait(): " + (typeof cw_super.wait === "undefined"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    76
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    77
var r = new (Java.type("java.lang.Runnable"))(function() {})
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    78
var r_super = Java.super(r)
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    79
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    80
// Can't invoke super for abstract methods
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    81
print("r_super has no run(): " + (typeof r_super.run === "undefined"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    82
// Interfaces can also invoke super Object methods
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    83
print("r_super has hashCode(): " + (typeof r_super.hashCode === "function"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    84
print("r_super has equals(): " + (typeof r_super.equals === "function"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    85
// But still can't invoke final methods
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    86
print("r_super has no getClass(): " + (typeof r_super.getClass === "undefined"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    87
print("r_super has no wait(): " + (typeof r_super.wait === "undefined"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    88
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    89
var name = "write"
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    90
print("cw_super can access write through [] getter: " + (typeof cw_super[name] === "function"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    91
var name = "hashCode"
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    92
print("cw_super can access hashCode through [] getter: " + (typeof cw_super[name] === "function"))
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    93
var name = "getClass"
90d910ec15a3 8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
diff changeset
    94
print("cw_super can not access getClass through [] getter: " + (typeof cw_super[name] === "undefined"))