1

please note: this link didn't help How can I get a list of all the implementations of an interface programmatically in Java

Hi I wonder how to load all types in code that Implements a generic interface without using external libraries from java?

for example how to load all types that implemets this interface & create them?

public interface MyInterface<TInput> {
Void Foo(TInput input);
}   

because this is generic interface I cannot code: MyInterface.class

can I do something like

ServiceLoader<Abc> loader = ServiceLoader.load(Class<? implements MyInterface<Integer>.class);
Community
  • 1
  • 1
roman
  • 607
  • 2
  • 10
  • 18
  • I have no idea if there is a way to get all implementing classes and interfaces, though I think, you can use java.lang.reflect.Type to compare generic classes. – n247s Aug 03 '16 at 16:37
  • But besides all that, you can create a registry for that behavior which might be a lot easier than what you want right now. – n247s Aug 03 '16 at 16:39
  • You will almost certainly have to use a library for this, unless you wish to write a fancy class loader. One possible solution is to get a list of all classes using Google's `com.google.common.reflect.ClassPath` class using the `getTopLevelClasses(packageName)` method. Thereafter, you can test each class for implementation of your interface. Even then, this is still a best guess algorithm. It won't guarantee every class will be found. – ManoDestra Aug 03 '16 at 17:05

0 Answers0