synchronized Object getPkgProperty(String name, Scriptable start, boolean createPkg) { Object cached = super.get(name, start); if (cached != NOT_FOUND) return cached; if (negativeCache != null && negativeCache.contains(name)) { // Performance optimization: see bug 421071 return null; } String className = (packageName.length() == 0) ? name : packageName + '.' + name; Context cx = Context.getContext(); ClassShutter shutter = cx.getClassShutter(); Scriptable newValue = null; if (shutter == null || shutter.visibleToScripts(className)) { Class<?> cl = null; if (classLoader != null) { cl = Kit.classOrNull(classLoader, className); } else { cl = Kit.classOrNull(className); } if (cl != null) { WrapFactory wrapFactory = cx.getWrapFactory(); newValue = wrapFactory.wrapJavaClass(cx, getTopLevelScope(this), cl); newValue.setPrototype(getPrototype()); } } if (newValue == null) { if (createPkg) { NativeJavaPackage pkg; pkg = new NativeJavaPackage(true, className, classLoader); ScriptRuntime.setObjectProtoAndParent(pkg, getParentScope()); newValue = pkg; } else { // add to negative cache if (negativeCache == null) negativeCache = new HashSet<String>(); negativeCache.add(name); } } if (newValue != null) { // Make it available for fast lookup and sharing of // lazily-reflected constructors and static members. super.put(name, start, newValue); } return newValue; }
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. } }
static { // Reflection in java is verbose Class<?>[] sig2 = new Class[2]; Class<?> cl = Kit.classOrNull("org.mozilla.javascript.JavaAdapter"); if (cl != null) { try { sig2[0] = ScriptRuntime.ObjectClass; sig2[1] = Kit.classOrNull("java.io.ObjectOutputStream"); adapter_writeAdapterObject = cl.getMethod("writeAdapterObject", sig2); sig2[0] = ScriptRuntime.ScriptableClass; sig2[1] = Kit.classOrNull("java.io.ObjectInputStream"); adapter_readAdapterObject = cl.getMethod("readAdapterObject", sig2); } catch (Exception ex) { adapter_writeAdapterObject = null; adapter_readAdapterObject = null; } } }
/** Check that testClass is accessible 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; }
private boolean isDom3Present() { Class<?> nodeClass = Kit.classOrNull("org.w3c.dom.Node"); if (nodeClass == null) return false; // Check to see whether DOM3 is present; use a new method defined in // DOM3 that is vital to our implementation try { nodeClass.getMethod("getUserData", new Class<?>[] {String.class}); return true; } catch (NoSuchMethodException e) { return false; } }
/** * Provides a default {@link org.mozilla.javascript.xml.XMLLib.Factory XMLLib.Factory} to be used * by the <code>Context</code> instances produced by this factory. See {@link * Context#getE4xImplementationFactory} for details. * * <p>May return null, in which case E4X functionality is not supported in Rhino. * * <p>The default implementation now prefers the DOM3 E4X implementation. */ protected org.mozilla.javascript.xml.XMLLib.Factory getE4xImplementationFactory() { // Must provide default implementation, rather than abstract method, // so that past implementors of ContextFactory do not fail at runtime // upon invocation of this method. // Note that the default implementation returns null if we // neither have XMLBeans nor a DOM3 implementation present. if (isDom3Present()) { return org.mozilla.javascript.xml.XMLLib.Factory.create( "org.mozilla.javascript.xmlimpl.XMLLibImpl"); } else if (Kit.classOrNull("org.apache.xmlbeans.XmlCursor") != null) { return org.mozilla.javascript.xml.XMLLib.Factory.create( "org.mozilla.javascript.xml.impl.xmlbeans.XMLLibImpl"); } else { return null; } }