1

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 literal 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.

javac
  • 2,431
  • 4
  • 17
  • 26
Chandan
  • 229
  • 1
  • 5
  • 14

1 Answers1

0

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.

WaelJ
  • 2,942
  • 4
  • 22
  • 28
  • Thnx for the reply. But , I have two concerns here. 1)Is it possible to unload a Class using code (unload from memory or I can only set some fiels to mark it unlodeed) 2)Since my code is open to all my users so they can also use my LoadedClassManager in the classes they are adding to my application.I want to prevent this as well. – Chandan Sep 15 '11 at 12:55
  • See the answer here regarding your first concern: http://stackoverflow.com/questions/148681/unloading-classes-in-java – WaelJ Sep 15 '11 at 13:30
  • As for your second point, I'm not quite sure what you mean. Your code is open as in your users can modify he source code? – WaelJ Sep 15 '11 at 13:33