Search in sources :

Example 91 with Reflections

use of org.reflections.Reflections in project charts by vaadin.

the class TListUi method listTestClasses.

private void listTestClasses(Container indexedContainer, String subpackage) {
    String packageName = "com.vaadin.addon.charts.examples";
    if (subpackage != null) {
        packageName += "." + subpackage;
    }
    Reflections reflections = new Reflections(packageName);
    Set<Class<? extends AbstractVaadinChartExample>> subTypes = reflections.getSubTypesOf(AbstractVaadinChartExample.class);
    for (Class<? extends AbstractVaadinChartExample> subType : subTypes) {
        try {
            addTest(indexedContainer, subType.getSimpleName(), subType, subpackage);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : AbstractVaadinChartExample(com.vaadin.addon.charts.examples.AbstractVaadinChartExample) Reflections(org.reflections.Reflections)

Example 92 with Reflections

use of org.reflections.Reflections in project oap by oaplatform.

the class Resources method urls.

public static List<URL> urls(String atPackage, String ext) {
    final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder().setNameFormat("reflections-%d").build());
    try {
        final ConfigurationBuilder configuration = new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(atPackage)).setScanners(new ResourcesScanner()).filterInputsBy(new FilterBuilder().includePackage(atPackage)).setExecutorService(executorService);
        final Reflections reflections = new Reflections(configuration);
        final Set<String> resources = reflections.getResources(in -> in.endsWith("." + ext));
        return new ArrayList<>(Sets.map(resources, r -> Thread.currentThread().getContextClassLoader().getResource(r)));
    } finally {
        executorService.shutdown();
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) URL(java.net.URL) Reflections(org.reflections.Reflections) Strings(oap.util.Strings) Executors(oap.concurrent.Executors) ArrayList(java.util.ArrayList) FilterBuilder(org.reflections.util.FilterBuilder) Sets(oap.util.Sets) Lists(oap.util.Lists) Stream(oap.util.Stream) Path(java.nio.file.Path) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ExecutorService(java.util.concurrent.ExecutorService) Set(java.util.Set) IOException(java.io.IOException) ClasspathHelper(org.reflections.util.ClasspathHelper) UncheckedIOException(java.io.UncheckedIOException) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Try(oap.util.Try) List(java.util.List) Paths(java.nio.file.Paths) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) Collections(java.util.Collections) InputStream(java.io.InputStream) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FilterBuilder(org.reflections.util.FilterBuilder) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Reflections(org.reflections.Reflections)

Example 93 with Reflections

use of org.reflections.Reflections in project connect-utils by jcustenborder.

the class TestDataUtils method loadJsonResourceFiles.

public static <T extends NamedTest> List<T> loadJsonResourceFiles(String packageName, Class<T> cls) throws IOException {
    Preconditions.checkNotNull(packageName, "packageName cannot be null");
    Reflections reflections = new Reflections(packageName, new ResourcesScanner());
    Set<String> resources = reflections.getResources(new FilterBuilder.Include(".*"));
    List<T> datas = new ArrayList<>(resources.size());
    Path packagePath = Paths.get("/" + packageName.replace(".", "/"));
    for (String resource : resources) {
        log.trace("Loading resource {}", resource);
        Path resourcePath = Paths.get("/" + resource);
        Path relativePath = packagePath.relativize(resourcePath);
        File resourceFile = new File("/" + resource);
        T data;
        try (InputStream inputStream = cls.getResourceAsStream(resourceFile.getAbsolutePath())) {
            data = ObjectMapperFactory.INSTANCE.readValue(inputStream, cls);
        } catch (IOException ex) {
            if (log.isErrorEnabled()) {
                log.error("Exception thrown while loading {}", resourcePath, ex);
            }
            throw ex;
        }
        String nameWithoutExtension = Files.getNameWithoutExtension(resource);
        if (null != relativePath.getParent()) {
            String parentName = relativePath.getParent().getFileName().toString();
            data.testName(parentName + "/" + nameWithoutExtension);
        } else {
            data.testName(nameWithoutExtension);
        }
        datas.add(data);
    }
    return datas;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ResourcesScanner(org.reflections.scanners.ResourcesScanner) IOException(java.io.IOException) FilterBuilder(org.reflections.util.FilterBuilder) File(java.io.File) Reflections(org.reflections.Reflections)

Example 94 with Reflections

use of org.reflections.Reflections in project TheLighterBot by PhotonBursted.

the class CommandParser method registerCommands.

public void registerCommands() {
    StringBuilder status = new StringBuilder("Registered the following commands:");
    int commandCount, activatedCommandCount = 0;
    Reflections r = new Reflections(Command.class.getPackage().getName());
    Set<Class<?>> commandObjects = r.getTypesAnnotatedWith(AvailableCommand.class);
    commandCount = commandObjects.size();
    for (Class<?> cmdClass : commandObjects) {
        try {
            addCommand((Command) cmdClass.newInstance());
            activatedCommandCount++;
            status.append("\n - ").append(cmdClass.getSimpleName());
        } catch (InstantiationException | IllegalAccessException ex) {
            log.warn(String.format("Something went wrong adding command %s:", cmdClass.getSimpleName()), ex);
        }
    }
    log.info(status.append(String.format("\n\nActivated %s/%s commands succesfully.", activatedCommandCount, commandCount)).toString());
}
Also used : Reflections(org.reflections.Reflections)

Example 95 with Reflections

use of org.reflections.Reflections in project CzechIdMng by bcvsolutions.

the class CzechIdMIcConfigurationService method getAvailableLocalConnectors.

/**
 * Return available local connectors for this IC implementation
 *
 * @return
 */
@SuppressWarnings("unchecked")
@Override
public Set<IcConnectorInfo> getAvailableLocalConnectors() {
    if (connectorInfos == null) {
        connectorInfos = new HashSet<>();
        connectorsConfigurations = new HashMap<>();
        connectorsClass = new HashMap<>();
        List<Class<?>> annotated = new ArrayList<>();
        // Find all class with annotation IcConnectorClass under specific
        // packages
        localConnectorsPackages.forEach(packageWithConnectors -> {
            Reflections reflections = new Reflections(packageWithConnectors);
            annotated.addAll(reflections.getTypesAnnotatedWith(IcConnectorClass.class));
        });
        LOG.info(MessageFormat.format("Found annotated classes with IcConnectorClass [{0}]", annotated));
        for (Class<?> clazz : annotated) {
            IcConnectorClass connectorAnnotation = clazz.getAnnotation(IcConnectorClass.class);
            if (!this.getFramework().equals(connectorAnnotation.framework())) {
                continue;
            }
            if (!IcConnector.class.isAssignableFrom(clazz)) {
                throw new IcException(MessageFormat.format("Cannot create instance of CzechIdM connector [{0}]! Connector class must be child of [{0}]!", IcConnector.class.getSimpleName()));
            }
            IcConnectorInfo info = CzechIdMIcConvertUtil.convertConnectorClass(connectorAnnotation, (Class<? extends IcConnector>) clazz);
            Class<? extends ConfigurationClass> configurationClass = connectorAnnotation.configurationClass();
            connectorInfos.add(info);
            IcConnectorConfiguration configuration = initDefaultConfiguration(configurationClass);
            // Put default configuration to cache
            connectorsConfigurations.put(info.getConnectorKey().getFullName(), configuration);
            // Put connector class to cache
            connectorsClass.put(info.getConnectorKey().getFullName(), ((Class<? extends IcConnector>) clazz));
        }
        LOG.info(MessageFormat.format("Found all local connector connectorInfos [{0}]", connectorInfos.toString()));
    }
    return connectorInfos;
}
Also used : IcConnectorConfiguration(eu.bcvsolutions.idm.ic.api.IcConnectorConfiguration) ArrayList(java.util.ArrayList) IcException(eu.bcvsolutions.idm.ic.exception.IcException) IcConnectorInfo(eu.bcvsolutions.idm.ic.api.IcConnectorInfo) IcConnectorClass(eu.bcvsolutions.idm.ic.api.annotation.IcConnectorClass) ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) IcConnectorClass(eu.bcvsolutions.idm.ic.api.annotation.IcConnectorClass) IcConnector(eu.bcvsolutions.idm.ic.api.IcConnector) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)160 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)54 SubTypesScanner (org.reflections.scanners.SubTypesScanner)41 ArrayList (java.util.ArrayList)26 Set (java.util.Set)23 ResourcesScanner (org.reflections.scanners.ResourcesScanner)21 Test (org.junit.Test)20 FilterBuilder (org.reflections.util.FilterBuilder)20 HashSet (java.util.HashSet)19 URL (java.net.URL)18 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)18 IOException (java.io.IOException)17 Collectors (java.util.stream.Collectors)17 Method (java.lang.reflect.Method)16 List (java.util.List)15 File (java.io.File)13 InputStream (java.io.InputStream)9 Field (java.lang.reflect.Field)9 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)9 ClasspathHelper (org.reflections.util.ClasspathHelper)9