Skip to content

Commit 8ffef3c

Browse files
committed
Added test class grabber
1 parent 54396d4 commit 8ffef3c

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/main/java/me/dustin/jex/feature/mod/core/FeatureManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.dustin.jex.feature.mod.core;
22

3+
import me.dustin.jex.helper.file.ClassHelper;
34
import org.reflections.Reflections;
45

56
import java.util.ArrayList;
@@ -14,7 +15,12 @@ public void initializeFeatureManager() {
1415

1516
//TODO: better method of doing this without a library
1617
Reflections reflections = new Reflections("me.dustin.jex.feature.mod.impl");
17-
18+
ArrayList<Class<?>> classes = new ArrayList<>();
19+
try {//works in intellij but not when built
20+
classes = ClassHelper.INSTANCE.getClassesOther("me.dustin.jex.feature.mod.impl", Feature.class);
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
}
1824
Set<Class<? extends Feature>> allClasses = reflections.getSubTypesOf(Feature.class);
1925
allClasses.forEach(clazz -> {
2026
try {

src/main/java/me/dustin/jex/helper/file/ClassHelper.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,64 @@
44
import com.google.common.reflect.ClassPath;
55
import me.dustin.jex.JexClient;
66

7+
import java.io.File;
78
import java.io.IOException;
9+
import java.net.URISyntaxException;
10+
import java.net.URL;
811
import java.util.ArrayList;
12+
import java.util.Enumeration;
913
import java.util.List;
1014

1115
public enum ClassHelper {
1216
INSTANCE;
1317

18+
public ArrayList<Class<?>> getClassesOther(String packageName, Class<?> assignableFrom) throws IOException, ClassNotFoundException, URISyntaxException {
19+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
20+
assert classLoader != null;
21+
String path = packageName.replace('.', '/');
22+
Enumeration<URL> resources = classLoader.getResources(path);
23+
List<File> dirs = new ArrayList<File>();
24+
while (resources.hasMoreElements()) {
25+
URL resource = resources.nextElement();
26+
try {
27+
dirs.add(new File(resource.toURI()));
28+
} catch (IllegalArgumentException e) {
29+
dirs.add(new File(resource.getFile()));
30+
}
31+
}
32+
ArrayList<Class<?>> classes = new ArrayList<>();
33+
for (File directory : dirs) {
34+
classes.addAll(findClasses(directory, packageName, assignableFrom));
35+
}
36+
ArrayList<Class<?>> removedDupes = new ArrayList<>();
37+
for (Class<?> clazz : classes) {
38+
if (!removedDupes.contains(clazz))
39+
removedDupes.add(clazz);
40+
}
41+
return removedDupes;
42+
}
43+
44+
private static List<Class<?>> findClasses(File directory, String packageName, Class<?> assignableFrom) throws ClassNotFoundException {
45+
List<Class<?>> classes = new ArrayList<>();
46+
if (!directory.exists()) {
47+
return classes;
48+
}
49+
File[] files = directory.listFiles();
50+
assert files != null;
51+
for (File file : files) {
52+
if (file.isDirectory()) {
53+
assert !file.getName().contains(".");
54+
classes.addAll(findClasses(file, packageName + "." + file.getName(), assignableFrom));
55+
} else if (file.getName().endsWith(".class")) {
56+
Class<?> clazz = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
57+
if (assignableFrom.isAssignableFrom(clazz))
58+
classes.add(clazz);
59+
} else {
60+
}
61+
}
62+
return classes;
63+
}
64+
1465
@SuppressWarnings("UnstableApiUsage")
1566
public List<Class<?>> getClasses(String packageName, Class<?> assignableFrom) {
1667
List<Class<?>> classes = new ArrayList<>();

0 commit comments

Comments
 (0)