jeudi 14 décembre 2017

Unloading Java classes using Reflection

I have tried this method of unloading few classes and loading the new version of those classes using Reflection.The code compiled and even during the debug class loader shown that it got populated with new classes.

But the same is not happening later the process.It is still taking the classes from old version of the jar.

Algo used:

    public void perform() throws NoSuchFieldException, IllegalAccessException, IOException, ClassNotFoundException {
        ClassLoader classLoader = getClass().getClassLoader();
// Get the access of classes(private variable) in classloader class and change its previlage. 
        Field f = ClassLoader.class.getDeclaredField("classes");
        f.setAccessible(true);

        Vector<Class> classes = (Vector<Class>) f.get(classLoader);
        List<String> deleteOldclasses = populateOldSet();// Get list of classes to be deleted.
        List<String> ClassNames=populateNewSetinHashmap();
        deleteOldclasses(classes, deleteOldclasses);

        f.set(classLoader, classes);

        populateItwithNewClasses(classLoader,ClassNames); //populating it withe new classes.

    }

I could also see the number of classes getting changed while debugging through each of these methods.

But somehow,this isn't affecting the runtime classloader though.

Note :No error/exception appeared during the compliation/Runtime and i could see the change in number of classes loaded during the Debugging.





Aucun commentaire:

Enregistrer un commentaire