Search in sources :

Example 6 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project sldeditor by robward-scisys.

the class RegisterClasses method registerSymbols.

/**
 * Register symbols converters.
 *
 * @param classLoadersList the class loaders list
 * @param data the data
 */
private static void registerSymbols(List<ClassLoader> classLoadersList, ConversionData data) {
    logger.info("Symbols supported:");
    Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new SubTypesScanner(false), new ResourcesScanner()).setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))).filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("com.sldeditor.importdata.esri.symbol"))));
    Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);
    for (Class<? extends Object> claszz : allClasses) {
        try {
            if (validClass(claszz, EsriSymbolInterface.class)) {
                EsriSymbolInterface symbolObj = (EsriSymbolInterface) claszz.newInstance();
                logger.info(symbolObj.getSymbolClass().getName());
                data.addSymbol(symbolObj);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) EsriSymbolInterface(com.sldeditor.importdata.esri.symbol.EsriSymbolInterface) SubTypesScanner(org.reflections.scanners.SubTypesScanner) FilterBuilder(org.reflections.util.FilterBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 7 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project sldeditor by robward-scisys.

the class RegisterClasses method registerNumberFormat.

/**
 * Register number format converters.
 *
 * @param classLoadersList the class loaders list
 * @param data the data
 */
private static void registerNumberFormat(List<ClassLoader> classLoadersList, ConversionData data) {
    logger.info("Number formats supported:");
    Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new SubTypesScanner(false), new ResourcesScanner()).setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))).filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("com.sldeditor.importdata.esri.numberformat"))));
    Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);
    for (Class<? extends Object> claszz : allClasses) {
        try {
            if (validClass(claszz, EsriNumberFormatInterface.class)) {
                EsriNumberFormatInterface numberFormatObj = (EsriNumberFormatInterface) claszz.newInstance();
                logger.info(numberFormatObj.getNumberFormatClass().getName());
                data.addNumberFormat(numberFormatObj);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) FilterBuilder(org.reflections.util.FilterBuilder) EsriNumberFormatInterface(com.sldeditor.importdata.esri.numberformat.EsriNumberFormatInterface) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 8 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project BoofCV by lessthanoptimal.

the class ExampleLauncherApp method createTree.

@Override
protected void createTree(DefaultMutableTreeNode root) {
    List<String> packages = new ArrayList<>();
    packages.add("boofcv.examples.calibration");
    packages.add("boofcv.examples.enhance");
    packages.add("boofcv.examples.features");
    packages.add("boofcv.examples.fiducial");
    packages.add("boofcv.examples.geometry");
    packages.add("boofcv.examples.imageprocessing");
    packages.add("boofcv.examples.recognition");
    packages.add("boofcv.examples.segmentation");
    packages.add("boofcv.examples.sfm");
    packages.add("boofcv.examples.stereo");
    packages.add("boofcv.examples.tracking");
    // Reflections is a weird package that does not behave the way one would expect.  Several hacks below
    for (String p : packages) {
        Reflections reflections = new Reflections(p, new SubTypesScanner(false));
        List<String> listTypes = new ArrayList<>();
        listTypes.addAll(reflections.getAllTypes());
        addAll((Set) reflections.getSubTypesOf(LearnSceneFromFiles.class), listTypes);
        String name = p.split("\\.")[2];
        Collections.sort(listTypes);
        List<Class> classes = new ArrayList<>();
        String[] classNames = listTypes.toArray(new String[1]);
        for (int i = 0; i < classNames.length; i++) {
            if (!classNames[i].contains("Example"))
                continue;
            // no idea why this is needed
            if (classNames[i].contains("$"))
                continue;
            try {
                classes.add(Class.forName(classNames[i]));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        createNodes(root, name, classes.toArray(new Class[0]));
    }
}
Also used : SubTypesScanner(org.reflections.scanners.SubTypesScanner) ArrayList(java.util.ArrayList) Reflections(org.reflections.Reflections)

Example 9 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project motech by motech.

the class ReflectionsUtil method getClasses.

/**
 * Looks for classes annotated with a given annotation.
 *
 * @param annotation an annotation to look for
 * @param bundle a bundle to look in.
 * @return A list of classes, annotated with the given annotation
 */
public static Set<Class<?>> getClasses(Class<? extends Annotation> annotation, Bundle bundle) {
    LOGGER.debug("Scanning bundle: {}", bundle.getSymbolicName());
    LOGGER.debug("Searching for classes with annotations: {}", annotation.getName());
    Reflections reflections = configureReflection(bundle, new PristineBundleClassLoader(bundle), new TypeAnnotationsScanner(), new SubTypesScanner());
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(annotation);
    // in order to prevent processing of user defined or auto generated fields
    // we have to load the bytecode from the jar and define the class in a temporary
    // classLoader
    PristineBundleClassLoader pristineBundleClassLoader = new PristineBundleClassLoader(bundle);
    Set<Class<?>> result = new HashSet<>();
    for (Class clazz : classes) {
        try {
            result.add(pristineBundleClassLoader.loadClass(clazz.getName()));
        } catch (ClassNotFoundException e) {
            LOGGER.error("Could not find class", e);
        }
    }
    LOGGER.debug("Searched for classes with annotations: {}", annotation.getName());
    LOGGER.trace("Found {} classes with annotations: {}", result.size(), annotation.getName());
    return result;
}
Also used : SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 10 with SubTypesScanner

use of org.reflections.scanners.SubTypesScanner in project pwm by pwm-project.

the class ControlledPwmServletTest method getClassAndMethods.

private Map<Class<? extends ControlledPwmServlet>, Map<String, Method>> getClassAndMethods() {
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("password.pwm")).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new FieldAnnotationsScanner()));
    Set<Class<? extends ControlledPwmServlet>> classes = reflections.getSubTypesOf(ControlledPwmServlet.class);
    final Map<Class<? extends ControlledPwmServlet>, Map<String, Method>> returnMap = new HashMap<>();
    for (final Class<? extends ControlledPwmServlet> controlledPwmServlet : classes) {
        if (!Modifier.isAbstract(controlledPwmServlet.getModifiers())) {
            final Map<String, Method> annotatedMethods = new HashMap<>();
            for (Method method : JavaHelper.getAllMethodsForClass(controlledPwmServlet)) {
                if (method.getAnnotation(ControlledPwmServlet.ActionHandler.class) != null) {
                    final String actionName = method.getAnnotation(ControlledPwmServlet.ActionHandler.class).action();
                    annotatedMethods.put(actionName, method);
                }
            }
            returnMap.put(controlledPwmServlet, Collections.unmodifiableMap(annotatedMethods));
        }
    }
    return Collections.unmodifiableMap(returnMap);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) HashMap(java.util.HashMap) Map(java.util.Map) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)41 SubTypesScanner (org.reflections.scanners.SubTypesScanner)41 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)29 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)18 HashSet (java.util.HashSet)9 FilterBuilder (org.reflections.util.FilterBuilder)9 ResourcesScanner (org.reflections.scanners.ResourcesScanner)8 URL (java.net.URL)7 ArrayList (java.util.ArrayList)7 FieldAnnotationsScanner (org.reflections.scanners.FieldAnnotationsScanner)6 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 File (java.io.File)4 List (java.util.List)4 Bean (org.springframework.context.annotation.Bean)4 HashMap (java.util.HashMap)3 Slf4j (lombok.extern.slf4j.Slf4j)3 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)3 MethodParameterScanner (org.reflections.scanners.MethodParameterScanner)3 IOException (java.io.IOException)2