Search in sources :

Example 21 with Reflections

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

the class ClassPathScanner method scan.

/**
   *
   * @param pathsToScan the locations to scan for .class files
   * @param packagePrefixes the whitelist of package prefixes to scan
   * @param parentResult if there was a prescan, its result
   * @return the merged scan
   */
static ScanResult scan(Collection<URL> pathsToScan, Collection<String> packagePrefixes, Collection<String> scannedClasses, Collection<String> scannedAnnotations, ScanResult parentResult) {
    Stopwatch watch = Stopwatch.createStarted();
    try {
        AnnotationScanner annotationScanner = new AnnotationScanner(scannedAnnotations);
        SubTypesScanner subTypesScanner = new SubTypesScanner(parentResult.getImplementations());
        if (packagePrefixes.size() > 0) {
            final FilterBuilder filter = new FilterBuilder();
            for (String prefix : packagePrefixes) {
                filter.include(FilterBuilder.prefix(prefix));
            }
            ConfigurationBuilder conf = new ConfigurationBuilder().setUrls(pathsToScan).setMetadataAdapter(// Scanners depend on this
            METADATA_ADAPTER).filterInputsBy(filter).setScanners(annotationScanner, subTypesScanner);
            // scans stuff, but don't use the funky storage layer
            new Reflections(conf);
        }
        List<ParentClassDescriptor> implementations = new ArrayList<>();
        for (String baseTypeName : scannedClasses) {
            implementations.add(new ParentClassDescriptor(baseTypeName, new ArrayList<>(subTypesScanner.getChildrenOf(baseTypeName))));
        }
        List<AnnotatedClassDescriptor> annotated = annotationScanner.getAnnotatedClasses();
        verifyClassUnicity(annotated, pathsToScan);
        return new ScanResult(packagePrefixes, scannedClasses, scannedAnnotations, annotated, implementations);
    } finally {
        logger.info(format("Scanning packages %s in locations %s took %dms", packagePrefixes, pathsToScan, watch.elapsed(MILLISECONDS)));
    }
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) FilterBuilder(org.reflections.util.FilterBuilder) ParentClassDescriptor(org.apache.drill.common.scanner.persistence.ParentClassDescriptor) Reflections(org.reflections.Reflections)

Example 22 with Reflections

use of org.reflections.Reflections in project ocvn by devgateway.

the class ReflectionsConfiguration method reflections.

@Bean
public Reflections reflections() {
    logger.debug("Starting reflections scanners...");
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("org.devgateway.ocds.persistence.mongo")).setScanners(new SubTypesScanner(), new FieldAnnotationsScanner(), new MethodParameterScanner()));
    logger.debug("Configured reflections bean.");
    return reflections;
}
Also used : MethodParameterScanner(org.reflections.scanners.MethodParameterScanner) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections) Bean(org.springframework.context.annotation.Bean)

Example 23 with Reflections

use of org.reflections.Reflections in project Dempsy by Dempsy.

the class Manager method makeInstance.

public T makeInstance(final String typeId) {
    // There's an issue opened on Reflections where multi-threaded access to the zip file is broken.
    // see: https://github.com/ronmamo/reflections/issues/81
    final Reflections reflections;
    final Set<Class<? extends T>> senderFactoryClasses;
    synchronized (Reflections.class) {
        // try something stupid like assume it's a package name and the sender factory is in that package
        reflections = new Reflections(typeId + ".");
        senderFactoryClasses = reflections.getSubTypesOf(clazz);
    }
    T ret = null;
    if (senderFactoryClasses != null && senderFactoryClasses.size() > 0) {
        final Class<? extends T> sfClass = senderFactoryClasses.iterator().next();
        if (senderFactoryClasses.size() > 1)
            LOGGER.warn("Multiple " + clazz.getSimpleName() + " implementations in the package \"{}\". Going with {}", typeId, sfClass.getName());
        try {
            ret = sfClass.newInstance();
        } catch (final InstantiationException | IllegalAccessException e) {
            throw new DempsyException("Failed to create an instance of the " + clazz.getSimpleName() + " \"" + sfClass.getName() + "\". Is there a default constructor?", e, false);
        }
    }
    if (ret == null)
        throw new DempsyException("Couldn't find a " + clazz.getSimpleName() + " registered with transport type id \"" + typeId + "\" and couldn't find an implementing class assuming the transport type id is a package name");
    return ret;
}
Also used : Reflections(org.reflections.Reflections)

Example 24 with Reflections

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

the class ConnIdIcConfigurationService method findAllLocalConnectorManagers.

private List<ConnectorInfoManager> findAllLocalConnectorManagers() {
    if (managers == null) {
        managers = new ArrayList<>();
        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(ConnectorClass.class));
        });
        LOG.info(MessageFormat.format("Found annotated classes with IcConnectorClass [{0}]", annotated));
        for (Class<?> clazz : annotated) {
            URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
            ConnectorInfoManagerFactory fact = ConnectorInfoManagerFactory.getInstance();
            ConnectorInfoManager manager = fact.getLocalManager(url);
            managers.add(manager);
        }
        LOG.info(MessageFormat.format("Found all local connector managers [{0}]", managers.toString()));
    }
    return managers;
}
Also used : ConnectorInfoManagerFactory(org.identityconnectors.framework.api.ConnectorInfoManagerFactory) ConnectorInfoManager(org.identityconnectors.framework.api.ConnectorInfoManager) ConnectorClass(org.identityconnectors.framework.spi.ConnectorClass) ArrayList(java.util.ArrayList) ConnectorClass(org.identityconnectors.framework.spi.ConnectorClass) URL(java.net.URL) Reflections(org.reflections.Reflections)

Example 25 with Reflections

use of org.reflections.Reflections in project cas by apereo.

the class JpaTicketRegistryConfiguration method ticketPackagesToScan.

@Bean
public List<String> ticketPackagesToScan() {
    final Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(CentralAuthenticationService.NAMESPACE)).setScanners(new SubTypesScanner(false)));
    final Set<Class<?>> subTypes = (Set) reflections.getSubTypesOf(AbstractTicket.class);
    final List<String> packages = subTypes.stream().map(t -> t.getPackage().getName()).collect(Collectors.toList());
    return packages;
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) JpaLockingStrategy(org.apereo.cas.ticket.registry.support.JpaLockingStrategy) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) Autowired(org.springframework.beans.factory.annotation.Autowired) Beans(org.apereo.cas.configuration.support.Beans) Reflections(org.reflections.Reflections) TicketRegistryProperties(org.apereo.cas.configuration.model.core.ticket.registry.TicketRegistryProperties) StringUtils(org.apache.commons.lang3.StringUtils) EnableTransactionManagement(org.springframework.transaction.annotation.EnableTransactionManagement) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) TicketRegistry(org.apereo.cas.ticket.registry.TicketRegistry) LockingStrategy(org.apereo.cas.ticket.registry.support.LockingStrategy) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Qualifier(org.springframework.beans.factory.annotation.Qualifier) DataSource(javax.sql.DataSource) InetAddressUtils(org.apereo.cas.util.InetAddressUtils) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) TicketCatalog(org.apereo.cas.ticket.TicketCatalog) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) JpaTicketRegistryProperties(org.apereo.cas.configuration.model.support.jpa.ticketregistry.JpaTicketRegistryProperties) JpaConfigDataHolder(org.apereo.cas.configuration.model.support.jpa.JpaConfigDataHolder) Set(java.util.Set) CoreTicketUtils(org.apereo.cas.util.CoreTicketUtils) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) SubTypesScanner(org.reflections.scanners.SubTypesScanner) JpaTicketRegistry(org.apereo.cas.ticket.registry.JpaTicketRegistry) Configuration(org.springframework.context.annotation.Configuration) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) Lazy(org.springframework.context.annotation.Lazy) AbstractTicket(org.apereo.cas.ticket.AbstractTicket) Bean(org.springframework.context.annotation.Bean) JpaBeans(org.apereo.cas.configuration.support.JpaBeans) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Set(java.util.Set) AbstractTicket(org.apereo.cas.ticket.AbstractTicket) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

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