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 23, 2018 in Java by 93.lynn
• 1,600 points
2,844 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().

answered Jul 23, 2018 by anto.trigg4
• 3,440 points
+2 votes
answered Dec 20, 2019 by Roberto
• 300 points

edited Dec 31, 2019 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
3 answers

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

import java.util.*; public class AmstrongNumber { public static void ...READ MORE

answered Aug 6, 2019 in Java by anonymous
11,345 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
• 48,780 points
160 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
• 48,780 points
126 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
• 48,780 points
489 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
1,169 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,190 points
965 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,190 points
85 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,660 points
122 views
+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
• 300 points

edited Jan 9 by anonymous 976 views