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-8023373: allow super invocation for adapters 26 * 27 * @test 28 * @run 29 */ 30 31 var CharArray = Java.type("char[]") 32 var jString = Java.type("java.lang.String") 33 var Character = Java.type("java.lang.Character") 34 35 function 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 45 var sw = new (Java.type("java.io.StringWriter")) 46 47 var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter")) 48 49 var 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 } 60 61 cw.write("abcd") 62 cw.write("e".charAt(0)) 63 cw.write("fgh".toCharArray()) 64 cw.write("**ijk**", 2, 3) 65 cw.write("***lmno**".toCharArray(), 3, 4) 66 cw.flush() 67 print(sw) 68 69 // Can invoke super for Object methods 70 print("cw has super hashCode(): " + (typeof cw.super$hashCode === "function")) 71 print("cw has super equals(): " + (typeof cw.super$equals === "function")) 72 // Can't invoke super for final methods 73 print("cw has no super getClass(): " + (typeof cw.super$getClass === "undefined")) 74 print("cw has no super wait(): " + (typeof cw.super$wait === "undefined")) 75 76 var r = new (Java.type("java.lang.Runnable"))(function() {}) 77 // Can't invoke super for abstract methods 78 print("r has no super run(): " + (typeof r.super$run === "undefined")) 79 // Interfaces can also invoke super Object methods 80 print("r has super hashCode(): " + (typeof r.super$hashCode === "function")) 81 print("r has super equals(): " + (typeof r.super$equals === "function")) 82 // But still can't invoke final methods 83 print("r has no super getClass(): " + (typeof r.super$getClass === "undefined")) 84 print("r has no super wait(): " + (typeof r.super$wait === "undefined"))