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