Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Commit 48dbb88

Browse files
Merge pull request #7 from StaticDefault/master
Fixed class finder jar entry error.
2 parents 43468bb + d4aa56b commit 48dbb88

1 file changed

Lines changed: 39 additions & 27 deletions

File tree

src/main/java/com/realtimetech/reflection/classfile/ClassFinder.java

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
package com.realtimetech.reflection.classfile;
22

3-
import java.io.File;
43
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
56
import java.net.URL;
7+
import java.nio.file.FileSystemNotFoundException;
8+
import java.nio.file.FileSystems;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Collections;
613
import java.util.Enumeration;
714
import java.util.LinkedList;
8-
import java.util.List;
15+
import java.util.stream.Stream;
916

1017
public class ClassFinder {
11-
private static void recursiveSearch(File directory, String packageName, List<Class<?>> resultClasses) throws ClassNotFoundException {
12-
if (directory.exists()) {
13-
File[] files = directory.listFiles();
14-
for (File file : files) {
15-
if (file.isDirectory()) {
16-
recursiveSearch(file, packageName + "." + file.getName(), resultClasses);
17-
} else if (file.getName().endsWith(".class")) {
18-
resultClasses.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
19-
}
20-
}
21-
}
22-
}
23-
2418
public static Class<?>[] getClassInPackages(Class<?> packageInClass) throws IOException {
2519
return getClassInPackages(packageInClass.getPackageName());
2620
}
2721

2822
public static Class<?>[] getClassInPackages(String... packageNames) throws IOException {
2923
LinkedList<Class<?>> resultClasses = new LinkedList<Class<?>>();
3024

31-
for(String packageName : packageNames) {
32-
String path = packageName.replace('.', '/');
33-
34-
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(path);
35-
List<File> directories = new LinkedList<File>();
25+
for (String packageName : packageNames) {
26+
String packagePath = packageName.replace('.', '/');
27+
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packagePath);
3628

3729
while (resources.hasMoreElements()) {
38-
URL resource = resources.nextElement();
39-
directories.add(new File(resource.getFile()));
40-
}
41-
42-
for (File directory : directories) {
4330
try {
44-
recursiveSearch(directory, packageName, resultClasses);
45-
} catch (ClassNotFoundException e) {
31+
URL resource = resources.nextElement();
32+
URI packageUri = resource.toURI();
33+
34+
Path root;
35+
36+
if (packageUri.toString().startsWith("jar:")) {
37+
try {
38+
root = FileSystems.getFileSystem(packageUri).getPath(packagePath);
39+
} catch (final FileSystemNotFoundException e) {
40+
root = FileSystems.newFileSystem(packageUri, Collections.emptyMap()).getPath(packagePath);
41+
}
42+
} else {
43+
root = Paths.get(packageUri);
44+
}
45+
46+
final String extension = ".class";
47+
try (final Stream<Path> allPaths = Files.walk(root)) {
48+
allPaths.filter(Files::isRegularFile).forEach(file -> {
49+
try {
50+
final String path = file.toString().replace('/', '.');
51+
final String name = path.substring(path.indexOf(packageName), path.length() - extension.length());
52+
resultClasses.add(Class.forName(name));
53+
} catch (final ClassNotFoundException | StringIndexOutOfBoundsException ignored) {
54+
}
55+
});
56+
}
57+
} catch (URISyntaxException e) {
4658
}
4759
}
4860
}

0 commit comments

Comments
 (0)