Skip to content

Commit

Permalink
Merge pull request #437 from Ladicek/fix-index-singleclass-for-modules
Browse files Browse the repository at this point in the history
Fix Index.singleClass() when the passed class file is in fact a module descriptor
  • Loading branch information
Ladicek authored Sep 9, 2024
2 parents 90dd417 + 1268fd0 commit f6434fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 16 additions & 2 deletions core/src/main/java/org/jboss/jandex/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,14 @@ public static ClassInfo singleClass(Class<?> clazz) throws IOException {
Indexer indexer = new Indexer();
indexer.indexClass(clazz);
Index index = indexer.complete();
return index.getKnownClasses().iterator().next();

if (!index.getKnownClasses().isEmpty()) {
return index.getKnownClasses().iterator().next();
} else if (!index.getKnownModules().isEmpty()) {
return index.getKnownModules().iterator().next().moduleInfoClass();
} else {
return null;
}
}

/**
Expand All @@ -305,7 +312,14 @@ public static ClassInfo singleClass(InputStream classData) throws IOException {
Indexer indexer = new Indexer();
indexer.index(classData);
Index index = indexer.complete();
return index.getKnownClasses().iterator().next();

if (!index.getKnownClasses().isEmpty()) {
return index.getKnownClasses().iterator().next();
} else if (!index.getKnownModules().isEmpty()) {
return index.getKnownModules().iterator().next().moduleInfoClass();
} else {
return null;
}
}

// ---
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/org/jboss/jandex/test/ModuleInfoTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -128,6 +129,16 @@ public void testModuleVersion() {
assertEquals("1.0", mod.version());
}

@Test
public void testIndexSingleClassModuleInfo() throws IOException {
Enumeration<URL> modules = ModuleInfoTestCase.class.getClassLoader().getResources("module-info.class");
while (modules.hasMoreElements()) {
try (InputStream in = modules.nextElement().openStream()) {
assertNotNull(Index.singleClass(in));
}
}
}

private ModuleInfo indexModuleInfo() throws IOException {
Index index = buildIndex();
return index.getModuleByName(DotName.createSimple("org.jboss.jandex.typeannotationtest"));
Expand Down

0 comments on commit f6434fb

Please sign in to comment.