ClassScaner.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package cn.com.taiji.zhongxiao.handle;
  2. import java.io.IOException;
  3. import java.lang.annotation.Annotation;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Set;
  8. import org.apache.commons.lang3.ArrayUtils;
  9. import org.springframework.beans.factory.BeanDefinitionStoreException;
  10. import org.springframework.context.ResourceLoaderAware;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.core.io.ResourceLoader;
  13. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  14. import org.springframework.core.io.support.ResourcePatternResolver;
  15. import org.springframework.core.io.support.ResourcePatternUtils;
  16. import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
  17. import org.springframework.core.type.classreading.MetadataReader;
  18. import org.springframework.core.type.classreading.MetadataReaderFactory;
  19. import org.springframework.core.type.filter.AnnotationTypeFilter;
  20. import org.springframework.core.type.filter.TypeFilter;
  21. import org.springframework.util.StringUtils;
  22. import org.springframework.util.SystemPropertyUtils;
  23. public class ClassScaner implements ResourceLoaderAware {
  24. private final List<TypeFilter> includeFilters = new LinkedList<TypeFilter>();
  25. private final List<TypeFilter> excludeFilters = new LinkedList<TypeFilter>();
  26. private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
  27. private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
  28. @SafeVarargs
  29. public static Set<Class<?>> scan(String[] basePackages, Class<? extends Annotation>... annotations) {
  30. ClassScaner cs = new ClassScaner();
  31. if (ArrayUtils.isNotEmpty(annotations)) {
  32. for (Class anno : annotations) {
  33. cs.addIncludeFilter(new AnnotationTypeFilter(anno));
  34. }
  35. }
  36. Set<Class<?>> classes = new HashSet<>();
  37. for (String s : basePackages) {
  38. classes.addAll(cs.doScan(s));
  39. }
  40. return classes;
  41. }
  42. @SafeVarargs
  43. public static Set<Class<?>> scan(String basePackages, Class<? extends Annotation>... annotations) {
  44. return ClassScaner.scan(StringUtils.tokenizeToStringArray(basePackages, ",; \t\n"), annotations);
  45. }
  46. public final ResourceLoader getResourceLoader() {
  47. return this.resourcePatternResolver;
  48. }
  49. @Override
  50. public void setResourceLoader(ResourceLoader resourceLoader) {
  51. this.resourcePatternResolver = ResourcePatternUtils
  52. .getResourcePatternResolver(resourceLoader);
  53. this.metadataReaderFactory = new CachingMetadataReaderFactory(
  54. resourceLoader);
  55. }
  56. public void addIncludeFilter(TypeFilter includeFilter) {
  57. this.includeFilters.add(includeFilter);
  58. }
  59. public void addExcludeFilter(TypeFilter excludeFilter) {
  60. this.excludeFilters.add(0, excludeFilter);
  61. }
  62. public void resetFilters(boolean useDefaultFilters) {
  63. this.includeFilters.clear();
  64. this.excludeFilters.clear();
  65. }
  66. public Set<Class<?>> doScan(String basePackage) {
  67. Set<Class<?>> classes = new HashSet<>();
  68. try {
  69. String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
  70. + org.springframework.util.ClassUtils
  71. .convertClassNameToResourcePath(SystemPropertyUtils
  72. .resolvePlaceholders(basePackage))
  73. + "/**/*.class";
  74. Resource[] resources = this.resourcePatternResolver
  75. .getResources(packageSearchPath);
  76. for (int i = 0; i < resources.length; i++) {
  77. Resource resource = resources[i];
  78. if (resource.isReadable()) {
  79. MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
  80. if ((includeFilters.size() == 0 && excludeFilters.size() == 0) || matches(metadataReader)) {
  81. try {
  82. classes.add(Class.forName(metadataReader
  83. .getClassMetadata().getClassName()));
  84. } catch (ClassNotFoundException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }
  90. } catch (IOException ex) {
  91. throw new BeanDefinitionStoreException(
  92. "I/O failure during classpath scanning", ex);
  93. }
  94. return classes;
  95. }
  96. protected boolean matches(MetadataReader metadataReader) throws IOException {
  97. for (TypeFilter tf : this.excludeFilters) {
  98. if (tf.match(metadataReader, this.metadataReaderFactory)) {
  99. return false;
  100. }
  101. }
  102. for (TypeFilter tf : this.includeFilters) {
  103. if (tf.match(metadataReader, this.metadataReaderFactory)) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. }