smallrye / jandex

Java Annotation Indexer
Apache License 2.0
400 stars 94 forks source link

Exception in Index.singleClass() when applied to module-info.class #436

Closed bbobcik closed 2 months ago

bbobcik commented 2 months ago

When a client invokes method singleClass() on org.jboss.jandex.Index and provides an argument that represents module-info.class, the method fails with NoSuchElements exception. This is caused by an assumption that a map holding known classes is never empty and therefore it is safe to call .iterator().next():

https://github.com/smallrye/jandex/blob/f8b6fa83b6b0141a2173eb428b2ba9e07f76ab2b/core/src/main/java/org/jboss/jandex/Index.java#L278-L283

In my case it occurs in WildFly 33.0.1, while trying to examine bean archive candidate Wicket-CDI 10.1.0; the deployment attempt is immediately aborted. When I manually deleted module-info.class from the Wicket-CDI JAR file, the deployment worked as expected. (The mentioned version of WildFly contains Jandex version 3.2.0 within its system modules.)

I believe the correct response would be to return null.

Ladicek commented 2 months ago

That doesn't seem right on the first sight, even a module-info.class should result in a valid ClassInfo. I'll take a look.

Ladicek commented 2 months ago

Ah, now I understand. Indexer.processClassInfo() does

if (!currentClass.isModule()) {
    classes.put(currentClass.name(), currentClass);
}

so while there is a ClassInfo for the module, it isn't put into the map of classes. Should be straightforward -- if Index.singleClass() gets an Index with no classes, check the modules.