habib said:
I need to change the heap for editing objects, for example, adding a
method to a object.
It's good idea for changing software without stopping it.
If the reason you want to do this is to experiment, to learn, and to play with
new concepts then go ahead: have lots of fun! You might even get a research
paper out of it (although dynamic modification of class definitions is not
itself new -- it has been around since at least the 1980's).
The rest of this reply is on the assumption that that's /not/ the case. If you
/need/ to do this -- i.e. you have a design for "real" code (code that will be
used in a production environment by real people to help with real problems)
then I don't think that's the right way to approach it. You will never be able
to make it work reliably if you base it on a production-grade JVM, and you
can't use a less-than-production-grade JVM as the basis for a production
system.
The "correct" way to allow classes to change definition are:
1) Use classloaders. This will allow several versions of the "same"
(same name) class to exist -- at the same time if necessary. The big problem
is that you can't change the definition of the class of an existing object --
you can create a new definition of a class, but all the instances of the old
class remain just that: instances of the old class (which does not change).
2) The other way is not (IMO) suitable for production code, which is to use the
debugging API to change the definition of the running code. That is severely
limited, and in any case not supported by all JVMs. But within (mostly
undocumented) limits you can modify classes in a running application.
3) The last way is simply to give up on Java. Its designers and the creators
of its JVM definition (and the canonical implementation) -- Sun -- simply do
not "get" dynamic languages. So there is no real support for dynamic
operations. You either have to use a complete layer of interpretation above
the JVM stuff (which amounts to using a different language) or you use an
explicitly different language which doesn't have a crippled semantics.
Personally I prefer Smalltalk, but that is by no means the only languages with
the ability to handle dynamic constructs, for instance Ruby and Python are both
dynamic.
-- chris