|
| 1 | +package com.codingapi.txlcn.tc.core.transaction.txc.analy.util; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.JarURLConnection; |
| 9 | +import java.net.URL; |
| 10 | +import java.net.URLDecoder; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Enumeration; |
| 13 | +import java.util.List; |
| 14 | +import java.util.jar.JarEntry; |
| 15 | +import java.util.jar.JarFile; |
| 16 | + |
| 17 | +/** |
| 18 | + |
| 19 | + * @since 2019/7/29 |
| 20 | + */ |
| 21 | +public class ClassUtils { |
| 22 | + |
| 23 | + private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtils.class); |
| 24 | + |
| 25 | + private static final String FILE = "file"; |
| 26 | + private static final String JAR = "jar"; |
| 27 | + private static final String CLASS_SIGN = ".class"; |
| 28 | + |
| 29 | + /** |
| 30 | + * 从包package中获取所有的Class |
| 31 | + * |
| 32 | + * @param packageName packageName |
| 33 | + * @return List |
| 34 | + */ |
| 35 | + public static List<Class<?>> getClasses(String packageName) { |
| 36 | + List<Class<?>> classes = new ArrayList<>(); |
| 37 | + boolean recursive = true; |
| 38 | + String packageDirName = packageName.replace('.', '/'); |
| 39 | + Enumeration<URL> dirs; |
| 40 | + try { |
| 41 | + dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); |
| 42 | + while (dirs.hasMoreElements()) { |
| 43 | + URL url = dirs.nextElement(); |
| 44 | + String protocol = url.getProtocol(); |
| 45 | + if (FILE.equals(protocol)) { |
| 46 | + String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); |
| 47 | + findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); |
| 48 | + } else if (JAR.equals(protocol)) { |
| 49 | + JarFile jar; |
| 50 | + try { |
| 51 | + jar = ((JarURLConnection) url.openConnection()).getJarFile(); |
| 52 | + Enumeration<JarEntry> entries = jar.entries(); |
| 53 | + while (entries.hasMoreElements()) { |
| 54 | + JarEntry entry = entries.nextElement(); |
| 55 | + String name = entry.getName(); |
| 56 | + if (name.charAt(0) == '/') { |
| 57 | + name = name.substring(1); |
| 58 | + } |
| 59 | + if (name.startsWith(packageDirName)) { |
| 60 | + int idx = name.lastIndexOf('/'); |
| 61 | + if (idx != -1) { |
| 62 | + packageName = name.substring(0, idx).replace('/', '.'); |
| 63 | + } |
| 64 | + if (idx != -1) { |
| 65 | + if (name.endsWith(CLASS_SIGN) && !entry.isDirectory()) { |
| 66 | + // 去掉后面的".class" 获取真正的类名 |
| 67 | + String className = name.substring(packageName.length() + 1, name.length() - 6); |
| 68 | + try { |
| 69 | + classes.add(Class.forName(packageName + '.' + className)); |
| 70 | + } catch (ClassNotFoundException e) { |
| 71 | + LOGGER.error(e.getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } catch (IOException e) { |
| 78 | + LOGGER.error(e.getMessage()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } catch (IOException e) { |
| 83 | + LOGGER.error(e.getMessage()); |
| 84 | + } |
| 85 | + return classes; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * 以文件的形式来获取包下的所有Class |
| 90 | + * |
| 91 | + * @param packageName packageName |
| 92 | + * @param packagePath packagePath |
| 93 | + * @param recursive recursive |
| 94 | + * @param classes classes |
| 95 | + */ |
| 96 | + private static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, List<Class<?>> classes) { |
| 97 | + File dir = new File(packagePath); |
| 98 | + if (!dir.exists() || !dir.isDirectory()) { |
| 99 | + return; |
| 100 | + } |
| 101 | + File[] dirFiles = dir.listFiles(file -> (recursive && file.isDirectory()) || (file.getName().endsWith(CLASS_SIGN))); |
| 102 | + assert dirFiles != null; |
| 103 | + for (File file : dirFiles) { |
| 104 | + if (file.isDirectory()) { |
| 105 | + findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes); |
| 106 | + } else { |
| 107 | + String className = file.getName().substring(0, file.getName().length() - 6); |
| 108 | + try { |
| 109 | + classes.add(Class.forName(packageName + '.' + className)); |
| 110 | + } catch (ClassNotFoundException e) { |
| 111 | + LOGGER.error(e.getMessage()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments