aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. attila@962: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. attila@962: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). attila@962: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. attila@962: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * JDK-8023630: Implement Java.super() as the preferred way to call super methods aoqi@0: * aoqi@0: * @test aoqi@0: * @run aoqi@0: */ aoqi@0: aoqi@0: var CharArray = Java.type("char[]") aoqi@0: var jString = Java.type("java.lang.String") aoqi@0: var Character = Java.type("java.lang.Character") aoqi@0: aoqi@0: function capitalize(s) { aoqi@0: if(s instanceof CharArray) { aoqi@0: return new jString(s).toUpperCase() aoqi@0: } aoqi@0: if(s instanceof jString) { aoqi@0: return s.toUpperCase() aoqi@0: } aoqi@0: return Character.toUpperCase(s) // must be int aoqi@0: } aoqi@0: aoqi@0: var sw = new (Java.type("java.io.StringWriter")) aoqi@0: aoqi@0: var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter")) aoqi@0: aoqi@0: var cw = new FilterWriterAdapter(sw) { aoqi@0: write: function(s, off, len) { aoqi@0: s = capitalize(s) aoqi@0: // Must handle overloads by arity attila@962: if(off === undefined) { aoqi@0: cw_super.write(s, 0, s.length()) aoqi@0: } else if (typeof s === "string") { aoqi@0: cw_super.write(s, off, len) aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: var cw_super = Java.super(cw) aoqi@0: aoqi@0: cw.write("abcd") aoqi@0: cw.write("e".charAt(0)) aoqi@0: cw.write("fgh".toCharArray()) aoqi@0: cw.write("**ijk**", 2, 3) aoqi@0: cw.write("***lmno**".toCharArray(), 3, 4) aoqi@0: cw.flush() aoqi@0: print(sw) aoqi@0: aoqi@0: // Can invoke super for Object methods aoqi@0: print("cw_super has hashCode(): " + (typeof cw_super.hashCode === "function")) aoqi@0: print("cw_super has super equals(): " + (typeof cw_super.equals === "function")) aoqi@0: // Can't invoke super for final methods aoqi@0: print("cw_super has no getClass(): " + (typeof cw_super.getClass === "undefined")) aoqi@0: print("cw_super has no wait(): " + (typeof cw_super.wait === "undefined")) aoqi@0: aoqi@0: var r = new (Java.type("java.lang.Runnable"))(function() {}) aoqi@0: var r_super = Java.super(r) aoqi@0: aoqi@0: // Can't invoke super for abstract methods aoqi@0: print("r_super has no run(): " + (typeof r_super.run === "undefined")) aoqi@0: // Interfaces can also invoke super Object methods aoqi@0: print("r_super has hashCode(): " + (typeof r_super.hashCode === "function")) aoqi@0: print("r_super has equals(): " + (typeof r_super.equals === "function")) aoqi@0: // But still can't invoke final methods aoqi@0: print("r_super has no getClass(): " + (typeof r_super.getClass === "undefined")) aoqi@0: print("r_super has no wait(): " + (typeof r_super.wait === "undefined")) aoqi@0: aoqi@0: var name = "write" aoqi@0: print("cw_super can access write through [] getter: " + (typeof cw_super[name] === "function")) aoqi@0: var name = "hashCode" aoqi@0: print("cw_super can access hashCode through [] getter: " + (typeof cw_super[name] === "function")) aoqi@0: var name = "getClass" aoqi@0: print("cw_super can not access getClass through [] getter: " + (typeof cw_super[name] === "undefined"))