001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-2000
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: *
026: * Alternatively, the contents of this file may be used under the terms of
027: * the GNU General Public License Version 2 or later (the "GPL"), in which
028: * case the provisions of the GPL are applicable instead of those above. If
029: * you wish to allow use of your version of this file only under the terms of
030: * the GPL and not to allow others to use your version of this file under the
031: * MPL, indicate your decision by deleting the provisions above and replacing
032: * them with the notice and other provisions required by the GPL. If you do
033: * not delete the provisions above, a recipient may use your version of this
034: * file under either the MPL or the GPL.
035: *
036: * ***** END LICENSE BLOCK ***** */
037:
038: // API class
039: package org.mozilla.javascript;
040:
041: import java.lang.reflect.Method;
042: import java.lang.reflect.Member;
043: import java.util.Iterator;
044:
045: public abstract class VMBridge {
046:
047: static final VMBridge instance = makeInstance();
048:
049: private static VMBridge makeInstance() {
050: String[] classNames = {
051: "org.mozilla.javascript.VMBridge_custom",
052: "org.mozilla.javascript.jdk15.VMBridge_jdk15",
053: "org.mozilla.javascript.jdk13.VMBridge_jdk13",
054: "org.mozilla.javascript.jdk11.VMBridge_jdk11", };
055: for (int i = 0; i != classNames.length; ++i) {
056: String className = classNames[i];
057: Class cl = Kit.classOrNull(className);
058: if (cl != null) {
059: VMBridge bridge = (VMBridge) Kit.newInstanceOrNull(cl);
060: if (bridge != null) {
061: return bridge;
062: }
063: }
064: }
065: throw new IllegalStateException(
066: "Failed to create VMBridge instance");
067: }
068:
069: /**
070: * Return a helper object to optimize {@link Context} access.
071: * <p>
072: * The runtime will pass the resulting helper object to the subsequent
073: * calls to {@link #getContext(Object contextHelper)} and
074: * {@link #setContext(Object contextHelper, Context cx)} methods.
075: * In this way the implementation can use the helper to cache
076: * information about current thread to make {@link Context} access faster.
077: */
078: protected abstract Object getThreadContextHelper();
079:
080: /**
081: * Get {@link Context} instance associated with the current thread
082: * or null if none.
083: *
084: * @param contextHelper The result of {@link #getThreadContextHelper()}
085: * called from the current thread.
086: */
087: protected abstract Context getContext(Object contextHelper);
088:
089: /**
090: * Associate {@link Context} instance with the current thread or remove
091: * the current association if <tt>cx</tt> is null.
092: *
093: * @param contextHelper The result of {@link #getThreadContextHelper()}
094: * called from the current thread.
095: */
096: protected abstract void setContext(Object contextHelper, Context cx);
097:
098: /**
099: * Return the ClassLoader instance associated with the current thread.
100: */
101: protected abstract ClassLoader getCurrentThreadClassLoader();
102:
103: /**
104: * In many JVMSs, public methods in private
105: * classes are not accessible by default (Sun Bug #4071593).
106: * VMBridge instance should try to workaround that via, for example,
107: * calling method.setAccessible(true) when it is available.
108: * The implementation is responsible to catch all possible exceptions
109: * like SecurityException if the workaround is not available.
110: *
111: * @return true if it was possible to make method accessible
112: * or false otherwise.
113: */
114: protected abstract boolean tryToMakeAccessible(
115: Object accessibleObject);
116:
117: /**
118: * Create helper object to create later proxies implementing the specified
119: * interfaces later. Under JDK 1.3 the implementation can look like:
120: * <pre>
121: * return java.lang.reflect.Proxy.getProxyClass(..., interfaces).
122: * getConstructor(new Class[] {
123: * java.lang.reflect.InvocationHandler.class });
124: * </pre>
125: *
126: * @param interfaces Array with one or more interface class objects.
127: */
128: protected Object getInterfaceProxyHelper(ContextFactory cf,
129: Class[] interfaces) {
130: throw Context
131: .reportRuntimeError("VMBridge.getInterfaceProxyHelper is not supported");
132: }
133:
134: /**
135: * Create proxy object for {@link InterfaceAdapter}. The proxy should call
136: * {@link InterfaceAdapter#invoke(ContextFactory cf,
137: * Object target,
138: * Scriptable topScope,
139: * Method method,
140: * Object[] args)}
141: * as implementation of interface methods associated with
142: * <tt>proxyHelper</tt>.
143: *
144: * @param proxyHelper The result of the previous call to
145: * {@link #getInterfaceProxyHelper(ContextFactory, Class[])}.
146: */
147: protected Object newInterfaceProxy(Object proxyHelper,
148: ContextFactory cf, InterfaceAdapter adapter, Object target,
149: Scriptable topScope) {
150: throw Context
151: .reportRuntimeError("VMBridge.newInterfaceProxy is not supported");
152: }
153:
154: /**
155: * Returns whether or not a given member (method or constructor)
156: * has variable arguments.
157: * Variable argument methods have only been supported in Java since
158: * JDK 1.5.
159: */
160: protected abstract boolean isVarArgs(Member member);
161:
162: /**
163: * If "obj" is a java.util.Iterator or a java.lang.Iterable, return a
164: * wrapping as a JavaScript Iterator. Otherwise, return null.
165: * This method is in VMBridge since Iterable is a JDK 1.5 addition.
166: */
167: public Iterator getJavaIterator(Context cx, Scriptable scope,
168: Object obj) {
169: if (obj instanceof Wrapper) {
170: Object unwrapped = ((Wrapper) obj).unwrap();
171: Iterator iterator = null;
172: if (unwrapped instanceof Iterator)
173: iterator = (Iterator) unwrapped;
174: return iterator;
175: }
176: return null;
177: }
178: }
|