Search in sources :

Example 1 with MethodAnnotationsScanner

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;
}
Also used : Before(org.junit.Before) AfterStory(org.jbehave.core.annotations.AfterStory) Given(org.jbehave.core.annotations.Given) AfterStories(org.jbehave.core.annotations.AfterStories) BeforeStory(org.jbehave.core.annotations.BeforeStory) BeforeScenario(org.jbehave.core.annotations.BeforeScenario) AfterScenario(org.jbehave.core.annotations.AfterScenario) When(org.jbehave.core.annotations.When) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) After(org.junit.After) Then(org.jbehave.core.annotations.Then) BeforeStories(org.jbehave.core.annotations.BeforeStories) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 2 with MethodAnnotationsScanner

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()));
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) StatsInit(me.shadorc.shadbot.data.stats.annotation.StatsInit) JSONObject(org.json.JSONObject) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) Method(java.lang.reflect.Method) IOException(java.io.IOException) JSONException(org.json.JSONException) Reflections(org.reflections.Reflections) DataInit(me.shadorc.shadbot.data.annotation.DataInit)

Example 3 with MethodAnnotationsScanner

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));
    }
}
Also used : JSONObject(org.json.JSONObject) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FileWriter(java.io.FileWriter) StatsJSON(me.shadorc.shadbot.data.stats.annotation.StatsJSON) Method(java.lang.reflect.Method) IOException(java.io.IOException) JSONException(org.json.JSONException) Reflections(org.reflections.Reflections) DataSave(me.shadorc.shadbot.data.annotation.DataSave)

Example 4 with MethodAnnotationsScanner

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;
}
Also used : MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) Method(java.lang.reflect.Method) DataSave(me.shadorc.shadbot.data.annotation.DataSave) Reflections(org.reflections.Reflections)

Example 5 with MethodAnnotationsScanner

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);
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) MalformedURLException(java.net.MalformedURLException) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FilterBuilder(org.reflections.util.FilterBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) URLClassLoader(java.net.URLClassLoader) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) File(java.io.File) URL(java.net.URL) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)12 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)12 Method (java.lang.reflect.Method)6 SubTypesScanner (org.reflections.scanners.SubTypesScanner)4 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)4 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)4 URL (java.net.URL)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 DataSave (me.shadorc.shadbot.data.annotation.DataSave)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 MethodParameterScanner (org.reflections.scanners.MethodParameterScanner)2 FilterBuilder (org.reflections.util.FilterBuilder)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 File (java.io.File)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1