Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* -*- 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;

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(s):
Norris Boyd
 
 public class ImporterTopLevel extends TopLevel {
     static final long serialVersionUID = -9095380847465315412L;
 
     private static final Object IMPORTER_TAG = "Importer";
 
     public ImporterTopLevel() { }
 
     public ImporterTopLevel(Context cx) {
         this(cxfalse);
     }
 
     public ImporterTopLevel(Context cxboolean sealed)
     {
         initStandardObjects(cxsealed);
     }
 
     @Override
     public String getClassName()
     {
         return () ? "global" : "JavaImporter";
     }
 
     public static void init(Context cxScriptable scopeboolean sealed)
     {
         ImporterTopLevel obj = new ImporterTopLevel();
         obj.exportAsJSClass(scopesealed);
     }
 
     public void initStandardObjects(Context cxboolean sealed)
     {
         // Assume that Context.initStandardObjects initialize JavaImporter
         // property lazily so the above init call is not yet called
         cx.initStandardObjects(thissealed);
          = 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(thisfalse);
         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 nameScriptable start) {
         return super.has(namestart)
                || getPackageProperty(namestart) != ;
     }
 
     @Override
     public Object get(String nameScriptable start) {
         Object result = super.get(namestart);
         if (result != )
             return result;
         result = getPackageProperty(namestart);
        return result;
    }
    private Object getPackageProperty(String nameScriptable start) {
        Object result = ;
        Object[] elements;
        synchronized () {
            elements = .toArray();
        }
        for (int i=0; i < elements.lengthi++) {
            NativeJavaPackage p = (NativeJavaPackageelements[i];
            Object v = p.getPkgProperty(namestartfalse);
            if (v != null && !(v instanceof NativeJavaPackage)) {
                if (result == ) {
                    result = v;
                } else {
                    throw Context.reportRuntimeError2(
                        "msg.ambig.import"result.toString(), v.toString());
                }
            }
        }
        return result;
    }

    

Deprecated:
Kept only for compatibility.
    public void importPackage(Context cxScriptable thisObjObject[] args,
                              Function funObj)
    {
        js_importPackage(args);
    }
    private Object js_construct(Scriptable scopeObject[] args)
    {
        ImporterTopLevel result = new ImporterTopLevel();
        for (int i = 0; i != args.length; ++i) {
            Object arg = args[i];
            if (arg instanceof NativeJavaClass) {
                result.importClass((NativeJavaClass)arg);
            } else if (arg instanceof NativeJavaPackage) {
                result.importPackage((NativeJavaPackage)arg);
            } else {
                throw Context.reportRuntimeError1(
                    "msg.not.class.not.pkg", Context.toString(arg));
            }
        }
        // set explicitly prototype and scope
        // as otherwise in top scope mode BaseFunction.construct
        // would keep them set to null. It also allow to use
        // JavaImporter without new and still get properly
        // initialized object.
        result.setParentScope(scope);
        result.setPrototype(this);
        return result;
    }
    private Object js_importClass(Object[] args)
    {
        for (int i = 0; i != args.lengthi++) {
            Object arg = args[i];
            if (!(arg instanceof NativeJavaClass)) {
                throw Context.reportRuntimeError1(
                    "msg.not.class", Context.toString(arg));
            }
            importClass((NativeJavaClass)arg);
        }
        return .;
    }
    private Object js_importPackage(Object[] args)
    {
        for (int i = 0; i != args.lengthi++) {
            Object arg = args[i];
            if (!(arg instanceof NativeJavaPackage)) {
                throw Context.reportRuntimeError1(
                    "msg.not.pkg", Context.toString(arg));
            }
            importPackage((NativeJavaPackage)arg);
        }
        return .;
    }
    private void importPackage(NativeJavaPackage pkg)
    {
        if(pkg == null) {
            return;
        }
        synchronized () {
            for (int j = 0; j != .size(); j++) {
                if (pkg.equals(.get(j))) {
                    return;
                }
            }
            .add(pkg);
        }
    }
    private void importClass(NativeJavaClass cl)
    {
        String s = cl.getClassObject().getName();
        String n = s.substring(s.lastIndexOf('.')+1);
        Object val = get(nthis);
        if (val !=  && val != cl) {
            throw Context.reportRuntimeError1("msg.prop.defined"n);
        }
        //defineProperty(n, cl, DONTENUM);
        put(nthiscl);
    }
    @Override
    protected void initPrototypeId(int id)
    {
        String s;
        int arity;
        switch (id) {
          case :   arity=0; s="constructor";   break;
          case :   arity=1; s="importClass";   break;
          case arity=1; s="importPackage"break;
          defaultthrow new IllegalArgumentException(String.valueOf(id));
        }
        initPrototypeMethod(idsarity);
    }
    @Override
    public Object execIdCall(IdFunctionObject fContext cxScriptable scope,
                             Scriptable thisObjObject[] args)
    {
        if (!f.hasTag()) {
            return super.execIdCall(fcxscopethisObjargs);
        }
        int id = f.methodId();
        switch (id) {
          case :
            return js_construct(scopeargs);
          case :
            return realThis(thisObjf).js_importClass(args);
          case :
            return realThis(thisObjf).js_importPackage(args);
        }
        throw new IllegalArgumentException(String.valueOf(id));
    }
    private ImporterTopLevel realThis(Scriptable thisObjIdFunctionObject f)
    {
        if () {
            // when used as top scope importPackage and importClass are global
            // function that ignore thisObj
            return this;
        }
        if (!(thisObj instanceof ImporterTopLevel))
            throw incompatibleCallError(f);
        return (ImporterTopLevel)thisObj;
    }
// #string_id_map#
    @Override
    protected int findPrototypeId(String s)
    {
        int id;
// #generated# Last update: 2007-05-09 08:15:24 EDT
        L0: { id = 0; String X = nullint c;
            int s_length = s.length();
            if (s_length==11) {
                c=s.charAt(0);
                if (c=='c') { X="constructor";id=; }
                else if (c=='i') { X="importClass";id=; }
            }
            else if (s_length==13) { X="importPackage";id=; }
            if (X!=null && X!=s && !X.equals(s)) id = 0;
            break L0;
        }
// #/generated#
        return id;
    }
    private static final int
        Id_constructor          = 1,
        Id_importClass          = 2,
        Id_importPackage        = 3,
        MAX_PROTOTYPE_ID        = 3;
// #/string_id_map#
    private ObjArray importedPackages = new ObjArray();
    private boolean topScopeFlag;
New to GrepCode? Check out our FAQ X