AnnotationUtil作ってみた。

今後、Javaにおいてアノテーションの利用がいっそう活発になると思うので
とりあえず、作ってみた。



使い方は対象のクラスとメソッド名と取得対象のアノテーションクラスを渡す。

AnnotationSample anno =
getAnnotationMethod(sample.class,"test",null,AnnotationSample.class);

System.out.println(anno.xxxx());
		       

全ソースはこんな感じ。

/**
 * 
 */
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 
 * @author kensir0u
 * 
 * TODO Check the Retention defining is the RetentionPolicy.RUNTIME.
 *
 */
public final class AnnotationUtil {
	
	/**
	 * 
	 *
	 * <DL>
	 * <DT><b>メソッド概要:</b></DT><DD>
	 * <BR>
	 * </DD><BR>
	 * </DL>
	 *
	 * @param args
	 * @throws Exception
	 */
	public static void main(String args[]) throws Exception{
		
		AnnotationSample anno = getAnnotationClass(sample.class,AnnotationSample.class);
		System.out.println(anno.xxxx());
		
		anno = getAnnotationConstructor(sample.class,null,AnnotationSample.class);
		System.out.println(anno.xxxx());
		
		anno = getAnnotationMethod(sample.class,"test",null,AnnotationSample.class);
		System.out.println(anno.xxxx());
		
		anno = getAnnotationMethod(sample.class,"test2",null,AnnotationSample.class);
		System.out.println(anno.xxxx());
		
		anno = getAnnotationField(sample.class,"a",AnnotationSample.class);
		System.out.println(anno.xxxx());
		
		anno = getAnnotationField(sample.class,"b",AnnotationSample.class);
		System.out.println(anno.xxxx());
	
	}
	
	/**
	 * 
	  * <DL>
	  * <DT><b>メソッド概要:</b></DT><DD>
	  * Return the Annotation instance.<BR>
	  * </DD><BR>
	  * </DL>
	  *
	  * @param <T>
	  * @param target
	  * @param anno
	  * @return
	 */
	@SuppressWarnings("unchecked")
	public  static final <T> T getAnnotationClass(Class target,Class<T> anno) {

		return anno.cast(target.getAnnotation(anno));	
	}
	
	/**
	 * 
	  *
	  * <DL>
	  * <DT><b>メソッド概要:</b></DT><DD>
	  * Return the Annotation instance.<BR>
	  * </DD><BR>
	  * </DL>
	  *
	  * @param <T>
	  * @param target
	  * @param method
	  * @param param
	  * @param anno
	  * @return
	 */
	@SuppressWarnings("unchecked")
	public  static final <T> T getAnnotationConstructor(Class target,Class param[],Class<T> anno) {
		
		try {
			Constructor c = null;
			if (param == null){
				
				if (target.isMemberClass()) {
					Class superc = target.getDeclaringClass();
					c = target.getConstructor(new Class[]{ superc });
				} else {
					c = target.getDeclaredConstructor();
				}
				
			} else {
				c = target.getDeclaredConstructor(param);
			}
			if (c == null) return null;
			if (!c.isAccessible()) c.setAccessible(true);
			Annotation as[] = c.getAnnotations();
			for (Annotation a : as) {
				if (a.annotationType().isAssignableFrom(anno)) {
					return anno.cast(a);
				}
			}
			return null;
			
		} catch (SecurityException e) {
			throw e;
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 
	  *
	  * <DL>
	  * <DT><b>メソッド概要:</b></DT><DD>
	  * Return the Annotation instance.<BR>
	  * </DD><BR>
	  * </DL>
	  *
	  * @param <T>
	  * @param target
	  * @param method
	  * @param param
	  * @param anno
	  * @return
	 */
	@SuppressWarnings("unchecked")
	public  static final <T> T getAnnotationMethod(Class target,String method, Class param[],Class<T> anno) {
		
		try {
			Method m = target.getDeclaredMethod(method, param);
			if (m == null) return null;
			if (!m.isAccessible()) m.setAccessible(true);
			Annotation as[] = m.getAnnotations();
			for (Annotation a : as) {
				if (a.annotationType().isAssignableFrom(anno)) {
					return anno.cast(a);
				}
			}
			return null;
			
		} catch (SecurityException e) {
			throw e;
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}
	

	/**
	 * 
	 *
	 * <DL>
	 * <DT><b>メソッド概要:</b></DT><DD>
	 * Return the Annotation instance.<BR>
	 * </DD><BR>
	 * </DL>
	 *
	 * @param <T>
	 * @param target
	 * @param field
	 * @param anno
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public  static final <T> T getAnnotationField(Class target,String field,Class<T> anno) {
		
		try {
			Field f = target.getDeclaredField(field);
			if (f == null) return null;
			if (!f.isAccessible()) f.setAccessible(true);
			Annotation as[] = f.getAnnotations();
			for (Annotation a : as) {
				if (a.annotationType().isAssignableFrom(anno)) {
					return anno.cast(a);
				}
			}
			return null;
			
		} catch (SecurityException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	
	/**
	 * 
	 * @author kensir0u
	 *
	 */
	@AnnotationSample(xxxx="class")
	class sample{
		
		@AnnotationSample(xxxx="constructor")
		public sample(){
		}
		
		@AnnotationSample(xxxx="field1")
		private String a;
		
		@AnnotationSample(xxxx="field2")
		public String b;
		
		
		@AnnotationSample(xxxx="method1")
		public void test(){
		}
		
		@AnnotationSample(xxxx="method2")
		private void test2(){
		}
		
	}
	
	/**
	 * 
	 * @author kensir0u
	 *
	 */
	@Retention(RetentionPolicy.RUNTIME)
	@interface AnnotationSample {
		String xxxx();
	}
}