
org.mozilla.javascript.ImporterTopLevel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rhino Show documentation
Show all versions of rhino Show documentation
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically
embedded into Java applications to provide scripting to end users.
The newest version!
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// API class
package org.mozilla.javascript;
import java.util.ArrayList;
/**
* Class ImporterTopLevel
*
* This class defines a ScriptableObject that can be instantiated as a top-level ("global")
* object to provide functionality similar to Java's "import" statement.
*
*
This class can be used to create a top-level scope using the following code:
*
*
* Scriptable scope = new ImporterTopLevel(cx);
*
*
* Then JavaScript code will have access to the following methods:
*
*
* - importClass - will "import" a class by making its unqualified name available as a property
* of the top-level scope
*
- importPackage - will "import" all the classes of the package by searching for unqualified
* names as classes qualified by the given package.
*
*
* The following code from the shell illustrates this use:
*
*
* js> importClass(java.io.File)
* js> f = new File('help.txt')
* help.txt
* js> importPackage(java.util)
* js> v = new Vector()
* []
*
*
* @author Norris Boyd
*/
public class ImporterTopLevel extends TopLevel {
private static final long serialVersionUID = -9095380847465315412L;
private static final Object IMPORTER_TAG = "Importer";
public ImporterTopLevel() {}
public ImporterTopLevel(Context cx) {
this(cx, false);
}
public ImporterTopLevel(Context cx, boolean sealed) {
initStandardObjects(cx, sealed);
}
@Override
public String getClassName() {
return topScopeFlag ? "global" : "JavaImporter";
}
public static void init(Context cx, Scriptable scope, boolean sealed) {
ImporterTopLevel obj = new ImporterTopLevel();
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
}
public void initStandardObjects(Context cx, boolean sealed) {
// Assume that Context.initStandardObjects initialize JavaImporter
// property lazily so the above init call is not yet called
cx.initStandardObjects(this, sealed);
topScopeFlag = true;
// If seal is true then exportAsJSClass(cx, seal) would seal
// this obj. Since this is scope as well, it would not allow
// to add variables.
IdFunctionObject ctor = exportAsJSClass(MAX_PROTOTYPE_ID, this, false);
if (sealed) {
ctor.sealObject();
}
// delete "constructor" defined by exportAsJSClass so "constructor"
// name would refer to Object.constructor
// and not to JavaImporter.prototype.constructor.
delete("constructor");
}
@Override
public boolean has(String name, Scriptable start) {
return super.has(name, start) || getPackageProperty(name, start) != NOT_FOUND;
}
@Override
public Object get(String name, Scriptable start) {
Object result = super.get(name, start);
if (result != NOT_FOUND) return result;
result = getPackageProperty(name, start);
return result;
}
private Object getPackageProperty(String name, Scriptable start) {
Object result = NOT_FOUND;
Scriptable scope = start;
if (topScopeFlag) {
scope = ScriptableObject.getTopLevelScope(scope);
}
Object[] elements = getNativeJavaPackages(scope);
if (elements == null) {
return result;
}
for (Object element : elements) {
NativeJavaPackage p = (NativeJavaPackage) element;
Object v = p.getPkgProperty(name, start, false);
if (v != null && !(v instanceof NativeJavaPackage)) {
if (result == NOT_FOUND) {
result = v;
} else {
throw Context.reportRuntimeErrorById(
"msg.ambig.import", result.toString(), v.toString());
}
}
}
return result;
}
private static Object[] getNativeJavaPackages(Scriptable scope) {
// retrivee the native java packages stored in top scope.
synchronized (scope) {
if (scope instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject) scope;
@SuppressWarnings("unchecked")
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy