test/script/sandbox/javaextend.js

Fri, 31 Jan 2014 22:24:45 -0800

author
jeff
date
Fri, 31 Jan 2014 22:24:45 -0800
changeset 796
2d9af8fd30cd
parent 136
c54e218333be
child 719
11b83c913cca
permissions
-rw-r--r--

8032726: THIRD_PARTY_LICENSE_README Update for Little CMS to 2.5
Reviewed-by: lana, prr

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation.
jlaskey@3 8 *
jlaskey@3 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 12 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 13 * accompanied this code).
jlaskey@3 14 *
jlaskey@3 15 * You should have received a copy of the GNU General Public License version
jlaskey@3 16 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 18 *
jlaskey@3 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 20 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 21 * questions.
jlaskey@3 22 */
jlaskey@3 23
jlaskey@3 24 /**
jlaskey@3 25 * @test
jlaskey@3 26 * @run
jlaskey@3 27 */
jlaskey@3 28
jlaskey@3 29 function model(n) {
sundar@136 30 return Java.type("jdk.nashorn.test.models." + n)
jlaskey@3 31 }
jlaskey@3 32
jlaskey@3 33 // Can't extend a final class
jlaskey@3 34 try {
jlaskey@3 35 Java.extend(model("FinalClass"))
jlaskey@3 36 } catch(e) {
jlaskey@3 37 print(e)
jlaskey@3 38 }
jlaskey@3 39
jlaskey@3 40 // Can't extend a class with no public or protected constructor
jlaskey@3 41 try {
jlaskey@3 42 Java.extend(model("NoAccessibleConstructorClass"))
jlaskey@3 43 } catch(e) {
jlaskey@3 44 print(e)
jlaskey@3 45 }
jlaskey@3 46
jlaskey@3 47 // Can't extend a non-public class
jlaskey@3 48 try {
jlaskey@3 49 Java.extend(model("NonPublicClass"))
jlaskey@3 50 } catch(e) {
jlaskey@3 51 print(e)
jlaskey@3 52 }
jlaskey@3 53
attila@27 54 // Can't extend two classes
attila@27 55 try {
attila@27 56 Java.extend(java.lang.Thread,java.lang.Number)
attila@27 57 } catch(e) {
attila@27 58 print(e)
attila@27 59 }
attila@27 60
jlaskey@3 61 // Make sure we can implement interfaces from the unnamed package
jlaskey@3 62 var c = new (Java.extend(Java.type("UnnamedPackageTestCallback")))() { call: function(s) { return s + s } }
jlaskey@3 63 print(c.call("abcd"))
jlaskey@3 64
jlaskey@3 65 // Basic Runnable from an object
jlaskey@3 66 new (Java.extend(java.lang.Runnable))({ run: function() { print("run-object") } }).run()
jlaskey@3 67
jlaskey@3 68 // Basic Runnable from a function
jlaskey@3 69 new (Java.extend(java.lang.Runnable))(function() { print("run-fn") }).run()
jlaskey@3 70
jlaskey@3 71 // Basic Runnable from an autoconverted function
jlaskey@3 72 var t = new java.lang.Thread(function() { print("run-fn-autoconvert") })
jlaskey@3 73 t.start()
jlaskey@3 74 t.join()
jlaskey@3 75
jlaskey@3 76 // SAM conversion should work on overloaded methods of same name
jlaskey@3 77 var os = new (Java.extend(model("OverloadedSam")))(function(s1, s2) { print("overloaded-sam: " + s1 + ", " + s2) })
jlaskey@3 78 os.sam("x")
jlaskey@3 79 os.sam("x", "y")
jlaskey@3 80
jlaskey@3 81 // Test overriding of hashCode, equals, and toString
jlaskey@3 82 var oo = Java.extend(model("OverrideObject"))
jlaskey@3 83 // First, see non-overridden values
jlaskey@3 84 print("oo-plain-hashCode: " + (new oo({})).hashCode())
jlaskey@3 85 print("oo-plain-toString: " + (new oo({})).toString())
jlaskey@3 86 print("oo-plain-equals : " + (new oo({})).equals({}))
jlaskey@3 87 // Now, override them
jlaskey@3 88 print("oo-overridden-hashCode: " + (new oo({ hashCode: function() { return 6 }})).hashCode())
jlaskey@3 89 print("oo-overridden-toString: " + (new oo({ toString: function() { return "override-object-overriden" }})).toString())
jlaskey@3 90 print("oo-overridden-equals : " + (new oo({ equals: function() { return true }})).equals({}))
jlaskey@3 91 // Finally, test that equals and hashCode can be overridden with functions from a prototype, but toString() can't:
jlaskey@3 92 function Proto() {
jlaskey@3 93 return this;
jlaskey@3 94 }
jlaskey@3 95 Proto.prototype = {
jlaskey@3 96 toString: function() { return "this-will-never-be-seen" }, // toString only overridden when it's own property, never from prototype
jlaskey@3 97 equals: function() { return true },
jlaskey@3 98 hashCode: function() { return 7 }
jlaskey@3 99 }
jlaskey@3 100 print("oo-proto-overridden-hashCode: " + (new oo(new Proto())).hashCode())
jlaskey@3 101 print("oo-proto-overridden-toString: " + (new oo(new Proto())).toString())
jlaskey@3 102 print("oo-proto-overridden-equals : " + (new oo(new Proto())).equals({}))
jlaskey@3 103
jlaskey@3 104 // Subclass a class with a protected constructor, and one that takes
jlaskey@3 105 // additional constructor arguments (a token). Also demonstrates how can
jlaskey@3 106 // you access the Java adapter instance from the script (just store it in the
jlaskey@3 107 // scope, in this example, "cwa") to retrieve the token later on.
attila@23 108 var cwa = new (Java.extend(model("ConstructorWithArgument")))("cwa-token", function() { print(cwa.token) })
jlaskey@3 109 cwa.doSomething()
attila@23 110
attila@23 111 // Do the same thing with proprietary syntax and object literal
attila@23 112 var cwa2 = new (model("ConstructorWithArgument"))("cwa2-token") { doSomething: function() { print("cwa2-" + cwa2.token ) } }
attila@23 113 cwa2.doSomething()
attila@27 114
attila@27 115 // Implement two interfaces
attila@27 116 var desertToppingAndFloorWax = new (Java.extend(model("DessertTopping"), model("FloorWax"))) {
attila@27 117 pourOnDessert: function() { print("Glop; IM IN UR DESSERT NOW") },
attila@27 118 shineUpTheFloor: function() { print("The floor sure is shining!") }
attila@27 119 }
attila@27 120 var dtfwDriver = new (model("DessertToppingFloorWaxDriver"))
attila@27 121 dtfwDriver.decorateDessert(desertToppingAndFloorWax)
attila@27 122 dtfwDriver.waxFloor(desertToppingAndFloorWax)
attila@27 123
attila@27 124 // Extend a class and implement two interfaces. For additional measure, put the class in between the two interfaces
attila@27 125 var desertToppingFloorWaxAndToothpaste = new (Java.extend(model("DessertTopping"), model("Toothpaste"), model("FloorWax"))) {
attila@27 126 pourOnDessert: function() { print("Yum") },
attila@27 127 shineUpTheFloor: function() { print("Scrub, scrub, scrub") },
attila@27 128 applyToBrushImpl: function() { print("It's a dessert topping! It's a floor wax! It's a toothpaste!") }
attila@27 129 }
attila@27 130 dtfwDriver.decorateDessert(desertToppingFloorWaxAndToothpaste)
attila@27 131 dtfwDriver.waxFloor(desertToppingFloorWaxAndToothpaste)
attila@27 132 desertToppingFloorWaxAndToothpaste.applyToBrush();

mercurial