Skip to content

Commit

Permalink
Improve Index.of to also accept class files and JAR files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Jan 14, 2022
1 parent a65b921 commit 97b0b27
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 31 deletions.
58 changes: 41 additions & 17 deletions core/src/main/java/org/jboss/jandex/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
* An index useful for quickly processing annotations. The index is read-only and supports
Expand Down Expand Up @@ -137,30 +141,50 @@ public static Index of(Class<?>... classes) throws IOException {
}

/**
* Constructs an Index of class files found in the passed directories.
* The directories are <i>not</i> scanned recursively.
* Constructs an Index of the passed files and directories. Files may be class files or JAR files.
* Directories are scanned for class files, but <i>not</i> recursively.
*
* @param directories Directories containing class files to index
* @param files class files, JAR files or directories containing class files to index
* @return the index
* @throws IllegalArgumentException if any passed {@code File} is null or not a directory
* @throws IllegalArgumentException if any passed {@code File} is null or not a class file, JAR file or directory
*/
public static Index of(File... directories) throws IOException {
public static Index of(File... files) throws IOException {
Indexer indexer = new Indexer();

for (File directory : directories) {
if (directory == null || !directory.isDirectory()) {
throw new IllegalArgumentException("not a directory: " + directory);
}
for (File file : files) {
if (file == null) {
throw new IllegalArgumentException("File must not be null");
} else if (file.isDirectory()) {
File[] classFiles = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile() && pathname.getName().endsWith(".class");
}
});

File[] sources = directory.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile() && pathname.getName().endsWith(".class");
for (File classFile : classFiles) {
try (InputStream in = new FileInputStream(classFile)) {
indexer.index(in);
}
}
});

for (File source : sources) {
indexer.index(new FileInputStream(source));
} else if (file.isFile() && file.getName().endsWith(".class")) {
try (InputStream in = new FileInputStream(file)) {
indexer.index(in);
}
} else if (file.isFile() && file.getName().endsWith(".jar")) {
try (JarFile jarFile = new JarFile(file)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().endsWith(".class")) {
try (InputStream in = jarFile.getInputStream(entry)) {
indexer.index(in);
}
}
}
}
} else {
throw new IllegalArgumentException("Not a class file, JAR file or directory: " + file);
}
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/jboss/jandex/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2030,8 +2030,9 @@ public ClassInfo indexClass(Class<?> clazz) throws IOException {
throw new IllegalArgumentException("clazz cannot be null");
}
String resourceName = '/' + clazz.getName().replace('.', '/') + ".class";
InputStream resource = clazz.getResourceAsStream(resourceName);
return index(resource);
try (InputStream resource = clazz.getResourceAsStream(resourceName)) {
return index(resource);
}
}

/**
Expand All @@ -2054,7 +2055,7 @@ public ClassInfo index(InputStream stream) throws IOException {

// Retroweaved classes may contain annotations
// Also, hierarchy info is needed regardless
if (!isJDK11OrNewer(data))
if (!isJDK11OrNewer(data)) // refers to JDK 1.1, not JDK 11
return null;

initIndexMaps();
Expand Down
55 changes: 44 additions & 11 deletions core/src/test/java/org/jboss/jandex/test/BasicTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,19 @@ public boolean accept(File pathname) {

@Test
public void testIndexOfEmptyDirectory() throws IOException, URISyntaxException {
URL testLocation = getClass().getResource(getClass().getSimpleName() + ".class");
File testDirectory = new File(testLocation.toURI().resolve("../../../../"));
Index index = Index.of(testDirectory);
assertEquals(0, index.getKnownClasses().size());
File tempDir = null;

try {
tempDir = File.createTempFile("temp", ".dir");
tempDir.delete();
tempDir.mkdir();
Index index = Index.of(tempDir);
assertEquals(0, index.getKnownClasses().size());
} finally {
if (tempDir != null) {
tempDir.delete();
}
}
}

@Test
Expand All @@ -306,19 +315,43 @@ public void testIndexOfDirectoryNonClassFile() throws IOException, URISyntaxExce
}

@Test
public void testIndexOfNonDirectory() throws IOException, URISyntaxException {
public void testIndexOfClassFile() throws IOException, URISyntaxException {
final URL testLocation = getClass().getResource(getClass().getSimpleName() + ".class");
final File thisClassFile = new File(testLocation.toURI());
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
Index.of(thisClassFile);
Index index = Index.of(thisClassFile);
assertEquals(1, index.getKnownClasses().size());
}

@Test
public void testIndexOfNonClassFile() throws IOException {
File tempDir = null;
File tempFile = null;

try {
tempDir = File.createTempFile("temp", ".dir");
tempDir.delete();
tempDir.mkdir();
tempFile = File.createTempFile("dummy", ".tmp", tempDir);

File temp = tempFile;
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
Index.of(temp);
}
});
} finally {
if (tempFile != null) {
tempFile.delete();
}
});
if (tempDir != null) {
tempDir.delete();
}
}
}

@Test
public void testIndexOfNullDirectory() throws IOException, URISyntaxException {
public void testIndexOfNullDirectory() {
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
Expand Down

0 comments on commit 97b0b27

Please sign in to comment.