nashorn/test/script/basic/javaclassoverrides.js
changeset 22669 75563515567f
parent 16777 207a993adb9a
child 24778 2ff5d7041566
equal deleted inserted replaced
22668:245094625886 22669:75563515567f
    44 })
    44 })
    45 
    45 
    46 var r1 = new R1
    46 var r1 = new R1
    47 var r2 = new R2
    47 var r2 = new R2
    48 // Create one with an instance-override too
    48 // Create one with an instance-override too
    49 var r3 = new R2(function() { print("r3.run() invoked") })
    49 var R3 = Java.extend(R2)
       
    50 var r3 = new R3({ run: function() { print("r3.run() invoked") }})
    50 
    51 
    51 // Run 'em - we're passing them through a Thread to make sure they indeed
    52 // Run 'em - we're passing them through a Thread to make sure they indeed
    52 // are full-blown Runnables
    53 // are full-blown Runnables
    53 function runInThread(r) {
    54 function runInThread(r) {
    54     var t = new java.lang.Thread(r)
    55     var t = new java.lang.Thread(r)
    58 runInThread(r1)
    59 runInThread(r1)
    59 runInThread(r2)
    60 runInThread(r2)
    60 runInThread(r3)
    61 runInThread(r3)
    61 
    62 
    62 // Two class-override classes differ
    63 // Two class-override classes differ
    63 print("r1.class != r2.class: " + (r1.class != r2.class))
    64 print("r1.class !== r2.class: " + (r1.class !== r2.class))
    64 // However, adding instance-overrides doesn't change the class
    65 // instance-override class also differs
    65 print("r2.class == r3.class: " + (r2.class == r3.class))
    66 print("r2.class !== r3.class: " + (r2.class !== r3.class))
    66 
    67 
    67 function checkAbstract(r) {
    68 function checkAbstract(r) {
    68     try {
    69     try {
    69         r.run()
    70         r.run()
    70         print("Expected to fail!")
    71         print("Expected to fail!")
    75 
    76 
    76 // Check we're hitting UnsupportedOperationException if neither class
    77 // Check we're hitting UnsupportedOperationException if neither class
    77 // overrides nor instance overrides are present
    78 // overrides nor instance overrides are present
    78 var RAbstract = Java.extend(java.lang.Runnable, {})
    79 var RAbstract = Java.extend(java.lang.Runnable, {})
    79 checkAbstract(new RAbstract()) // class override (empty)
    80 checkAbstract(new RAbstract()) // class override (empty)
    80 checkAbstract(new RAbstract() {}) // class+instance override (empty)
    81 checkAbstract(new (Java.extend(RAbstract))() {}) // class+instance override (empty)
    81 
    82 
    82 // Check we delegate to superclass if neither class
    83 // Check we delegate to superclass if neither class
    83 // overrides nor instance overrides are present
    84 // overrides nor instance overrides are present
    84 var ExtendsList = Java.extend(java.util.ArrayList, {})
    85 var ExtendsList = Java.extend(java.util.ArrayList, {})
    85 print("(new ExtendsList).size() = " + (new ExtendsList).size())
    86 print("(new ExtendsList).size() = " + (new ExtendsList).size())
    86 print("(new ExtendsList(){}).size() = " + (new ExtendsList(){}).size())
    87 print("(new (Java.extend(ExtendsList)){}).size() = " + (new (Java.extend(ExtendsList)){}).size())