Here you can find the source of findField(Class> type, Class
public static <T extends Annotation> Field findField(Class<?> type, Class<T> annotationClass) throws Exception
//package com.java2s; //License from project: Open Source License import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class Main { private static Map<String, Field> neverExpiredFieldObject = new ConcurrentHashMap<>(); public static <T extends Annotation> Field findField(Class<?> type, Class<T> annotationClass) throws Exception { String classKey = "$Utility$findField".concat(type.getName()).concat(annotationClass.getName()); if (!neverExpiredFieldObject.containsKey(classKey)) { neverExpiredFieldObject.put(classKey, findFieldEx(type, annotationClass)); }//from w ww .j a v a2 s. co m return neverExpiredFieldObject.get(classKey); } private static <T extends Annotation> Field findFieldEx(Class<?> type, Class<T> annotationClass) { for (Field field : type.getDeclaredFields()) { if (field.getAnnotation(annotationClass) != null) { return field; } } throw new IllegalArgumentException("Specific ".concat(type.getName()).concat(" must have annotation ") .concat(annotationClass.getName())); } }