use of me.shadorc.shadbot.data.stats.annotation.StatsInit in project Shadbot by Shadorc.
the class StatsManager method init.
@DataInit
public static void init() throws IOException {
if (!FILE.exists()) {
try (FileWriter writer = new FileWriter(FILE)) {
writer.write(new JSONObject().toString(Config.JSON_INDENT_FACTOR));
}
}
JSONObject statsObj;
try (InputStream stream = FILE.toURI().toURL().openStream()) {
statsObj = new JSONObject(new JSONTokener(stream));
}
Reflections reflections = new Reflections(StatsManager.class.getPackage().getName(), new MethodAnnotationsScanner());
for (Method initMethod : reflections.getMethodsAnnotatedWith(StatsInit.class)) {
StatsInit annotation = initMethod.getAnnotation(StatsInit.class);
try {
initMethod.invoke(null, statsObj.has(annotation.name()) ? statsObj.getJSONObject(annotation.name()) : new JSONObject());
} catch (Exception err) {
LogUtils.error(err, String.format("An error occurred while initializing statistics %s.", initMethod.getDeclaringClass().getSimpleName()));
}
}
}
Aggregations