📄 kit.java
字号:
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape 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/NPL/ * * 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 Netscape are * Copyright (C) 1997-1999 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * Igor Bukanov, igor@fastmail.fm * * Alternatively, the contents of this file may be used under the * terms of the GNU Public License (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 NPL, indicate your decision by * deleting the provisions above and replace 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 NPL or the GPL. */package org.mozilla.javascript;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.lang.reflect.Method;import java.util.Hashtable;/** * Collection of utilities */public class Kit{ /** * Reflection of Throwable.initCause(Throwable) from JDK 1.4 * or nul if it is not available. */ private static Method Throwable_initCause = null; static { // Are we running on a JDK 1.4 or later system? try { Class ThrowableClass = Kit.classOrNull("java.lang.Throwable"); Class[] signature = { ThrowableClass }; Throwable_initCause = ThrowableClass.getMethod("initCause", signature); } catch (Exception ex) { // Assume any exceptions means the method does not exist. } } public static Class classOrNull(String className) { try { return Class.forName(className); } catch (ClassNotFoundException ex) { } catch (SecurityException ex) { } catch (LinkageError ex) { } catch (IllegalArgumentException e) { // Can be thrown if name has characters that a class name // can not contain } return null; } public static Class classOrNull(ClassLoader loader, String className) { try { return loader.loadClass(className); } catch (ClassNotFoundException ex) { } catch (SecurityException ex) { } catch (LinkageError ex) { } catch (IllegalArgumentException e) { // Can be thrown if name has characters that a class name // can not contain } return null; } static Object newInstanceOrNull(Class cl) { try { return cl.newInstance(); } catch (SecurityException x) { } catch (LinkageError ex) { } catch (InstantiationException x) { } catch (IllegalAccessException x) { } return null; } /** * Check that testClass is accesible from the given loader. */ static boolean testIfCanLoadRhinoClasses(ClassLoader loader) { Class testClass = ScriptRuntime.ContextFactoryClass; Class x = Kit.classOrNull(loader, testClass.getName()); if (x != testClass) { // The check covers the case when x == null => // loader does not know about testClass or the case // when x != null && x != testClass => // loader loads a class unrelated to testClass return false; } return true; } /** * If initCause methods exists in Throwable, call * <tt>ex.initCause(cause)</tt> or otherwise do nothing. * @return The <tt>ex</tt> argument. */ public static RuntimeException initCause(RuntimeException ex, Throwable cause) { if (Throwable_initCause != null) { Object[] args = { cause }; try { Throwable_initCause.invoke(ex, args); } catch (Exception e) { // Ignore any exceptions } } return ex; } /** * Split string into array of strings using semicolon as string terminator * (; after the last string is required). */ public static String[] semicolonSplit(String s) { String[] array = null; for (;;) { // loop 2 times: first to count semicolons and then to fill array int count = 0; int cursor = 0; for (;;) { int next = s.indexOf(';', cursor); if (next < 0) { break; } if (array != null) { array[count] = s.substring(cursor, next); } ++count; cursor = next + 1; } // after the last semicolon if (array == null) { // array size counting state: // check for required terminating ';' if (cursor != s.length()) throw new IllegalArgumentException(); array = new String[count]; } else { // array filling state: stop the loop break; } } return array; } /** * If character <tt>c</tt> is a hexadecimal digit, return * <tt>accumulator</tt> * 16 plus corresponding * number. Otherise return -1. */ public static int xDigitToInt(int c, int accumulator) { check: { // Use 0..9 < A..Z < a..z if (c <= '9') { c -= '0'; if (0 <= c) { break check; } } else if (c <= 'F') { if ('A' <= c) { c -= ('A' - 10); break check; } } else if (c <= 'f') { if ('a' <= c) { c -= ('a' - 10); break check; } } return -1; } return (accumulator << 4) | c; } /** * Add <i>listener</i> to <i>bag</i> of listeners. * The function does not modify <i>bag</i> and return a new collection * containing <i>listener</i> and all listeners from <i>bag</i>. * Bag without listeners always represented as the null value. * <p> * Usage example: * <pre> * private volatile Object changeListeners; * * public void addMyListener(PropertyChangeListener l) * { * synchronized (this) { * changeListeners = Kit.addListener(changeListeners, l); * } * } * * public void removeTextListener(PropertyChangeListener l) * { * synchronized (this) { * changeListeners = Kit.removeListener(changeListeners, l); * } * } * * public void fireChangeEvent(Object oldValue, Object newValue) * { * // Get immune local copy * Object listeners = changeListeners; * if (listeners != null) { * PropertyChangeEvent e = new PropertyChangeEvent( * this, "someProperty" oldValue, newValue); * for (int i = 0; ; ++i) { * Object l = Kit.getListener(listeners, i); * if (l == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -