interface2.js revision 970:f82b83cf73ae
1130803Smarcel#// Usage: jjs -scripting interface2.js 2130803Smarcel 3130803Smarcel/* 4130803Smarcel * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 5130803Smarcel * 6130803Smarcel * Redistribution and use in source and binary forms, with or without 7130803Smarcel * modification, are permitted provided that the following conditions 8130803Smarcel * are met: 9130803Smarcel * 10130803Smarcel * - Redistributions of source code must retain the above copyright 11130803Smarcel * notice, this list of conditions and the following disclaimer. 12130803Smarcel * 13130803Smarcel * - Redistributions in binary form must reproduce the above copyright 14130803Smarcel * notice, this list of conditions and the following disclaimer in the 15130803Smarcel * documentation and/or other materials provided with the distribution. 16130803Smarcel * 17130803Smarcel * - Neither the name of Oracle nor the names of its 18130803Smarcel * contributors may be used to endorse or promote products derived 19130803Smarcel * from this software without specific prior written permission. 20130803Smarcel * 21130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22130803Smarcel * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23130803Smarcel * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24130803Smarcel * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 25130803Smarcel * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26130803Smarcel * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27130803Smarcel * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28130803Smarcel * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29130803Smarcel * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30130803Smarcel * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31130803Smarcel * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32130803Smarcel */ 33130803Smarcel 34130803Smarcel// Simple example demonstrating how to implement java interface 35130803Smarcel// whose methods are backed by script methods of a script object 36130803Smarcel 37130803Smarcelvar ScriptEngineManager = Java.type("javax.script.ScriptEngineManager"); 38130803Smarcel// create manager 39130803Smarcelvar manager = new ScriptEngineManager(); 40130803Smarcel// create engine 41130803Smarcelvar engine = manager.getEngineByName("js"); 42130803Smarcel 43130803Smarcel// eval code - too many script escapes? 44130803Smarcel// use heredoc ! 45130803Smarcelengine.eval(<<CODE 46130803Smarcel var obj = { 47130803Smarcel run: function() { 48130803Smarcel print("I am run method of 'obj'"); 49130803Smarcel } 50130803Smarcel }; 51130803SmarcelCODE); 52130803Smarcel 53130803Smarcel// create Java interface object whose methods are 54130803Smarcel// implemented by script methods of a script object 55130803Smarcel// This is from javax.script.Invocable. But we are 56130803Smarcel// in JS world, don't worry about types :) 57130803Smarcel 58130803Smarcelvar Runnable = Java.type("java.lang.Runnable"); 59130803Smarcel 60130803Smarcelvar scriptObj = engine.get("obj"); 61130803Smarcelvar r = engine.getInterface(scriptObj, Runnable.class); 62130803Smarcelprint(r.class); 63130803Smarcelr.run(); 64130803Smarcel