Retrieve all the implementations of an interface in Java

+1 vote

Can someone explain how to retrieve the entire list of the implementations of an interface in Java?

Jul 24, 2018 in Java by 93.lynn
• 1,600 points
26,089 views

2 answers to this question.

+1 vote

Hi...I think you can achieve this by using the following:

  1. Reflections: As Reflections help you to perform the following actions:
    • get all subtypes of some type
    • get all types annotated with some annotation 
    • get all types annotated with some annotation, including annotation parameters matching
    • get all methods annotated with some
      Reflections reflections = new Reflections("firstdeveloper.examples.reflections");
      Set<Class<? extends Fruit>> classes = reflections.getSubTypesOf(Fruit.class);
  2. ServiceLoader 
    ServiceLoader<Fruit> loader = ServiceLoader.load(Fruit.class);
    for (Fruit implClass : loader) {
        System.out.println(implClass.getClass().getSimpleName()); ​
    // prints Apple, Mango
    }
    Do remember, you need to define Fruit as a ServiceProviderInterface (SPI) and declare its implementations. you do that by creating a file in resources/META-INF/services with the name examples.reflections.Fruit and declare all implementations of Fruit in it.
    examples.reflections.Apple
    examples.reflections.Mango
  3. package-level annotation
     

    Package[] packages = Package.getPackages();
    for (Package p : packages) {
        MyPackageAnnotation annotation = p.getAnnotation(MyPackageAnnotation.class);
        if (annotation != null) {
            Class<?>[]  implementations = annotation.implementationsOfPet();
            for (Class<?> impl : implementations) {
                System.out.println(impl.getSimpleName());
            }
        }
    }

    and the annotation definition:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PACKAGE)
    public @interface MyPackageAnnotation {
        Class<?>[] implementationsOfPet() default {};
    }

    and you need to declare the package-level annotation in a file named package-info.java inside that package. here are sample contents:

    @MyPackageAnnotation(implementationsOfPet = {Apple.class, Mango.class})
    package examples.reflections;

    Note that only packages that are known to the ClassLoader at that time will be loaded by a call to Package.getPackages(). In case you need to know more about Java interface, join our Java training class today.

answered Jul 24, 2018 by anto.trigg4
• 3,440 points
+4 votes
answered Dec 21, 2019 by Roberto
• 460 points

edited Jul 7, 2020 by Roberto
Hi @roberto, there is an option to edit your answer. Please edit the answer with the updated link.

Thank you

Related Questions In Java

0 votes
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,520 points
144,073 views
0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,190 points
974 views
0 votes
2 answers

What are all the different ways to create an object in Java?

There are different ways you could do this ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,190 points
465 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,190 points
7,849 views
0 votes
0 answers

Unscramble Letters in Java - Get all possible combinations of the letters

I am currently an AP Computer Science ...READ MORE

Apr 13 in Java by Rahul
• 3,380 points
196 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,220 points
9,164 views
0 votes
1 answer

using comparator for sorting in java

You've got two problems: 1) You're using Collections.sort (which takes ...READ MORE

answered Jul 6, 2018 in Java by samarth295
• 2,220 points
359 views
0 votes
1 answer

Usage of abstract class and interface

Well let me draw a clear line ...READ MORE

answered Oct 23, 2018 in Java by geek.erkami
• 2,680 points
327 views
0 votes
1 answer
+2 votes
3 answers

Listing all the subclasses of a specific class in Java

Try with BurningWave core library. Here an ...READ MORE

answered Dec 29, 2019 in Java by anonymous
• 460 points

edited Jan 9, 2020 by anonymous 8,474 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP