PackageHelper.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2011-2014, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.extension.toolkit;
  17. import java.io.File;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import org.springframework.core.io.Resource;
  21. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  22. import org.springframework.core.io.support.ResourcePatternResolver;
  23. import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
  24. import org.springframework.core.type.classreading.MetadataReader;
  25. import org.springframework.core.type.classreading.MetadataReaderFactory;
  26. import org.springframework.util.ClassUtils;
  27. import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
  28. /**
  29. * 包扫描辅助类
  30. *
  31. * @author hubin
  32. * @since 2016-06-16
  33. */
  34. public class PackageHelper {
  35. /**
  36. * 别名通配符设置
  37. * <p>
  38. * &lt;property name="typeAliasesPackage" value="com.baomidou.*.entity"/&gt;
  39. * </p>
  40. *
  41. * @param typeAliasesPackage 类别名包路径
  42. * @return
  43. */
  44. public static String[] convertTypeAliasesPackage(String typeAliasesPackage) {
  45. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  46. MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
  47. String pkg = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
  48. + ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/*.class";
  49. /*
  50. * 将加载多个绝对匹配的所有Resource
  51. * 将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分,然后进行遍历模式匹配,排除重复包路径
  52. */
  53. try {
  54. Set<String> set = new HashSet<>();
  55. Resource[] resources = resolver.getResources(pkg);
  56. if (resources != null && resources.length > 0) {
  57. MetadataReader metadataReader;
  58. for (Resource resource : resources) {
  59. if (resource.isReadable()) {
  60. metadataReader = metadataReaderFactory.getMetadataReader(resource);
  61. set.add(ClassUtils.getPackageName(metadataReader.getClassMetadata().getClassName()));
  62. }
  63. }
  64. }
  65. if (!set.isEmpty()) {
  66. return set.toArray(new String[]{});
  67. }
  68. return new String[0];
  69. } catch (Exception e) {
  70. throw ExceptionUtils.mpe("not find typeAliasesPackage: %s", e, pkg);
  71. }
  72. }
  73. /**
  74. * 扫描获取指定包路径所有类
  75. *
  76. * @param typePackage 扫描类包路径
  77. * @return ignore
  78. */
  79. public static Set<Class> scanTypePackage(String typePackage) {
  80. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  81. MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
  82. String pkg = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
  83. + ClassUtils.convertClassNameToResourcePath(typePackage) + "/*.class";
  84. /*
  85. * 将加载多个绝对匹配的所有Resource
  86. * 将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分,然后进行遍历模式匹配,排除重复包路径
  87. */
  88. try {
  89. Set<Class> set = new HashSet<>();
  90. Resource[] resources = resolver.getResources(pkg);
  91. if (resources != null && resources.length > 0) {
  92. MetadataReader metadataReader;
  93. for (Resource resource : resources) {
  94. if (resource.isReadable()) {
  95. metadataReader = metadataReaderFactory.getMetadataReader(resource);
  96. set.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
  97. }
  98. }
  99. }
  100. return set;
  101. } catch (Exception e) {
  102. throw ExceptionUtils.mpe("not find scanTypePackage: %s", e, pkg);
  103. }
  104. }
  105. /**
  106. * 新建文件目录
  107. *
  108. * @param file 文件
  109. */
  110. public static void mkDir(File file) {
  111. file.mkdirs();
  112. }
  113. }