📄 nativejavamethod.java
字号:
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Rhino code, released * May 6, 1999. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1997-1999 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Norris Boyd * Frank Mitchell * Mike Shaver * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (the "GPL"), in which * case the provisions of the GPL are applicable instead of those above. If * you wish to allow use of your version of this file only under the terms of * the GPL and not to allow others to use your version of this file under the * MPL, indicate your decision by deleting the provisions above and replacing * them with the notice and other provisions required by the GPL. If you do * not delete the provisions above, a recipient may use your version of this * file under either the MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript;import java.lang.reflect.*;/** * This class reflects Java methods into the JavaScript environment and * handles overloading of methods. * * @author Mike Shaver * @see NativeJavaArray * @see NativeJavaPackage * @see NativeJavaClass */public class NativeJavaMethod extends BaseFunction{ static final long serialVersionUID = -3440381785576412928L; NativeJavaMethod(MemberBox[] methods) { this.functionName = methods[0].getName(); this.methods = methods; } NativeJavaMethod(MemberBox method, String name) { this.functionName = name; this.methods = new MemberBox[] { method }; } public NativeJavaMethod(Method method, String name) { this(new MemberBox(method), name); } public String getFunctionName() { return functionName; } static String scriptSignature(Object[] values) { StringBuffer sig = new StringBuffer(); for (int i = 0; i != values.length; ++i) { Object value = values[i]; String s; if (value == null) { s = "null"; } else if (value instanceof Boolean) { s = "boolean"; } else if (value instanceof String) { s = "string"; } else if (value instanceof Number) { s = "number"; } else if (value instanceof Scriptable) { if (value instanceof Undefined) { s = "undefined"; } else if (value instanceof Wrapper) { Object wrapped = ((Wrapper)value).unwrap(); s = wrapped.getClass().getName(); } else if (value instanceof Function) { s = "function"; } else { s = "object"; } } else { s = JavaMembers.javaSignature(value.getClass()); } if (i != 0) { sig.append(','); } sig.append(s); } return sig.toString(); } String decompile(int indent, int flags) { StringBuffer sb = new StringBuffer(); boolean justbody = (0 != (flags & Decompiler.ONLY_BODY_FLAG)); if (!justbody) { sb.append("function "); sb.append(getFunctionName()); sb.append("() {"); } sb.append("/*\n"); sb.append(toString()); sb.append(justbody ? "*/\n" : "*/}\n"); return sb.toString(); } public String toString() { StringBuffer sb = new StringBuffer(); for (int i = 0, N = methods.length; i != N; ++i) { Method method = methods[i].method(); sb.append(JavaMembers.javaSignature(method.getReturnType())); sb.append(' '); sb.append(method.getName()); sb.append(JavaMembers.liveConnectSignature(methods[i].argTypes)); sb.append('\n'); } return sb.toString(); } public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { // Find a method that matches the types given. if (methods.length == 0) { throw new RuntimeException("No methods defined for call"); } int index = findFunction(cx, methods, args); if (index < 0) { Class c = methods[0].method().getDeclaringClass(); String sig = c.getName() + '.' + getFunctionName() + '(' + scriptSignature(args) + ')'; throw Context.reportRuntimeError1("msg.java.no_such_method", sig); } MemberBox meth = methods[index]; Class[] argTypes = meth.argTypes; // First, we marshall the args. Object[] origArgs = args; for (int i = 0; i < args.length; i++) { Object arg = args[i]; Object coerced = Context.jsToJava(arg, argTypes[i]); if (coerced != arg) { if (origArgs == args) { args = (Object[])args.clone(); } args[i] = coerced; } } Object javaObject; if (meth.isStatic()) { javaObject = null; // don't need an object } else { Scriptable o = thisObj; Class c = meth.getDeclaringClass(); for (;;) { if (o == null) { throw Context.reportRuntimeError3( "msg.nonjava.method", getFunctionName(), ScriptRuntime.toString(thisObj), c.getName()); } if (o instanceof Wrapper) { javaObject = ((Wrapper)o).unwrap(); if (c.isInstance(javaObject)) { break; } } o = o.getPrototype(); } } if (debug) { printDebug("Calling ", meth, args); } Object retval = meth.invoke(javaObject, args); Class staticType = meth.method().getReturnType(); if (debug) { Class actualType = (retval == null) ? null : retval.getClass(); System.err.println(" ----- Returned " + retval + " actual = " + actualType + " expect = " + staticType); } Object wrapped = cx.getWrapFactory().wrap(cx, scope, retval, staticType); if (debug) { Class actualType = (wrapped == null) ? null : wrapped.getClass(); System.err.println(" ----- Wrapped as " + wrapped + " class = " + actualType); } if (wrapped == null && staticType == Void.TYPE) { wrapped = Undefined.instance; } return wrapped; } /** * Find the index of the correct function to call given the set of methods * or constructors and the arguments. * If no function can be found to call, return -1. */ static int findFunction(Context cx, MemberBox[] methodsOrCtors, Object[] args) { if (methodsOrCtors.length == 0) { return -1; } else if (methodsOrCtors.length == 1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -