Java Reflection
I am loading some Classes from a location (using my own Classloader) and my Application users can add their jar files to this location and I will load all the classes present in those jar files.It is like some runtime plugin Classes of a particular type.Then I keep these Class literal objects in a HashMap (which is a field of a particular class) and create instances from there whenever required.If some class lit开发者_运维百科eral is removed from this HashMap,then no-one should be able to a create an instance of that class-literal, like the class should get unloaded for my users who are adding classes. 2)I also want that my users should not a any code that manipulate with my hashMAp carrying all the class literals I have loaded.
I haven't had proper experience with reflection, but what I would suggest is the following:
Have a class (LoadedClassesManager, say) containing a record of all instances instantiated. It should also implement the observable pattern (i.e. respond to event listeners).
To have all instantiation of loaded classes to be invoked by a method call in LoadedClassManager (something like getNewInstance(Class clazz, params)
). The method would also register the calling object as a listener (for when the class is unloaded).
So when some class is to be unloaded, the event listeners' even handlers would be called by LoadedClassesManager to deal with the class's unloading appropriately.
精彩评论