Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

java - How can I find all implementations of interface in classpath?

I'm implementing an interface and now I'd like to get all implementations of this interface in classpath. Is this possible or should I do something else?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Reflections library will let you do that (to an extent):

Set<Class<? extends SomeClassOrInterface>> subTypes = 
     reflections.getSubTypesOf(SomeClassOrInterface.class);

However I wouldn't recommend that. Imagine a typical classpath with 50 external jars, each of which being a big framework like spring, hibernate, aspectj, jsf, etc. It would take much time.

If you want to have some kind of plugin mechanism, so that others can implement your interfaces and supply jars with the implementation, then look at java.util.ServiceLoader


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...