|
1 | 1 | package com.realtimetech.reflection.classfile; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | 3 | import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.URISyntaxException; |
5 | 6 | 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; |
6 | 13 | import java.util.Enumeration; |
7 | 14 | import java.util.LinkedList; |
8 | | -import java.util.List; |
| 15 | +import java.util.stream.Stream; |
9 | 16 |
|
10 | 17 | 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 | | - |
24 | 18 | public static Class<?>[] getClassInPackages(Class<?> packageInClass) throws IOException { |
25 | 19 | return getClassInPackages(packageInClass.getPackageName()); |
26 | 20 | } |
27 | 21 |
|
28 | 22 | public static Class<?>[] getClassInPackages(String... packageNames) throws IOException { |
29 | 23 | LinkedList<Class<?>> resultClasses = new LinkedList<Class<?>>(); |
30 | 24 |
|
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); |
36 | 28 |
|
37 | 29 | while (resources.hasMoreElements()) { |
38 | | - URL resource = resources.nextElement(); |
39 | | - directories.add(new File(resource.getFile())); |
40 | | - } |
41 | | - |
42 | | - for (File directory : directories) { |
43 | 30 | 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) { |
46 | 58 | } |
47 | 59 | } |
48 | 60 | } |
|
0 commit comments