test/script/basic/javaclassoverrides.js

Wed, 20 Aug 2014 18:59:11 +0530

author
sundar
date
Wed, 20 Aug 2014 18:59:11 +0530
changeset 964
8f2ed41abb26
parent 962
ac62e33a99b0
child 1205
4112748288bb
permissions
-rw-r--r--

8050078: Nashorn ClassFilter Support
Reviewed-by: attila, hannesw, jlaskey, lagergren

attila@175 1 /*
attila@175 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
attila@175 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
attila@962 4 *
attila@175 5 * This code is free software; you can redistribute it and/or modify it
attila@175 6 * under the terms of the GNU General Public License version 2 only, as
attila@175 7 * published by the Free Software Foundation.
attila@962 8 *
attila@175 9 * This code is distributed in the hope that it will be useful, but WITHOUT
attila@175 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
attila@175 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
attila@175 12 * version 2 for more details (a copy is included in the LICENSE file that
attila@175 13 * accompanied this code).
attila@962 14 *
attila@175 15 * You should have received a copy of the GNU General Public License version
attila@175 16 * 2 along with this work; if not, write to the Free Software Foundation,
attila@175 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
attila@962 18 *
attila@175 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
attila@175 20 * or visit www.oracle.com if you need additional information or have any
attila@175 21 * questions.
attila@175 22 */
attila@175 23
attila@175 24 /**
attila@175 25 * Check behavior of class-level overrides.
attila@175 26 *
attila@175 27 * @test
attila@175 28 * @run
attila@175 29 */
attila@175 30
attila@175 31
attila@175 32 // Make two classes with class overrides
attila@175 33
attila@175 34 var R1 = Java.extend(java.lang.Runnable, {
attila@175 35 run: function() {
attila@175 36 print("R1.run() invoked")
attila@175 37 }
attila@175 38 })
attila@175 39
attila@175 40 var R2 = Java.extend(java.lang.Runnable, {
attila@175 41 run: function() {
attila@175 42 print("R2.run() invoked")
attila@175 43 }
attila@175 44 })
attila@175 45
attila@175 46 var r1 = new R1
attila@175 47 var r2 = new R2
attila@175 48 // Create one with an instance-override too
attila@719 49 var R3 = Java.extend(R2)
attila@719 50 var r3 = new R3({ run: function() { print("r3.run() invoked") }})
attila@175 51
attila@175 52 // Run 'em - we're passing them through a Thread to make sure they indeed
attila@175 53 // are full-blown Runnables
attila@175 54 function runInThread(r) {
attila@175 55 var t = new java.lang.Thread(r)
attila@175 56 t.start()
attila@175 57 t.join()
attila@175 58 }
attila@175 59 runInThread(r1)
attila@175 60 runInThread(r2)
attila@175 61 runInThread(r3)
attila@175 62
attila@175 63 // Two class-override classes differ
attila@719 64 print("r1.class !== r2.class: " + (r1.class !== r2.class))
attila@719 65 // instance-override class also differs
attila@719 66 print("r2.class !== r3.class: " + (r2.class !== r3.class))
attila@175 67
attila@175 68 function checkAbstract(r) {
attila@175 69 try {
attila@175 70 r.run()
attila@175 71 print("Expected to fail!")
attila@175 72 } catch(e) {
attila@175 73 print("Got exception: " + e)
attila@175 74 }
attila@175 75 }
attila@175 76
attila@175 77 // Check we're hitting UnsupportedOperationException if neither class
attila@175 78 // overrides nor instance overrides are present
attila@175 79 var RAbstract = Java.extend(java.lang.Runnable, {})
attila@175 80 checkAbstract(new RAbstract()) // class override (empty)
attila@719 81 checkAbstract(new (Java.extend(RAbstract))() {}) // class+instance override (empty)
attila@175 82
attila@175 83 // Check we delegate to superclass if neither class
attila@175 84 // overrides nor instance overrides are present
attila@175 85 var ExtendsList = Java.extend(java.util.ArrayList, {})
attila@175 86 print("(new ExtendsList).size() = " + (new ExtendsList).size())
attila@719 87 print("(new (Java.extend(ExtendsList)){}).size() = " + (new (Java.extend(ExtendsList)){}).size())

mercurial