Re: Reloading class dynamically

>Alexandre Rafalovitch wrote:
>>
>> There is no dinamicall reloading of the Java classes. This has to do with
>> nature of Java Virtual Machine.
>
>Are you certain of this? I don't see what prevents an application from
>explicitly reloading a class.
>

I am 95% certain of this.

There are two ways (that I know) to load a class. One is to go
Class.forName(String name) and another is loadClass(String name, boolean
resolve) on instance of ClassLoader. Also, ClassLoader is called implicitly
when a method of a class is called for the first time during run. This last
reason has to do with quick instructions of JVM and late dynamic binding.
The gory details are available at sunsoft site.

The first call would most probably go getClassLoader().loadClass(name,
true), so we are back to ClassLoader.

Now, the current implementation of the ClassLoader goes through the
following steps (courtesy of Javaworld October article
<http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html>)


1)       Verify class name.
2)       Check to see if the class requested has already been loaded.
3)       Check to see if the class is a "system" class.
4)       Attempt to fetch the class from this class loader's repository.
5)       Define the class for the VM.
6)       Resolve the class.
7)       Return the class to the caller.


As you can see, if the file is already loaded, class loader would stop at
the second step and would not even think about checking if the file was
updated. That would prevent reloading the new version of the class. If this
mechanism did not exist, there would be a filesystem check or even worse
HTTP connection every time a method if an object is referenced for the
first time.

This is where my 95% are coming from as well as from the fact that I am
following Java news closely but did not see 'how to reload a class in an
application/applet' in FAQ answers.

My 5% are coming from the fact that it might be possible to define a class
loader that would have additional methods allowing to mark which classes
should be reloaded and when the next reference to those classes is done,
ClassLoader would get the new copy. However, this would most probably stuff
up the static registration mechanism, Jigsaw would have to be restarted and
probably some other nasty things I did not think about yet.

Hope that explained my answer,
    Alex.

Ps. If there are people on this list (or people on this list who know
people) who delved into the depths of class loaders and associated issues
and know how to add class reloading to Jigsaw, then I and most probably the
rest of the Jigsaw developers would be very anxious to hear about it. :-}

alex@access.com.au

Received on Tuesday, 17 September 1996 02:10:00 UTC