How to automatically register all available interface implementations in Quarkus?

533 Views Asked by At

I'm trying to adapt a library to be usable in Quarkus native mode. Since it's reflection-heavy, I need to manually register all implementations of certain interfaces.

What I've done so far and which seems to work fine for user code:

private static void registerAllImplementations(CombinedIndexBuildItem combinedIndexBuildItem, 
  BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchyClass,
  Class<?>... classNames) {

    for (Class<?> klass : classNames) {
        combinedIndexBuildItem.getIndex().getAllKnownImplementors(DotName.createSimple(klass.getName())).stream()
          .map(ci -> new ReflectiveHierarchyBuildItem(Type.create(ci.name(), Type.Kind.CLASS)))
          .forEach(reflectiveHierarchyClass::produce);
    }
}

However, the below line doesn't pick up implementors that come from external jars:

combinedIndexBuildItem.getIndex().getAllKnownImplementors(...)

It's not a tragedy, but it'd be much more future-proof if one did not need to pay attention to the internals of some external jar and make sure that all relevant implementations get registered manually.

Do you have any clues?

1

There are 1 best solutions below

0
On

Behind the scene, Quarkus uses Jandex to index your sources. This is Jandex that provides the CombinedIndexBuildItem so you need all the external jar to be indexed by Jandex.

For this you can add the Jandex maven plugin to those external JAR or add some configuration options for each jar :

quarkus.index-dependency.<name>.group-id=
quarkus.index-dependency.<name>.artifact-id=

More information here: https://quarkus.io/guides/cdi-reference#how-to-generate-a-jandex-index