At runtime, find all classes in a Java application that extend a base class

前端 未结 13 1763
长情又很酷
长情又很酷 2020-11-22 17:25

I want to do something like this:

List animals = new ArrayList();

for( Class c: list_of_all_classes_available_to_my_app() )
   i         


        
相关标签:
13条回答
  • 2020-11-22 18:06

    I solved this problem pretty elegantly using Package Level Annotations and then making that annotation have as an argument a list of classes.

    Find Java classes implementing an interface

    Implementations just have to create a package-info.java and put the magic annotation in with the list of classes they want to support.

    0 讨论(0)
提交回复
热议问题