Hey folks...is it possible to get all instances of...
# getting-started
d
Hey folks...is it possible to get all instances of a class (
TimerTask
) at runtime and excute the
run()
method on them?
a
Probably possible with some introspection kinda stuff, but you'll probably want to just load them up somewhere? Maybe use a factory method for creating a TimerTask and store them somewhere?
d
Well, I have nested TimerTask callbacks and I need to be sure, that every TimerTask callback is executed once. This belongs to an internal application testing feature.
n
this is not kotlin related imho but perhaps you could register them in a custom registry in an
init
block
b
Or you could use reflection hacks, but those are VEEERY ugly
d
@Big Chungus thx ... how would that look like?
c
delegates might be an elegant solution here. Create the
TimerTask
through a local delegate instead of the normal function/constructor, which registers that instance with a registry
d
Just intercept creation at runtime using a factory function of any kind, but preferably, write the code such that there's no such system necessary.