2021-04-13 09:30:45 +02:00
|
|
|
package io.gitlab.jfronny.breakme;
|
|
|
|
|
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
|
2022-02-28 21:44:08 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2021-04-13 09:30:45 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.NoSuchElementException;
|
2022-07-28 15:30:07 +02:00
|
|
|
import java.util.stream.Stream;
|
2021-04-13 09:30:45 +02:00
|
|
|
|
|
|
|
public class ClassFinder {
|
|
|
|
public static List<Class<?>> find(String packageName) throws NoSuchElementException, IOException {
|
|
|
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
2022-02-28 21:44:08 +01:00
|
|
|
Path p = FabricLoader.getInstance()
|
|
|
|
.getModContainer(BreakMe.MOD_ID)
|
|
|
|
.orElseThrow(FileNotFoundException::new)
|
2022-06-08 12:00:57 +02:00
|
|
|
.findPath(".")
|
|
|
|
.orElseThrow(FileNotFoundException::new)
|
|
|
|
.toAbsolutePath();
|
|
|
|
return findInternal(p, p.resolve(packageName.replace('.', '/')), classLoader);
|
2021-04-13 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-08 12:00:57 +02:00
|
|
|
private static List<Class<?>> findInternal(Path rootPath, Path path, ClassLoader classLoader) throws IOException {
|
2021-04-13 09:30:45 +02:00
|
|
|
List<Class<?>> result = new ArrayList<>();
|
2022-07-28 15:30:07 +02:00
|
|
|
try (Stream<Path> files = Files.list(path)) {
|
|
|
|
files.forEach(s -> {
|
|
|
|
try {
|
|
|
|
if (Files.isDirectory(s)) {
|
|
|
|
result.addAll(findInternal(rootPath, s, classLoader));
|
|
|
|
} else if (s.getFileName().toString().endsWith(".class")) {
|
|
|
|
String p = rootPath.relativize(s).toString().replace('/', '.');
|
|
|
|
result.add(classLoader.loadClass(p.substring(0, p.length() - ".class".length())));
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
BreakMe.LOGGER.error("Could not scan classpath for crash method", e);
|
2021-04-13 09:30:45 +02:00
|
|
|
}
|
2022-07-28 15:30:07 +02:00
|
|
|
});
|
|
|
|
}
|
2021-04-13 09:30:45 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|