Matthias
Matthias

Reputation: 3556

Keep object ids between two debugging sessions

I am debugging a java application using eclipse debugger.

I wonder about the object ids the debugger shows to me (see also Java object ID in jvm ).

Is it possible to make these ids identical between two debugging session? So that, when i debug the same application twice (one after the other) same objects have the same id? Or is it possible to set breakpoints like this: Break at whatever statement when object with given id is involved?

I have in mind a definition of breakpoint that does not involve a certain line of code. I have in mind a breakpoint that is indepent from line of code. A breakpoint that only involves id. The debugger, having a tool like these ids at hand: can he not use it to offer me a feature like i propose?

Upvotes: 0

Views: 250

Answers (2)

dtech
dtech

Reputation: 14060

This is not possible. The id is internal to the debugger and randomly generated each time an object is created (or restored from serialization etc.). Furthermore you shouldn't try to abuse the property that way.

Luckily Java has something which will likely suit your needs: Object#hashCode. It is a (semi-)unique identifier for each unique object (as far as Object#equals is concerned). It can thus be used to identify a object consistently across debugging session as long as the data is the same.

Eclipse can generate a hashCode() for you based on all class member variables. I'd suggest using that since it will likely suit your needs and guarantees that the hash has all desired properties. It is under Right click->Source->Generate hashCode() and equals()

As far as the breakpoint is concerned, just create an if checking for the correct object and place a breakpoint inside.

Upvotes: 3

Attila
Attila

Reputation: 28772

I do not know specifically what the IDs represent in Eclipse during debugging (aside that they uniquely identify the object in that debugging session) -- could be memory address, handles (internal or external), etc. Since objects are usually dynamically created there is no way for the debugger to know that one object is the same as another in a different (past) debugging session.

The only way I can imagine this happening is if the Ids are assigned based on order of creation and the exact same objects are created in the exact same order -- not a likely event.

So the answer is no.

Upvotes: 0

Related Questions