package com.ls.utils; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; /** * 注解工具类 * * @author caozj * */ public class AnnotationUtil { /** * 获取类上所有的注解 * * @param c * @return */ public static Annotation[] getClassAnnotations(Class<?> c) { return c.getAnnotations(); } /** * 获取类上指定的注解 * * @param c * @param annotationClass * @return */ public static <T extends Annotation> T getClassAnnotation(Class<?> c, Class<T> annotationClass) { return c.getAnnotation(annotationClass); } /** * 获取属性上所有的注解 * * @param c * @param field * @return */ public static Annotation[] getFiledAnnotions(Class<?> c, String field) { Field[] fields = c.getDeclaredFields(); Field f = null; for (Field fi : fields) { if (fi.getName().equals(field)) { f = fi; break; } } if (f == null) { return null; } return f.getAnnotations(); } /** * 获取属性上指定的注解 * * @param c * @param field * @param annotationClass * @return */ public static <T extends Annotation> T getFiledAnnotion(Class<?> c, String field, Class<T> annotationClass) { Field[] fields = c.getDeclaredFields(); Field f = null; for (Field fi : fields) { if (fi.getName().equals(field)) { f = fi; break; } } if (f == null) { return null; } return f.getAnnotation(annotationClass); } /** * 获取类的所有属性的所有注解 * * @param c * @return */ public static Map<String, Annotation[]> getFieldsAnnotations(Class<?> c) { Field[] fields = c.getDeclaredFields(); Map<String, Annotation[]> m = new HashMap<>(fields.length); for (Field fi : fields) { m.put(fi.getName(), fi.getAnnotations()); } return m; } /** * 获取方法上的所有的注解 * * @param c * @param methodName * @return */ public static Annotation[] getMethodAnnotations(Class<?> c, String methodName) { Method[] methods = c.getMethods(); Method m = null; for (Method method : methods) { if (methodName.equals(method.getName())) { m = method; break; } } if (m == null) { return null; } return m.getAnnotations(); } /** * 获取方法上指定的注解 * * @param c * @param methodName * @param annotationClass * @return */ public static <T extends Annotation> T getMethodAnnotation(Class<?> c, String methodName, Class<T> annotationClass) { Method[] methods = c.getMethods(); Method m = null; for (Method method : methods) { if (methodName.equals(method.getName())) { m = method; break; } } if (m == null) { return null; } return m.getAnnotation(annotationClass); } }

yinyun1985 LV14
2022年4月11日
葡萄吃西瓜 LV4
2021年9月10日
liangkai123 LV1
2021年8月24日
admin_hhh LV3
2020年12月28日
呆萌的爱斯逼 LV8
2020年4月8日
xuyongff LV24
2020年2月23日
EasonLi LV8
2019年12月27日
linfeng127 LV1
2019年12月11日
TenzeTseng LV6
2019年9月15日
ythlibo LV12
2019年9月12日

youwuzuichen LV11
2024年11月13日
denliv_hui LV13
2024年4月26日
微信网友_6761617215229952
2023年12月2日
暂无贡献等级
zhumeng168 LV5
2023年7月8日
微信网友_6319582217965568 LV3
2023年2月24日
and123456 LV11
2022年10月26日
neilcheung LV4
2022年8月22日
364502984 LV18
2022年6月2日
caodehao1 LV3
2022年4月20日
yinyun1985 LV14
2022年4月11日