Here you can find the source of findFieldEx(Class> type, Class
private static <T extends Annotation> Field findFieldEx(Class<?> type, Class<T> annotationClass)
//package com.java2s; //License from project: Open Source License import java.lang.annotation.Annotation; import java.lang.reflect.Field; public class Main { private static <T extends Annotation> Field findFieldEx(Class<?> type, Class<T> annotationClass) { for (Field field : type.getDeclaredFields()) { if (field.getAnnotation(annotationClass) != null) { return field; }/*from w ww. java 2 s .c o m*/ } throw new IllegalArgumentException("Specific ".concat(type.getName()).concat(" must have annotation ") .concat(annotationClass.getName())); } }