All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zusmart.base.util.AnnotationUtils Maven / Gradle / Ivy

Go to download

提供基础的工具类及方法类,Logging,Scanner,Buffer,NetWork,Future,Thread

There is a newer version: 1.0.6
Show newest version
package com.zusmart.base.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public abstract class AnnotationUtils {

	/**
	 * 判断构造函数是否包含指定注解
	 * 
	 * @param constructor
	 *            构造函数
	 * @param annotation
	 *            指定的注解
	 * @return true为存在
	 */
	public static boolean hasAnnotation(Constructor constructor, Class annotation) {
		return null != constructor.getAnnotation(annotation);
	}

	public static boolean hasAnnotation(AccessibleObject accessibleObject, Class annotation) {
		return null != accessibleObject.getAnnotation(annotation);
	}

	public static boolean hasAnnotation(Method method, Class annotation) {
		return null != method.getAnnotation(annotation);
	}

	public static boolean hasAnnotation(Field field, Class annotation) {
		return null != field.getAnnotation(annotation);
	}

	public static boolean hasAnnotation(Class clazz, Class annotation) {
		return null != clazz.getAnnotation(annotation);
	}

	public static boolean hasAnnotation(Annotation[][] annotations, Class annotation) {
		if (null == annotations || annotations.length == 0) {
			return false;
		}
		for (int i = 0; i < annotations.length; i++) {
			Annotation[] ans = annotations[i];
			if (null == ans || ans.length == 0) {
				continue;
			}
			for (int j = 0; j < ans.length; j++) {
				Annotation an = ans[j];
				if (an.annotationType().equals(annotation)) {
					return true;
				}
			}
		}
		return false;
	}

	public static  T getAnnotation(AccessibleObject accessibleObject, Class annotation) {
		return accessibleObject.getAnnotation(annotation);
	}

	public static  T getAnnotation(Method method, Class annotation) {
		return method.getAnnotation(annotation);
	}

	public static  T getAnnotation(Field field, Class annotation) {
		return field.getAnnotation(annotation);
	}

	public static  T getAnnotation(Class clazz, Class annotation) {
		return clazz.getAnnotation(annotation);
	}

	public static Annotation[] getAnnotation(Class[] parameterTypes, Annotation[][] annotations, Class annotation) {
		if (null == parameterTypes || parameterTypes.length == 0) {
			return new Annotation[0];
		}
		Annotation[] list = new Annotation[parameterTypes.length];
		if (null == parameterTypes || parameterTypes.length == 0) {
			return list;
		}
		for (int i = 0; i < parameterTypes.length; i++) {
			Annotation[] ans = annotations[i];
			Annotation an = null;
			for (Annotation a : ans) {
				if (a.annotationType().equals(annotation)) {
					an = a;
					break;
				}
			}
			list[i] = an;
		}
		return list;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy