1

If wonder if it's possible to "scan" for classes that implement a certain trait and are located in a pred-defined package and/or folder. My application provides a trait that can be implemented, it would be nice to programmatically get a list of all implementations at runtime without having to "manually register" every implementation first.

I found a similar question and the accepted answer proposes the usage of ServiceLoader. However, it's still necessary to list the available implementations in a text file (META-INF/services).

I understand that it probably would be too expensive to scan all classes for the specific type. But I thought that perhaps restricting the search to a certain package or folder could help?

Community
  • 1
  • 1
ceran
  • 1,392
  • 1
  • 17
  • 43

1 Answers1

0

You can use Reflections or org.clapper.util.classutil.ClassFinder libraries, since "classes that implement a trait" are compiled to classes which implement the corresponding interface. See also Is it possible to get all classes that implementing an interface? and the linked questions.

Community
  • 1
  • 1
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487