Java Reflection with Child Classes -
i'm attempting use reflections (as provided org.reflections
) handle heavy lifting, , don't need manually create instance every class in long list. however, reflections isn't targeting classes in way i'd expect, causing issue.
my current reflections code:
reflections reflections = new reflections(this.getclass().getpackage().getname() + ".command.defaults"); set<class<? extends command>> commandclasses = reflections.getsubtypesof(command.class); // iterate through detected/found classes (class c : commandclasses) { // if class abstract, ignore it. if (modifier.isabstract(c.getmodifiers())) { continue; } // attempt create instance of class/command whatever. try { c.newinstance(); } catch (instantiationexception | illegalaccessexception ex) { // once, right thing ignore exception. // if command loaded can't create instance or // can't access it, skip. but, we'll @ least log (for now). ex.printstacktrace(); } }
essentially, program has commands present in com.example.command.defaults
, they're divided number of sub-packages visual grouping. each command in own class, extends 1 of our 3 abstract classes in com.example.command
: playercommand
, simplecommand
, or command
. playercommand
, simplecommand
extend command
.
from understanding, reflections.getsubtypesof(command.class)
should allow me target class extends command
in way, doesn't seem working way. using debugger tool, i've noticed single class (which extends command
) being read system.
how can make reflections code target classes extend command
, classes extend class extends command
?
acording api documentation, getsubtypesof(class<t> type)
gets all sub types in hierarchy of given type stats "depends on subtypesscanner
configured".
the issue not classes loaded , known class loader in advanced , therefor don't in result list.
Comments
Post a Comment