注解開發工具類,主要是根據注解獲取對應的類、屬性和方法,突然的一個想法做出來,我也不知道有沒有用,還有一些想做的功能會在之后慢慢更新,
附Gitee倉庫:AnnotationUtils
package pers.LovelyBunny.AnnotationUtils.utils;
import java.io.File;
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.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
*
* 注解開發工具類
*
* 全域靜態方法呼叫,不需要實體化物件
*
* @author 張澤楠
* @since 2021-06-18
* @version 1.0
*
*/
public class AnnotationUtils {
/** 掃描根路徑{@value} */
private static String rootPath;
/** 類名串列{@value} */
private static List<String> nameList = new ArrayList<String>();
/** 注解串列{@value} */
private static List<Class<Annotation>> annotationList = new ArrayList<Class<Annotation>>();
/** 類表{注解名: 類}{@value} */
private static Map<String, List<Class<?>>> classPool = new HashMap<String, List<Class<?>>>();
/** 介面表{注解名: 介面}{@value} */
private static Map<String, List<Class<?>>> interfacePool = new HashMap<String, List<Class<?>>>();
/** 列舉類表{注解名: 列舉類}{@value} */
private static Map<String, List<Class<?>>> enumPool = new HashMap<String, List<Class<?>>>();
/** 屬性表{注解名: {類名: 屬性串列}}{@value} */
private static Map<String, Map<String, List<Field>>> fieldPool = new HashMap<String, Map<String, List<Field>>>();
/** 方法表{注解名: {類名: 方法串列}}{@value} */
private static Map<String, Map<String, List<Method>>> methodPool = new HashMap<String, Map<String, List<Method>>>();
/**
* 程式加載時自動呼叫初始化函式{@link pers.LovelyBunny.AnnotationUtils.utils.AnnotationUtils#init()}
*/
static {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 不需要實體化物件
*/
private AnnotationUtils() {
super();
}
/**
*
* AnnotationUtils初始化
*
* @throws Exception
*/
private static void init() throws Exception {
System.out.println("----------------AnnotationUtils init begin----------------");
// 獲取class根路徑
String runModel = AnnotationUtils.class.getResource("").getProtocol();
// 判斷運行模式,掃描包獲取所有class的全限定類名,并保存在nameList中
if ("file" == runModel) {
rootPath = AnnotationUtils.class.getResource("/").toString().replace("file:/", "").replace("\\", "/");
scanPackageInFile(new File(rootPath));
} else if ("jar" == runModel) {
rootPath = AnnotationUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
scanPackageInJar();
} else {
System.out.println("----------------AnnotationUtils init failure----------------");
return;
}
// 根據注解和類對所有class分類
sortClass();
System.out.println("----------------AnnotationUtils init finished----------------");
}
/**
*
* 掃描jar包,獲取所有class的全限定類名
*
* @throws IOException
*/
private static void scanPackageInJar() throws IOException {
JarFile jarFile = new JarFile(new File(rootPath));
Enumeration<JarEntry> entrys = jarFile.entries();
while (entrys.hasMoreElements()) {
JarEntry jar = entrys.nextElement();
String name = jar.getName();
if (name.endsWith(".class")) {
nameList.add(name.replace("\\", "/").replace(".class", "").replace("/", "."));
}
}
jarFile.close();
}
/**
*
* 遞回掃描包,獲取所有class的全限定類名
*
* @param nodeFile
* 遞回節點檔案
* @throws Exception
*/
private static void scanPackageInFile(File nodeFile) throws Exception {
File[] filesList = nodeFile.listFiles();
for (File file : filesList) {
if (file.isDirectory()) {
scanPackageInFile(file);
} else {
if (file.getAbsolutePath().endsWith(".class")) {
// 將class路徑轉換為全限定類名
nameList.add(file.getAbsolutePath().replace("\\", "/").replace(rootPath, "").replace(".class", "")
.replace("/", "."));
}
}
}
}
/**
*
* 根據注解和類對所有class分類
*
* @throws Exception
*/
private static void sortClass() throws Exception {
// 獲取所有注解類并保存
for (String className : nameList) {
Class<?> clazz = Class.forName(className);
if (clazz.isAnnotation()) {
System.out.println("[AnnotationUtils] Scanning annotation: " + className);
@SuppressWarnings("unchecked")
Class<Annotation> annotation = (Class<Annotation>) clazz;
annotationList.add(annotation);
}
}
// 遍歷所有類
for (String className : nameList) {
Class<?> clazz = Class.forName(className);
// 跳過所有注解類
if (clazz.isAnnotation()) {
continue;
}
// 獲取當前遍歷類的所有屬性
Field[] fields = clazz.getFields();
// 獲取當前遍歷類的所有方法
Method[] methods = clazz.getMethods();
// 遍歷所有注解
for (Class<Annotation> annotation : annotationList) {
String annotationName = annotation.getName();
// 當前遍歷類擁有當前遍歷注解,則根據注解和當前遍歷類的型別進行分類保存
if (null != clazz.getAnnotation(annotation)) {
if (clazz.isInterface()) {
System.out.println("[AnnotationUtils] Scanning class(interface): " + className);
if (interfacePool.containsKey(annotationName)) {
List<Class<?>> list = interfacePool.get(annotationName);
list.add(clazz);
interfacePool.put(annotationName, list);
} else {
List<Class<?>> list = new ArrayList<Class<?>>();
list.add(clazz);
interfacePool.put(annotationName, list);
}
} else if (clazz.isEnum()) {
System.out.println("[AnnotationUtils] Scanning class(enum): " + className);
if (enumPool.containsKey(annotationName)) {
List<Class<?>> list = enumPool.get(annotationName);
list.add(clazz);
enumPool.put(annotationName, list);
} else {
List<Class<?>> list = new ArrayList<Class<?>>();
list.add(clazz);
enumPool.put(annotationName, list);
}
} else {
System.out.println("[AnnotationUtils] Scanning class: " + className);
if (classPool.containsKey(annotationName)) {
List<Class<?>> list = classPool.get(annotationName);
list.add(clazz);
classPool.put(annotationName, list);
} else {
List<Class<?>> list = new ArrayList<Class<?>>();
list.add(clazz);
classPool.put(annotationName, list);
}
}
}
// 遍歷當前遍歷類的所有屬性
for (Field field : fields) {
// 當前遍歷屬性擁有當前遍歷注解,則根據注解和當前遍歷屬性的所屬類進行分類保存
if (null != field.getAnnotation(annotation)) {
Map<String, List<Field>> map = null;
List<Field> list = null;
if (fieldPool.containsKey(annotationName)) {
map = fieldPool.get(annotationName);
if (map.containsKey(className)) {
list = map.get(className);
} else {
list = new ArrayList<Field>();
}
} else {
map = new HashMap<String, List<Field>>();
list = new ArrayList<Field>();
}
list.add(field);
map.put(className, list);
fieldPool.put(annotationName, map);
}
}
// 遍歷當前遍歷類的所有方法
for (Method method : methods) {
// 當前遍歷屬性擁有當前遍歷注解,則根據注解和當前遍歷屬性的所屬類進行分類保存
if (null != method.getAnnotation(annotation)) {
Map<String, List<Method>> map = null;
List<Method> list = null;
if (methodPool.containsKey(annotationName)) {
map = methodPool.get(annotationName);
if (map.containsKey(className)) {
list = map.get(className);
} else {
list = new ArrayList<Method>();
}
} else {
map = new HashMap<String, List<Method>>();
list = new ArrayList<Method>();
}
list.add(method);
map.put(className, list);
methodPool.put(annotationName, map);
}
}
}
}
}
/**
*
* 根據注解查詢類
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的類的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Class<?>> getClasssByAnnotation(Class<?> annotation) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
List<Class<?>> result = null;
if (classPool.containsKey(annotationName)) {
result = classPool.get(annotationName);
}
return result;
}
/**
*
* 根據注解查詢介面
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的介面的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Class<?>> getInterfacesByAnnotation(Class<?> annotation) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
List<Class<?>> result = null;
if (interfacePool.containsKey(annotationName)) {
result = interfacePool.get(annotationName);
}
return result;
}
/**
*
* 根據注解查詢列舉類
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的列舉類的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Class<?>> getEnumsByAnnotation(Class<?> annotation) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
List<Class<?>> result = null;
if (enumPool.containsKey(annotationName)) {
result = enumPool.get(annotationName);
}
return result;
}
/**
*
* 根據注解查詢屬性,并根據全限定類名分類
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的屬性的HashMap{全限定類名:
* 屬性的ArrayList},如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static Map<String, List<Field>> getFieldsByAnnotationSortByClass(Class<?> annotation) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
Map<String, List<Field>> result = null;
if (fieldPool.containsKey(annotationName)) {
result = fieldPool.get(annotationName);
}
return result;
}
/**
*
* 根據注解查詢方法,并根據全限定類名分類
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的方法的HashMap{全限定類名:
* 方法的ArrayList},如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static Map<String, List<Method>> getMethodsByAnnotationSortByClass(Class<?> annotation) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
Map<String, List<Method>> result = null;
if (methodPool.containsKey(annotationName)) {
result = methodPool.get(annotationName);
}
return result;
}
/**
*
* 根據注解查詢屬性
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的屬性的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Field> getFieldsByAnnotation(Class<?> annotation) {
// 獲取根據全限定類名分類的所有擁有所查詢注解的屬性的HashMap,如果未查詢到結果,則回傳null
Map<String, List<Field>> temp = getFieldsByAnnotationSortByClass(annotation);
if (null == temp) {
return null;
}
// 將所有查詢到的屬性整合為一個ArrayList
List<Field> result = new ArrayList<>();
for (List<Field> list : temp.values()) {
result.addAll(list);
}
return result;
}
/**
*
* 根據注解查詢方法
*
* @param annotation
* 所查詢的注解類
* @return 所有擁有所查詢注解的方法的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Method> getMethodsByAnnotation(Class<?> annotation) {
// 獲取根據全限定類名分類的所有擁有所查詢注解的方法的HashMap,如果未查詢到結果,則回傳null
Map<String, List<Method>> temp = getMethodsByAnnotationSortByClass(annotation);
if (null == temp) {
return null;
}
// 將所有查詢到的方法整合為一個ArrayList
List<Method> result = new ArrayList<>();
for (List<Method> list : temp.values()) {
result.addAll(list);
}
return result;
}
/**
*
* 根據注解和類查詢屬性
*
* @param annotation
* 所查詢的注解類
* @param clazz
* 所查詢的類
* @return 所有查詢類下所有擁有所查詢注解的屬性的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Field> getFieldByAnnotationAndClass(Class<?> annotation, Class<?> clazz) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
String className = clazz.getName();
List<Field> result = null;
if (fieldPool.containsKey(annotationName)) {
Map<String, List<Field>> map = fieldPool.get(annotationName);
if (map.containsKey(className)) {
result = map.get(className);
}
}
return result;
}
/**
*
* 根據注解和類查詢方法
*
* @param annotation
* 所查詢的注解類
* @param clazz
* 所查詢的類
* @return 所有查詢類下所有擁有所查詢注解的方法的ArrayList,如果未查詢到結果或所傳入類不是一個注解,則回傳{@code null}
*/
public static List<Method> getMethodByAnnotationAndClass(Class<?> annotation, Class<?> clazz) {
// 判斷傳入類是否為注解
if (!annotation.isAnnotation()) {
return null;
}
String annotationName = annotation.getName();
String className = clazz.getName();
List<Method> result = null;
if (methodPool.containsKey(annotationName)) {
Map<String, List<Method>> map = methodPool.get(annotationName);
if (map.containsKey(className)) {
result = map.get(className);
}
}
return result;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/333714.html
標籤:其他
