Search in sources :

Example 16 with Reflections

use of org.reflections.Reflections in project alluxio by Alluxio.

the class AlluxioShell method loadCommands.

/**
   * Uses reflection to get all the {@link ShellCommand} classes and store them in a map.
   */
private void loadCommands() {
    String pkgName = ShellCommand.class.getPackage().getName();
    Reflections reflections = new Reflections(pkgName);
    for (Class<? extends ShellCommand> cls : reflections.getSubTypesOf(ShellCommand.class)) {
        // Only instantiate a concrete class
        if (!Modifier.isAbstract(cls.getModifiers())) {
            ShellCommand cmd;
            try {
                cmd = CommonUtils.createNewClassInstance(cls, new Class[] { FileSystem.class }, new Object[] { mFileSystem });
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
            mCommands.put(cmd.getCommandName(), cmd);
        }
    }
}
Also used : FileSystem(alluxio.client.file.FileSystem) ShellCommand(alluxio.shell.command.ShellCommand) IOException(java.io.IOException) Reflections(org.reflections.Reflections)

Example 17 with Reflections

use of org.reflections.Reflections in project asterixdb by apache.

the class SimpleSerializerDeserializerTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() {
    Reflections reflections = new Reflections("org.apache.asterix.dataflow.data.nontagged.serde");
    Set<Class<? extends ISerializerDeserializer>> allClasses = reflections.getSubTypesOf(ISerializerDeserializer.class);
    for (Class<? extends ISerializerDeserializer> cl : allClasses) {
        String className = cl.getName();
        if (className.endsWith("ARecordSerializerDeserializer") || className.endsWith("AUnorderedListSerializerDeserializer") || className.endsWith("AOrderedListSerializerDeserializer") || className.endsWith("AStringSerializerDeserializer")) {
            // Serializer/Deserializer for complex types can have (immutable) states.
            continue;
        }
        // Verifies the class does not have non-static fields.
        for (Field field : cl.getDeclaredFields()) {
            if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
                throw new IllegalStateException("The serializer/deserializer " + cl.getName() + " is not stateless!");
            }
        }
        // Verifies the class follows the singleton pattern.
        for (Constructor constructor : cl.getDeclaredConstructors()) {
            if (!java.lang.reflect.Modifier.isPrivate(constructor.getModifiers())) {
                throw new IllegalStateException("The serializer/deserializer " + cl.getName() + " is not implemented as a singleton class!");
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 18 with Reflections

use of org.reflections.Reflections in project sling by apache.

the class ConfigurationMetadataUtil method getConfigurationClassesForPackages.

/**
     * Get configuration classes in list of packages (and subpackages), and cache result in static map.
     * @param packageNames Package names
     * @return List of classes
     */
public static Collection<Class> getConfigurationClassesForPackages(String packageNames) {
    List<Class> classes = CONFIGURATION_CLASSES_FOR_PACKAGES.get(packageNames);
    if (classes == null) {
        classes = new ArrayList<Class>();
        String[] packageNameArray = StringUtils.split(packageNames, ",");
        // add "." to each package name because it's a prefix, not a package name
        Object[] prefixArray = new Object[packageNameArray.length];
        for (int i = 0; i < packageNameArray.length; i++) {
            prefixArray[i] = packageNameArray[i] + ".";
        }
        Reflections reflections = new Reflections(prefixArray);
        classes.addAll(reflections.getTypesAnnotatedWith(Configuration.class));
        CONFIGURATION_CLASSES_FOR_PACKAGES.putIfAbsent(packageNames, classes);
    }
    return classes;
}
Also used : Configuration(org.apache.sling.caconfig.annotation.Configuration) Reflections(org.reflections.Reflections)

Example 19 with Reflections

use of org.reflections.Reflections in project sling by apache.

the class ModelAdapterFactoryUtil method getModelClassUrlsForPackages.

/**
     * Get model classes in list of packages (and subpackages), and cache result in static map.
     * @param packageNames Package names
     * @return List of URLs
     */
private static Collection<URL> getModelClassUrlsForPackages(String packageNames) {
    List<URL> urls = MODEL_URLS_FOR_PACKAGES.get(packageNames);
    if (urls == null) {
        urls = new ArrayList<URL>();
        String[] packageNameArray = StringUtils.split(packageNames, ",");
        // add "." to each package name because it's a prefix, not a package name
        Object[] prefixArray = new Object[packageNameArray.length];
        for (int i = 0; i < packageNameArray.length; i++) {
            prefixArray[i] = packageNameArray[i] + ".";
        }
        Reflections reflections = new Reflections(prefixArray);
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Model.class);
        for (Class<?> clazz : classes) {
            urls.add(classToUrl(clazz));
        }
        MODEL_URLS_FOR_PACKAGES.putIfAbsent(packageNames, urls);
    }
    return urls;
}
Also used : URL(java.net.URL) Reflections(org.reflections.Reflections)

Example 20 with Reflections

use of org.reflections.Reflections in project opennms by OpenNMS.

the class FunctionsManager method getTypesAnnotatedWithFunction.

private List<Class<?>> getTypesAnnotatedWithFunction(String packageToScan) {
    Reflections reflections = new Reflections(packageToScan);
    Set<Class<?>> functions = reflections.getTypesAnnotatedWith(Function.class);
    return new ArrayList<>(functions);
}
Also used : ArrayList(java.util.ArrayList) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)60 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)26 SubTypesScanner (org.reflections.scanners.SubTypesScanner)20 ArrayList (java.util.ArrayList)10 URL (java.net.URL)9 Test (org.junit.Test)9 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)9 HashSet (java.util.HashSet)7 Set (java.util.Set)6 IOException (java.io.IOException)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 ResourcesScanner (org.reflections.scanners.ResourcesScanner)5 Slf4j (lombok.extern.slf4j.Slf4j)4 ClasspathHelper (org.reflections.util.ClasspathHelper)4 FilterBuilder (org.reflections.util.FilterBuilder)4 Bean (org.springframework.context.annotation.Bean)4 File (java.io.File)3 Method (java.lang.reflect.Method)3 HashMap (java.util.HashMap)3