Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/sandbox/javaextend.js
32281 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @run26*/2728function model(n) {29return Java.type("jdk.nashorn.test.models." + n)30}3132// Can't extend a final class33try {34Java.extend(model("FinalClass"))35} catch(e) {36print(e)37}3839// Can't extend a class with no public or protected constructor40try {41Java.extend(model("NoAccessibleConstructorClass"))42} catch(e) {43print(e)44}4546// Can't extend a non-public class47try {48Java.extend(model("NonPublicClass"))49} catch(e) {50print(e)51}5253// Can't extend a class with explicit non-overridable finalizer54try {55Java.extend(model("ClassWithFinalFinalizer"))56} catch(e) {57print(e)58}5960// Can't extend a class with inherited non-overridable finalizer61try {62Java.extend(model("ClassWithInheritedFinalFinalizer"))63} catch(e) {64print(e)65}666768// Can't extend two classes69try {70Java.extend(java.lang.Thread,java.lang.Number)71} catch(e) {72print(e)73}7475// Make sure we can implement interfaces from the unnamed package76var c = new (Java.extend(Java.type("UnnamedPackageTestCallback")))() { call: function(s) { return s + s } }77print(c.call("abcd"))7879// Basic Runnable from an object80new (Java.extend(java.lang.Runnable))({ run: function() { print("run-object") } }).run()8182// Basic Runnable from a function83new (Java.extend(java.lang.Runnable))(function() { print("run-fn") }).run()8485// Basic Runnable from an autoconverted function86var t = new java.lang.Thread(function() { print("run-fn-autoconvert") })87t.start()88t.join()8990// SAM conversion should work on overloaded methods of same name91var os = new (Java.extend(model("OverloadedSam")))(function(s1, s2) { print("overloaded-sam: " + s1 + ", " + s2) })92os.sam("x")93os.sam("x", "y")9495// Test overriding of hashCode, equals, and toString96var oo = Java.extend(model("OverrideObject"))97// First, see non-overridden values98print("oo-plain-hashCode: " + (new oo({})).hashCode())99print("oo-plain-toString: " + (new oo({})).toString())100print("oo-plain-equals : " + (new oo({})).equals({}))101// Now, override them102print("oo-overridden-hashCode: " + (new oo({ hashCode: function() { return 6 }})).hashCode())103print("oo-overridden-toString: " + (new oo({ toString: function() { return "override-object-overriden" }})).toString())104print("oo-overridden-equals : " + (new oo({ equals: function() { return true }})).equals({}))105// Finally, test that equals and hashCode can be overridden with functions from a prototype, but toString() can't:106function Proto() {107return this;108}109Proto.prototype = {110toString: function() { return "this-will-never-be-seen" }, // toString only overridden when it's own property, never from prototype111equals: function() { return true },112hashCode: function() { return 7 }113}114print("oo-proto-overridden-hashCode: " + (new oo(new Proto())).hashCode())115print("oo-proto-overridden-toString: " + (new oo(new Proto())).toString())116print("oo-proto-overridden-equals : " + (new oo(new Proto())).equals({}))117118// Subclass a class with a protected constructor, and one that takes119// additional constructor arguments (a token). Also demonstrates how can120// you access the Java adapter instance from the script (just store it in the121// scope, in this example, "cwa") to retrieve the token later on.122var cwa = new (Java.extend(model("ConstructorWithArgument")))("cwa-token", function() { print(cwa.token) })123cwa.doSomething()124125// Do the same thing with proprietary syntax and object literal126var cwa2 = new (model("ConstructorWithArgument"))("cwa2-token") { doSomething: function() { print("cwa2-" + cwa2.token ) } }127cwa2.doSomething()128129// Implement two interfaces130var desertToppingAndFloorWax = new (Java.extend(model("DessertTopping"), model("FloorWax"))) {131pourOnDessert: function() { print("Glop; IM IN UR DESSERT NOW") },132shineUpTheFloor: function() { print("The floor sure is shining!") }133}134var dtfwDriver = new (model("DessertToppingFloorWaxDriver"))135dtfwDriver.decorateDessert(desertToppingAndFloorWax)136dtfwDriver.waxFloor(desertToppingAndFloorWax)137138// Extend a class and implement two interfaces. For additional measure, put the class in between the two interfaces139var desertToppingFloorWaxAndToothpaste = new (Java.extend(model("DessertTopping"), model("Toothpaste"), model("FloorWax"))) {140pourOnDessert: function() { print("Yum") },141shineUpTheFloor: function() { print("Scrub, scrub, scrub") },142applyToBrushImpl: function() { print("It's a dessert topping! It's a floor wax! It's a toothpaste!") }143}144dtfwDriver.decorateDessert(desertToppingFloorWaxAndToothpaste)145dtfwDriver.waxFloor(desertToppingFloorWaxAndToothpaste)146desertToppingFloorWaxAndToothpaste.applyToBrush();147148149