use of org.reflections.scanners.MethodAnnotationsScanner in project jbehave-core by jbehave.
the class ScanningStepsFactory method scanTypes.
private Set<Class<?>> scanTypes(String packageName) {
Reflections reflections = new Reflections(packageName, new MethodAnnotationsScanner());
Set<Class<?>> types = new HashSet<>();
types.addAll(typesAnnotatedWith(reflections, Given.class));
types.addAll(typesAnnotatedWith(reflections, When.class));
types.addAll(typesAnnotatedWith(reflections, Then.class));
types.addAll(typesAnnotatedWith(reflections, Before.class));
types.addAll(typesAnnotatedWith(reflections, After.class));
types.addAll(typesAnnotatedWith(reflections, BeforeScenario.class));
types.addAll(typesAnnotatedWith(reflections, AfterScenario.class));
types.addAll(typesAnnotatedWith(reflections, BeforeStory.class));
types.addAll(typesAnnotatedWith(reflections, AfterStory.class));
types.addAll(typesAnnotatedWith(reflections, BeforeStories.class));
types.addAll(typesAnnotatedWith(reflections, AfterStories.class));
return types;
}
use of org.reflections.scanners.MethodAnnotationsScanner 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()));
}
}
}
use of org.reflections.scanners.MethodAnnotationsScanner in project Shadbot by Shadorc.
the class StatsManager method save.
@DataSave(filePath = FILE_NAME, initialDelay = 10, period = 10, unit = TimeUnit.MINUTES)
public static void save() throws JSONException, IOException {
JSONObject mainObj = new JSONObject();
Reflections reflections = new Reflections(StatsManager.class.getPackage().getName(), new MethodAnnotationsScanner());
for (Method jsonMethod : reflections.getMethodsAnnotatedWith(StatsJSON.class)) {
StatsJSON annotation = jsonMethod.getAnnotation(StatsJSON.class);
try {
mainObj.put(annotation.name(), jsonMethod.invoke(null));
} catch (Exception err) {
LogUtils.error(err, String.format("An error occurred while saving statistics %s.", jsonMethod.getDeclaringClass().getSimpleName()));
}
}
try (FileWriter writer = new FileWriter(FILE)) {
writer.write(mainObj.toString(Config.JSON_INDENT_FACTOR));
}
}
use of org.reflections.scanners.MethodAnnotationsScanner in project Shadbot by Shadorc.
the class DataManager method init.
public static boolean init() {
LogUtils.infof("Initializing data files...");
if (!SAVE_DIR.exists() && !SAVE_DIR.mkdir()) {
LogUtils.error("The save folder could not be created.");
return false;
}
Reflections reflections = new Reflections(DataManager.class.getPackage().getName(), new MethodAnnotationsScanner());
for (Method initMethod : reflections.getMethodsAnnotatedWith(DataInit.class)) {
try {
initMethod.invoke(null);
} catch (Exception err) {
LogUtils.error(err, String.format("An error occurred while initializing data %s.", initMethod.getDeclaringClass().getSimpleName()));
return false;
}
// Search for save() method and if found, schedule it
for (Method method : initMethod.getDeclaringClass().getMethods()) {
if (method.isAnnotationPresent(DataSave.class)) {
DataSave annotation = method.getAnnotation(DataSave.class);
Runnable saveTask = () -> {
try {
LogUtils.infof("Saving %s...", initMethod.getDeclaringClass().getSimpleName());
method.invoke(null);
LogUtils.infof("%s saved.", initMethod.getDeclaringClass().getSimpleName());
} catch (Exception err) {
LogUtils.error(err, String.format("An error occurred while saving %s.", annotation.filePath()));
}
};
SAVE_TASKS.add(saveTask);
SCHEDULED_EXECUTOR.scheduleAtFixedRate(saveTask, annotation.initialDelay(), annotation.period(), annotation.unit());
}
}
LogUtils.infof("%s initialized.", initMethod.getDeclaringClass().getSimpleName());
}
LogUtils.infof("Data files initialized.");
return true;
}
use of org.reflections.scanners.MethodAnnotationsScanner in project hazelcast by hazelcast.
the class ReflectionUtils method getReflectionsForTestPackage.
public static Reflections getReflectionsForTestPackage(String forPackage) {
try {
URL testClassesURL = new File("target/test-classes").toURI().toURL();
URLClassLoader classLoader = newInstance(new URL[] { testClassesURL }, ClasspathHelper.staticClassLoader());
return new Reflections(new ConfigurationBuilder().addUrls(ClasspathHelper.forPackage(forPackage, classLoader)).addClassLoader(classLoader).filterInputsBy(new FilterBuilder().includePackage(forPackage)).setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner(), new MethodAnnotationsScanner()));
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
Aggregations