
com.xiongyingqi.util.AnnotationHelper Maven / Gradle / Ivy
/**
* WebSocketForJavaEE7
*/
package com.xiongyingqi.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
/**
* @author xiongyingqi xiongyingqi.com
* @version 2014年2月25日 下午12:05:17
*/
public class AnnotationHelper {
public static boolean isAnnotationType(Class> clazz,
Class annotationClass) {
return clazz.isAnnotationPresent(annotationClass);
}
public static T readAnnotationValue(Class> clazz,
Class annotationClass) {
if (isAnnotationType(clazz, annotationClass)) {
T t = clazz.getAnnotation(annotationClass);
return t;
} else {
return null;
}
}
/**
* 读取field的注解值(包括get、set方法)
* @param field
* @param annotationClass
* @param
* @return
*/
public static T readAnnotationValueOnField(Field field, Class annotationClass) {
if (field.isAnnotationPresent(annotationClass)) {
T t = field.getAnnotation(annotationClass);
return t;
} else {
Method method = EntityHelper.findGetMethod(field);
if (method.isAnnotationPresent(annotationClass)) {
T t = method.getAnnotation(annotationClass);
return t;
}
return null;
}
}
public static Collection super Annotation> readAnnotationsOnField(Field field) {
Collection super Annotation> annotations = new HashSet();
Annotation[] annotationArray = field.getAnnotations();
if (annotationArray != null && annotationArray.length > 0) {
Collections.addAll(annotations, annotationArray);
return annotations;
}
Method method = EntityHelper.findGetMethod(field);
Annotation[] methodAnnotations = method.getAnnotations();
if (methodAnnotations != null && methodAnnotations.length > 0) {
Collections.addAll(annotations, methodAnnotations);
return annotations;
}
return null;
}
/**
* 在类内搜索所有的字段的annotationClass注解值
*
* @param clazz
* @param annotationClass
* @param
* @return
*/
public static Collection readAnnotationsInClass(Class> clazz, Class annotationClass) {
Collection collection = null;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
T t = readAnnotationValueOnField(field, annotationClass);
if (t == null) {
continue;
}
if (collection == null) {
collection = new ArrayList();
}
collection.add(t);
}
return collection;
}
/**
* 在类内搜索使用annotationClass注解的字段(包括在get、set方法上注解)
* @param inClazz
* @param annotationClass
* @param
* @return
*/
public static Collection getFieldsWithAnnotationInClass(Class> inClazz, Class annotationClass) {
Collection fieldCollection = null;
Field[] fields = inClazz.getDeclaredFields();
for (Field field : fields) {
if(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())){
continue;
}
T t = readAnnotationValueOnField(field, annotationClass);
if (t == null) {
continue;
}
if (fieldCollection == null) {
fieldCollection = new ArrayList();
}
fieldCollection.add(field);
}
return fieldCollection;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy