1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternUtils; import org.springframework.core.type.ClassMetadata; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.util.ClassUtils;
import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.ReentrantLock;
public class AnnotationUtils {
private static final Logger logger = LoggerFactory.getLogger(AnnotationUtils.class); static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; private static AnnotationUtils mInstance; private static final ReentrantLock LOCKER = new ReentrantLock();
public static AnnotationUtils getInstance() { if (mInstance == null) { LOCKER.lock(); if (mInstance == null) { mInstance = new AnnotationUtils(); } LOCKER.unlock(); } return mInstance; }
public List<Class<?>> getClassCandidates(String basePackages, Class clazz) { List<Class<?>> candidates = scanPackagesForClass(basePackages, clazz); validateCandidates(basePackages, candidates.isEmpty()); return candidates; }
public List<Method> getMethodCandidates(String basePackages, Class clazz) { List<Method> candidates = scanPackagesForMethod(basePackages, clazz); validateCandidates(basePackages, candidates.isEmpty()); return candidates; }
public List<Field> getFieldCandidates(String basePackages, Class clazz) { List<Field> candidates = scanPackagesForField(basePackages, clazz); validateCandidates(basePackages, candidates.isEmpty()); return candidates; }
public List<Class<?>> scanPackagesForClass(String basePackages, Class clazz) { List<Class<?>> candidates = new ArrayList<Class<?>>(); try { candidates.addAll(findCandidateClasses(basePackages, clazz)); } catch (IOException e) { logger.error("扫描指定包时出现异常", basePackages); } return candidates; }
public List<Method> scanPackagesForMethod(String basePackages, Class clazz) { List<Method> candidates = new ArrayList<Method>(); try { candidates.addAll(findCandidateMethod(basePackages, clazz)); } catch (IOException e) { logger.error("扫描指定包时出现异常", basePackages); } return candidates; }
public List<Field> scanPackagesForField(String basePackages, Class clazz) { List<Field> candidates = new ArrayList<Field>(); try { candidates.addAll(findCandidateField(basePackages, clazz)); } catch (IOException e) { logger.error("扫描指定包时出现异常", basePackages); } return candidates; }
public List<Class<?>> findCandidateClasses(String basePackage, Class clazz) throws IOException { List<Class<?>> candidates = new ArrayList<Class<?>>(); String packageSearchPath = getPackageSearchPath(basePackage); ResourceLoader resourceLoader = new DefaultResourceLoader(); MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory(resourceLoader); Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(packageSearchPath); for (Resource resource : resources) { MetadataReader reader = readerFactory.getMetadataReader(resource); if (match(reader.getClassMetadata(), clazz)) { Class<?> candidateClass = transform(reader.getClassMetadata().getClassName()); if (candidateClass != null) { candidates.add(candidateClass); logger.debug("扫描到符合要求的类:" + candidateClass.getName()); } } } return candidates; }
public List<Method> findCandidateMethod(String basePackage, Class clazz) throws IOException { List<Method> candidates = new ArrayList<Method>(); String packageSearchPath = getPackageSearchPath(basePackage); ResourceLoader resourceLoader = new DefaultResourceLoader(); MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory(resourceLoader); Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(packageSearchPath); for (Resource resource : resources) { MetadataReader reader = readerFactory.getMetadataReader(resource); matchMethod(clazz, candidates, reader); } return candidates; }
public List<Field> findCandidateField(String basePackage, Class clazz) throws IOException { List<Field> candidates = new ArrayList<Field>(); String packageSearchPath = getPackageSearchPath(basePackage); ResourceLoader resourceLoader = new DefaultResourceLoader(); MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory(resourceLoader); Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(packageSearchPath); for (Resource resource : resources) { MetadataReader reader = readerFactory.getMetadataReader(resource); matchField(clazz, candidates, reader); } return candidates; }
private void matchMethod(Class clazz, List<Method> candidates, MetadataReader reader) { Class<?> clz = transformToClass(reader.getClassMetadata().getClassName()); Method[] methods = clz.getMethods(); for (Method method : methods) { Annotation annotation = method.getAnnotation(clazz);
if (annotation == null) { continue; } if (method != null && method.isAnnotationPresent(clazz)) { candidates.add(method); logger.debug("扫描到符合要求的method:" + method.getName()); } } }
private void matchField(Class clazz, List<Field> candidates, MetadataReader reader) { Class<?> clz = transformToClass(reader.getClassMetadata().getClassName()); Field[] declaredFields = clz.getDeclaredFields(); for (Field field : declaredFields) { Annotation annotation = field.getAnnotation(clazz);
if (annotation == null) { continue; } if (field != null && field.isAnnotationPresent(clazz)) { candidates.add(field); logger.debug("扫描到符合要求的field:" + field.getName()); } } }
public String convertPath(String path) { return StringUtils.replace(path, ".", "/"); }
public Class<?> transform(String className) { Class<?> clazz = null; try { clazz = ClassUtils.forName(className, this.getClass().getClassLoader()); } catch (ClassNotFoundException e) { logger.info("未找到指定类", className); } return clazz; }
public boolean match(ClassMetadata metadata, Class c) { Class<?> clazz = transformToClass(metadata.getClassName()); return clazz != null && clazz.isAnnotationPresent(c); }
public Class<?> transformToClass(String className) { Class<?> clazz = null; try { clazz = ClassUtils.forName(className, this.getClass().getClassLoader()); } catch (ClassNotFoundException e) { logger.info("未找到指定类", className); } return clazz; }
private void validateCandidates(String basePackages, boolean empty) { if (empty) { logger.info("扫描指定包下,未发现符合条件的类", basePackages); } }
private String getPackageSearchPath(String basePackage) { return ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + convertPath(basePackage) + '/' + DEFAULT_RESOURCE_PATTERN; } }
|