ClassUtils { public static <T extends Annotation> T getAnnotationOnField(Class<?> clazz, String fieldName, Class<T> annotationClass) { Field field = getField(clazz, fieldName); if (field != null) { return getAnnotationOnField(field, annotationClass); } return null; } private  ClassUtils(); static boolean canSetProperty(Object obj, String fieldName); static void clearFieldValue(Object obj, String fieldName, Class<?> argType); static Class<?> forClass(final String clazz); static T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static R getAnnotationAttributeValue(Field field, Class<T> annotationClass, String attributeName); static T getAnnotationOnField(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static T getAnnotationOnField(Field field, Class<T> annotationClass); static T getAnnotationOnMethod(Class<?> clazz, String fieldName, Class<T> annotationClass); @SuppressWarnings("unchecked") static T getAnnotationOnMethod(Method method, Class<T> annotationClass); static byte[] getBytes(Object obj, String fieldName); static Constructor<T> getConstructor(Class<T> clazz, Object... args); static Field getField(Class<?> clazz, String fieldName); static Object getFieldValue(Object obj, String fieldName); static String getFieldValueAsString(Object obj, String fieldName); static String getFieldValueAsString(Object obj, String fieldName, String defValue); static Method getGetterMethod(Class<?> clazz, String fieldName); static int getMaxLength(Class<?> clazz, String fieldName); static PropertyDescriptor getPropertyDescriptorForNestedProperty(Class<?> clazz, String property); static Class<?> getResolvedType(Class<T> type, String fieldName, int... indexes); static Class<?> getResolvedType(Object object, int... indexes); static boolean hasMethod(Object obj, String methodName); static T instantiateClass(Class<T> clazz, Object... args); static void setBytes(byte[] bytes, Object obj, String fieldName); static void setFieldValue(Object obj, String fieldName, Object value); static void copyFields(T from, S to);  }
@Test public void testGetAnnotationOnField() { Field fieldOne = ClassUtils.getField(TestObject.class, "fieldOne"); Attribute attribute = ClassUtils.getAnnotationOnField(fieldOne, Attribute.class); assertNotNull(attribute); assertEquals("desc", attribute.description()); attribute = ClassUtils.getAnnotationOnField(TestObject.class, "fieldOne", Attribute.class); assertEquals("desc", attribute.description()); String desc = ClassUtils.getAnnotationAttributeValue(fieldOne, Attribute.class, "description"); assertEquals("desc", desc); Entity entity = ClassUtils.getAnnotationOnField(fieldOne, Entity.class); assertNull(entity); }