Is there a way to mark given java object (not class!) at runtime with reflection? Some way to modify anything about the object so we can recognize it next time we see it?
Or the only way for such recognition is to store object into IdentityHashMap?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java instances do not support dynamic metadata by default. However, even if they did, its kind of an anti-pattern what you are describing.
If you need create metadata around an instance there are many ways to do it, but the simplest is to just register the objects in a collection and attach metadata that way whether persistent or not.
You could use dynamic proxies to achieve this at runtime but again I recommend against it. If you are building a system that really needs this, a base object with some added metadata on your specific applications classes would be easy to implement, not cost you anything, and be low complexity.