Tue, 22 Jul 2014 10:48:36 -0700
Merge
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 */
24 /**
25 * JDK-8023630: Implement Java.super() as the preferred way to call super methods
26 *
27 * @test
28 * @run
29 */
31 var CharArray = Java.type("char[]")
32 var jString = Java.type("java.lang.String")
33 var Character = Java.type("java.lang.Character")
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 }
45 var sw = new (Java.type("java.io.StringWriter"))
47 var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter"))
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 var cw_super = Java.super(cw)
62 cw.write("abcd")
63 cw.write("e".charAt(0))
64 cw.write("fgh".toCharArray())
65 cw.write("**ijk**", 2, 3)
66 cw.write("***lmno**".toCharArray(), 3, 4)
67 cw.flush()
68 print(sw)
70 // Can invoke super for Object methods
71 print("cw_super has hashCode(): " + (typeof cw_super.hashCode === "function"))
72 print("cw_super has super equals(): " + (typeof cw_super.equals === "function"))
73 // Can't invoke super for final methods
74 print("cw_super has no getClass(): " + (typeof cw_super.getClass === "undefined"))
75 print("cw_super has no wait(): " + (typeof cw_super.wait === "undefined"))
77 var r = new (Java.type("java.lang.Runnable"))(function() {})
78 var r_super = Java.super(r)
80 // Can't invoke super for abstract methods
81 print("r_super has no run(): " + (typeof r_super.run === "undefined"))
82 // Interfaces can also invoke super Object methods
83 print("r_super has hashCode(): " + (typeof r_super.hashCode === "function"))
84 print("r_super has equals(): " + (typeof r_super.equals === "function"))
85 // But still can't invoke final methods
86 print("r_super has no getClass(): " + (typeof r_super.getClass === "undefined"))
87 print("r_super has no wait(): " + (typeof r_super.wait === "undefined"))
89 var name = "write"
90 print("cw_super can access write through [] getter: " + (typeof cw_super[name] === "function"))
91 var name = "hashCode"
92 print("cw_super can access hashCode through [] getter: " + (typeof cw_super[name] === "function"))
93 var name = "getClass"
94 print("cw_super can not access getClass through [] getter: " + (typeof cw_super[name] === "undefined"))