#Getting All Instances Of Class
18 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @sage lance! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
How many instances are you expecting?
Since if you're using expecting one instance, just use a singleton. Otherwise things might get trickier
I'd suggest looking at this library (though it is an external dependency): https://github.com/ronmamo/reflections
I've personally used it for reflecting and it is pretty easy and efficient to use
I don't think that contains what I need. And I actually already use it!
Oh nice! What are you trying to do by reflecting all the class instances?
May give some more insight as to how we can help
If it's a class that you yourself have made, I would recommend avoiding reflection and instead implementing a registry to record the instances you're creating. Create a static factory method to return all of the instances you're creating, and at the same time, store weak references to those instances in a cache.
From a quick google, it doesn't seem like there's a well known way to get all active instances of an arbitrary class during normal runtime.
It wouldn't be thread safe anyways, so custom implementation is really the only way.
There is a JVM workaround (as in, you get the JVM instance, attach a method to it, and use said method to query it). However, there is no way to get the current host and port, which are required to attach to the JVM.
Anyway, thanks for trying. Gonna have to find another way :/
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Good luck. Gimme a ping if you find any interesting solutions.
k thx