Example usage for org.springframework.core.type ClassMetadata getInterfaceNames

List of usage examples for org.springframework.core.type ClassMetadata getInterfaceNames

Introduction

In this page you can find the example usage for org.springframework.core.type ClassMetadata getInterfaceNames.

Prototype

String[] getInterfaceNames();

Source Link

Document

Return the names of all interfaces that the underlying class implements, or an empty array if there are none.

Usage

From source file:com.reactive.hzdfs.utils.EntityFinder.java

/**
 * Find implementation classes for the given interface.
 * @param <T>/*from  ww  w.j  a  v  a2 s. c  om*/
 * @param basePkg
 * @param baseInterface
 * @return
 * @throws ClassNotFoundException
 */
@SuppressWarnings("unchecked")
public static <T> List<Class<T>> findImplementationClasses(String basePkg, final Class<T> baseInterface)
        throws ClassNotFoundException {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false);
    provider.addIncludeFilter(new TypeFilter() {

        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
                throws IOException {
            ClassMetadata aMeta = metadataReader.getClassMetadata();
            String[] intf = aMeta.getInterfaceNames();
            Arrays.sort(intf);
            return Arrays.binarySearch(intf, baseInterface.getName()) >= 0;
        }
    });

    Set<Class<?>> collection = findComponents(provider, basePkg);
    List<Class<T>> list = new ArrayList<>(collection.size());
    for (Class<?> c : collection) {
        list.add((Class<T>) c);
    }
    return list;

}

From source file:org.syncope.core.rest.controller.ConfigurationController.java

@PreAuthorize("hasRole('CONFIGURATION_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/validators")
public ModelAndView getValidators() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> validators = new HashSet<String>();
    try {//w w w . j a v  a  2 s  .  c  o  m
        for (Resource resource : resResolver
                .getResources("classpath:org/syncope/core/persistence/validation/" + "attrvalue/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), Validator.class.getName())
                    || AbstractValidator.class.getName().equals(metadata.getSuperClassName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers())) {
                        validators.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Validator.class.getName(), e);
    }

    return new ModelAndView().addObject(validators);
}

From source file:com.haulmont.cuba.core.config.AppPropertiesLocator.java

protected Set<Class> findConfigInterfaces() {
    if (interfacesCache == null) {
        synchronized (this) {
            if (interfacesCache == null) {
                log.trace("Locating config interfaces");
                Set<String> cache = new HashSet<>();
                for (String rootPackage : metadata.getRootPackages()) {
                    String packagePrefix = rootPackage.replace(".", "/") + "/**/*.class";
                    String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + packagePrefix;
                    Resource[] resources;
                    try {
                        resources = resourcePatternResolver.getResources(packageSearchPath);
                        for (Resource resource : resources) {
                            if (resource.isReadable()) {
                                MetadataReader metadataReader = metadataReaderFactory
                                        .getMetadataReader(resource);
                                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                                if (classMetadata.isInterface()) {
                                    for (String intf : classMetadata.getInterfaceNames()) {
                                        if (Config.class.getName().equals(intf)) {
                                            cache.add(classMetadata.getClassName());
                                            break;
                                        }
                                    }//  w  ww  . j a  va  2  s.  c om
                                }
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException("Error searching for Config interfaces", e);
                    }
                }
                log.trace("Found config interfaces: {}", cache);
                interfacesCache = cache;
            }
        }
    }
    return interfacesCache.stream().map(ReflectionHelper::getClass).collect(Collectors.toSet());
}

From source file:org.syncope.core.rest.controller.ReportController.java

@PreAuthorize("hasRole('REPORT_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/reportletClasses")
public ModelAndView getReportletClasses() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> reportletClasses = new HashSet<String>();
    try {/*from   ww w.  j  ava  2  s.c  om*/
        for (Resource resource : resResolver.getResources("classpath*:**/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), Reportlet.class.getName())
                    || AbstractReportlet.class.getName().equals(metadata.getSuperClassName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers())) {

                        reportletClasses.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Reportlet.class.getName(), e);
    }

    ModelAndView result = new ModelAndView();
    result.addObject(reportletClasses);
    return result;
}

From source file:org.syncope.core.rest.controller.TaskController.java

@PreAuthorize("hasRole('TASK_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/jobActionsClasses")
public ModelAndView getJobActionClasses() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> jobActionsClasses = new HashSet<String>();
    try {// ww  w . ja v  a2s .  co  m
        for (Resource resource : resResolver.getResources("classpath*:**/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), SyncJobActions.class.getName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers())) {
                        jobActionsClasses.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", SyncJobActions.class.getName(), e);
    }

    ModelAndView result = new ModelAndView();
    result.addObject(jobActionsClasses);
    return result;
}

From source file:org.syncope.core.rest.controller.TaskController.java

@PreAuthorize("hasRole('TASK_LIST')")
@RequestMapping(method = RequestMethod.GET, value = "/jobClasses")
public ModelAndView getJobClasses() {
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    Set<String> jobClasses = new HashSet<String>();
    try {/*w w w  . ja v  a 2s  .  c  o  m*/
        for (Resource resource : resResolver.getResources("classpath*:**/*.class")) {

            ClassMetadata metadata = cachingMetadataReaderFactory.getMetadataReader(resource)
                    .getClassMetadata();
            if (ArrayUtils.contains(metadata.getInterfaceNames(), Job.class.getName())
                    || AbstractTaskJob.class.getName().equals(metadata.getSuperClassName())
                    || ArrayUtils.contains(metadata.getInterfaceNames(), StatefulJob.class.getName())) {

                try {
                    Class jobClass = Class.forName(metadata.getClassName());
                    if (!Modifier.isAbstract(jobClass.getModifiers()) && !metadata.hasEnclosingClass()
                            && !jobClass.equals(SyncJob.class) && !jobClass.equals(ReportJob.class)
                            && !jobClass.equals(NotificationJob.class)) {

                        jobClasses.add(jobClass.getName());
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error("Could not load class {}", metadata.getClassName(), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("While searching for class implementing {}", Job.class.getName(), e);
    }

    ModelAndView result = new ModelAndView();
    result.addObject(jobClasses);
    return result;
}

From source file:org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter.java

@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
        throws IOException {

    // This method optimizes avoiding unnecessary creation of ClassReaders
    // as well as visiting over those readers.
    if (matchSelf(metadataReader)) {
        return true;
    }/*  www. ja va2s.c o  m*/
    ClassMetadata metadata = metadataReader.getClassMetadata();
    if (matchClassName(metadata.getClassName())) {
        return true;
    }

    if (this.considerInherited) {
        String superClassName = metadata.getSuperClassName();
        if (superClassName != null) {
            // Optimization to avoid creating ClassReader for super class.
            Boolean superClassMatch = matchSuperClass(superClassName);
            if (superClassMatch != null) {
                if (superClassMatch.booleanValue()) {
                    return true;
                }
            } else {
                // Need to read super class to determine a match...
                try {
                    if (match(metadata.getSuperClassName(), metadataReaderFactory)) {
                        return true;
                    }
                } catch (IOException ex) {
                    logger.debug("Could not read super class [" + metadata.getSuperClassName()
                            + "] of type-filtered class [" + metadata.getClassName() + "]");
                }
            }
        }
    }

    if (this.considerInterfaces) {
        for (String ifc : metadata.getInterfaceNames()) {
            // Optimization to avoid creating ClassReader for super class
            Boolean interfaceMatch = matchInterface(ifc);
            if (interfaceMatch != null) {
                if (interfaceMatch.booleanValue()) {
                    return true;
                }
            } else {
                // Need to read interface to determine a match...
                try {
                    if (match(ifc, metadataReaderFactory)) {
                        return true;
                    }
                } catch (IOException ex) {
                    logger.debug("Could not read interface [" + ifc + "] for type-filtered class ["
                            + metadata.getClassName() + "]");
                }
            }
        }
    }

    return false;
}