Subj : Re: Rhino not wrapping java.lang.Class into NativeJavaClass object To : Jeremy Kwiatkowski From : Igor Bukanov Date : Fri May 02 2003 09:50 pm Jeremy Kwiatkowski wrote: > Hello, > > I'm integrating Rhino into an application that supports dynamic class > loading through an application defined class loader. Then, I want to call a > static method that may be defined on the dynamically loaded class. > > I put the class factory into the scope, so it's available like so: > > var clazz = classFactory.getClass("some.package.name"); > > If I print out clazz, I see that it is indeed a Java Class object. > Unfortunately, it seems to be wrapped in a JavaNativeObject, rather than a > Java NativeClass object. Therefore, this fails: > > clazz.invokeSomeStaticMethod(); Try to use a new option in Rhino 1.5R4 to use liveconnect against arbitrary classloader, see http://mozilla.org/rhino/rhino15R4.html . In this way you will need to make available the class loader itself for scripts, not class factory: var loaderWrapper = new Packages(customClassLoader); var clazz = loaderWrapper.some.package.name; clazz.invokeSomeStaticMethod(); > > I can fix the problem either by performing the wrapping manually, or, with > the the following WrapFactory: > > public class Wrapper extends org.mozilla.javascript.WrapFactory { > public Object wrap(Context cx, > Scriptable scope, > Object obj, > Class staticType) { > if (obj instanceof Class) { > return new NativeJavaClass(scope, (Class)obj); > } else { > return super.wrap(cx, scope, obj, staticType); > } > } > } > > What confuses me, though, is that there were some messages on this topic in > the 1999 timeframe. What I'm trying to understand is whether this feature > shouldn't be used (for some reason I don't understand), was just lost, is a > known issue, is there some other way I should be doing this?, etc. If you do it like this, you will lose an option to script the Class instances itself since Class methods would not be availbale through NativeJavaClass wrapper. But if it is OK with you, your WrapFactory should work since that discussion was probably about default Rhino behavior and custom WrapFactory was intriduced to give an option to override the default policy. Note that you may also want to call Context.setCachingEnabled(false) otherwise your classes from the dynamic loader will never be garbage collected. Regards, Igor .