Forum

Forum Navigation
You need to log in to create posts and topics.

How to reload class?

Hi,

I'm trying to find a way to reload previously loaded class, which may have been updated on file system.

I tried clearing cache between scans, but with no luck. I'm using something like:

// Load classes
ComponentContainer.getInstance().getClassHunter().loadInCache(...).find().getClasses();

ComponentContainer.getInstance().clear();
ComponentContainer.getInstance().clearCache();
ComponentContainer.clearAllCaches();

// Attempt to reload classes, which have been updated on file system
ComponentContainer.getInstance().getClassHunter().loadInCache(...).find().getClasses();

Am I doing something wrong or it isn't possible to reload updated version of a class?

 

Thanks

 

It turned out that I wasn't able to get an updated version of class, because accidentally the class was also in the Java's classpath. Once I leave it only on a custom location, the above code works OK.

I guess the issue was because (possibly) PathScannerClassLoader first asks the parent classloader for the class...

Sorry for the noise πŸ™‚

The method ComponentContainer.getInstance().clear() will destroy all components and, for example, the next call to ComponentContainer.getInstance().getClassHunter() will create a new ClassHunter.

The method ComponentContainer.getInstance().clearCache() will recreate the PathScannerClassLoader inside the ClassHunter and clear all cached items loaded by the ClassHunter, ClassPathHunter and ByteCodeHunter and also the cached files;

In your case you should simply call the method ComponentContainer.getInstance().clearCache() without calling the other two methods, but if the ClassLoader of your class you want to reload is the ClassLoader obtained by the method Thread.currentThread().getContextClassLoader() there is no way to reload it on this class loader. You can try, after called ComponentContainer.getInstance().clearCache(), Β to use the method useNewIsolatedClassLoader() on SearchConfig (here you can find junits test as example): it executes a scan with a new PathScannerClassLoader that has no parent, but after that I think is not very simple to use these classes: if you have troubles to use these classes you can try to set this PathScannerClassLoader as the parent class loader of the Thread.currentThread().getContextClassLoader() or as a master class loader with the Classes.Loaders component (here junit test you can use as examples): so the PathScannerClassLoader will be the first to be interpellated for class loading. This is a very experimental way

Quote from grigoriliev on 23 June 2020, 16:13

It turned out that I wasn't able to get an updated version of class, because accidentally the class was also in the Java's classpath. Once I leave it only on a custom location, the above code works OK.

I guess the issue was because (possibly) PathScannerClassLoader first asks the parent classloader for the class...

Sorry for the noise πŸ™‚

Don't worry. The default parent class loader of the PathScannerClassLoader of the ClassHunter is the class loader obtained by the Thread.currentThread().getContextClassLoader(), but you can change it to null or with another ClassLoader in the burningwave.properties file: the parameter is 'path-scanner-class-loader.parent' (take a look here) or you can use, as I mentioned in the previous post, an isolated PathScannerClassLoader through calling the method useNewIsolatedClassLoader() on SearchConfig. If you choose to set the 'path-scanner-class-loader.parent' property of the burningwave.properties file you must use Java code: it will be executed by the CodeExecutor

Thanks for the information!