I found this question quite interesting so I came up with a solution. The tricky part here is to actually find all classes that are in a given package.
In this example I assume that all classes are in the same package where the class C
is and that all classes except C
have an onLoad
method which may be inaccessible (i.e. private
). Then you can use the following example code.
public final class C {
public static void main(String[] args) throws Exception {
for (Class<?> cls : getClasses(C.class)) {
if (cls != C.class) {
Method onLoad = cls.getDeclaredMethod("onLoad");
onLoad.setAccessible(true);
onLoad.invoke(null);
}
}
}
private static List<Class<?>> getClasses(Class<?> caller)
throws IOException, URISyntaxException {
return Files.walk(getPackagePath(caller))
.filter(Files::isRegularFile)
.filter(file -> file.toString().endsWith(".class"))
.map(path -> mapPathToClass(path, caller.getPackage().getName()))
.collect(Collectors.toList());
}
private static Class<?> mapPathToClass(Path clsPath, String packageName) {
String className = clsPath.toFile().getName();
className = className.substring(0, className.length() - 6);
return loadClass(packageName + "." + className);
}
private static Path getPackagePath(Class<?> caller)
throws IOException, URISyntaxException {
String packageName = createPackageName(caller);
Enumeration<URL> resources = caller.getClassLoader()
.getResources(packageName);
return Paths.get(resources.nextElement().toURI());
}
private static String createPackageName(Class<?> caller) {
return caller.getPackage().getName().replace(".", "/");
}
private static Class<?> loadClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
}
}
I've omitted all exception checking and statements like resources.nextElement()
may even throw an exception. So if you want to cover those cases, you need to add a few checks here and there.