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

Commit

Permalink
Merge pull request #7 from StaticDefault/master
Browse files Browse the repository at this point in the history
Fixed class finder jar entry error.
  • Loading branch information
StaticDefaultTester2 authored Nov 28, 2019
2 parents 43468bb + d4aa56b commit 48dbb88
Showing 1 changed file with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
package com.realtimetech.reflection.classfile;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

public class ClassFinder {
private static void recursiveSearch(File directory, String packageName, List<Class<?>> resultClasses) throws ClassNotFoundException {
if (directory.exists()) {
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
recursiveSearch(file, packageName + "." + file.getName(), resultClasses);
} else if (file.getName().endsWith(".class")) {
resultClasses.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
}
}
}
}

public static Class<?>[] getClassInPackages(Class<?> packageInClass) throws IOException {
return getClassInPackages(packageInClass.getPackageName());
}

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

for(String packageName : packageNames) {
String path = packageName.replace('.', '/');

Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(path);
List<File> directories = new LinkedList<File>();
for (String packageName : packageNames) {
String packagePath = packageName.replace('.', '/');
Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packagePath);

while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
directories.add(new File(resource.getFile()));
}

for (File directory : directories) {
try {
recursiveSearch(directory, packageName, resultClasses);
} catch (ClassNotFoundException e) {
URL resource = resources.nextElement();
URI packageUri = resource.toURI();

Path root;

if (packageUri.toString().startsWith("jar:")) {
try {
root = FileSystems.getFileSystem(packageUri).getPath(packagePath);
} catch (final FileSystemNotFoundException e) {
root = FileSystems.newFileSystem(packageUri, Collections.emptyMap()).getPath(packagePath);
}
} else {
root = Paths.get(packageUri);
}

final String extension = ".class";
try (final Stream<Path> allPaths = Files.walk(root)) {
allPaths.filter(Files::isRegularFile).forEach(file -> {
try {
final String path = file.toString().replace('/', '.');
final String name = path.substring(path.indexOf(packageName), path.length() - extension.length());
resultClasses.add(Class.forName(name));
} catch (final ClassNotFoundException | StringIndexOutOfBoundsException ignored) {
}
});
}
} catch (URISyntaxException e) {
}
}
}
Expand Down

0 comments on commit 48dbb88

Please sign in to comment.