test/script/basic/JDK-8023630.js

Tue, 21 Mar 2017 13:41:57 -0700

author
asaha
date
Tue, 21 Mar 2017 13:41:57 -0700
changeset 2160
1df40fe54cd6
parent 962
ac62e33a99b0
permissions
-rw-r--r--

Merge

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

mercurial