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