-1

I try to use reflection to get every class that implements a given interface in java, using the classes in java.lang.reflect only. For of many reasons i can't use any external library.

I've found many tutorials to do a lot of things with java.lang.reflect but nothing to get every class that implements a given interface. Any idea?

Axel
  • 165
  • 3
  • 14
  • Can you use annotations? It would be simple, and actually built for the similar purpose, too. – Anand Vaidya Jul 31 '20 at 15:12
  • Could you expand a bit? I don't know annotations, what is it? – Axel Jul 31 '20 at 15:15
  • So is it your own interface or you are banking on some library ifc? – Anand Vaidya Jul 31 '20 at 16:19
  • Java classes are loaded dynamically, a class in a different .jar file may not be known to the Java runtime at all. You would be much better off using a [ServiceLoader](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/ServiceLoader.html) instead of reflection. – VGR Jul 31 '20 at 16:47

1 Answers1

0

If such a method existed, it would require previous loading of all classes in the Classpath, because how else would you know which classes implement that interface? That would certainly be a drain on performance, and run counter to common JVM design.

So, sorry, but I doubt you will find a way to do this without a more complex approach.