jeudi 17 janvier 2019

Java. How to get all instance(not class) of subclass with reflected?

Sure, I have do some research like package scan, reflection of java, gson and so on.

What I want?

I want get some instance randomly but keep this part code in the subclass.

So, each subclass should have a method named like SuperClass nextInstance(). If I get subclass List, say classes, by reflection, I just need invoke nextInstance for each element in classes.

That's just be done.

But, I need add static method for every subclass, and the name must be nextInstance, real stupid!!!

what I have do.

  • Create a factory class for each of them(too troublesome).
  • Add subclass path in a List to loading them in runtime(Hard to maintain).
  • Try a abstract static method by reflection(impossible in java).
  • And so on(may be, I give it up)

    Any suggest about it?





Aucun commentaire:

Enregistrer un commentaire