Subj : Re: How to use methods importClass and importPackage of class ImporterTopLevel? To : "S. Hasan Rizvi" From : Igor Bukanov Date : Thu Jul 17 2003 11:14 pm S. Hasan Rizvi wrote: > Hi : > > I am using Rhino1.5 release 4.1 and I have a doubt about the usage of > methods importClass and importPackage of class > ImporterTopLevel. > > I had a look at the api docs both methods expect 4 arguments. Also > ApiDocs of these methods are no proper and don't give much > detail. > > The API doc of the class has following explaination: > > *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()* > * []* > > Reading this explaination leavs lots of question marks, about the > 1)argumets required to use the methods(importClass and importPackage) The methods are intended to be called from scripts, not directly from java code. To make the methods available for scripts that you call from your program you have to change a scope initialization code from: Scriptable scope = cx.initStandardObjects(null); to Scriptable scope = new ImporterTopLevel(cx); since the methods are not available as a part of the standard library. When you call the methods from scripts, you have to pass as argument a special object representing Java class or package in Rhino. Typically to get that you simply write: Packages. like in Packages.org.w3c.dom.Node or if the class or package matches java. *, the you can ommit Packages, like in the above example: java.io.File > 2)What is code from the shell?? See http://mozilla.org/rhino/shell.html Since the shell uses extended version of ImporterTopLevel, the methods are directly available for it. > > Do we have the overloaded version of these methods which just expects > only one string Package/ClassName as agrument? No, but why do you need it? Since importClass/importPackage is useful to avoid ever typing full package name when accessing a KNOWN class, then IMO importPackage(Packages.com.foo.bar) is just as conviniet as importPackage('com.foo.bar') Regards, Igor .