的JavaDoc说以下内容:
JavaDoc says the following:
AccessibleObject#getDeclaredAnnotations:
返回直接$这个元素页上$ psent所有注释。与此接口中的其他方法,这种方法忽略继承的注释。 (如果没有注释直接$这个元素页上$ psent返回长度为零的数组。)该方法的调用者可以随意修改返回的数组;它将对其他调用者返回的数组产生任何影响。
Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
Field#getAnnotations:
返回此元素上的所有注释present。 (如果此元素没有注释,则返回长度为零的数组。)该方法的调用者可以随意修改返回的数组;它将对数组产生任何影响其他调用者返回。
Returns all annotations present on this element. (Returns an array of length zero if this element has no annotations.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
由于 getAnnotations 从类继承 java.lang.reflect.AccessibleObject中,有字段对象访问它。
Since getAnnotations is inherited from class java.lang.reflect.AccessibleObject, have Field objects access to it.
据我了解的是, getDeclaredAnnotations他们之间的唯一区别忽略继承的注释。 我得到的带类的处理,但据我所知字段不能继承的注解时。
As i understand it is the only difference between them that getDeclaredAnnotations ignores inherited annotations. I get that when dealing with Classes but as far as i know Fields can NOT inherit annotations.
推荐答案展望源 - code给出了答案:
Looking in the source-code gives the answer:
从 java.lang.reflect.AccessibleObject中:
/** * @since 1.5 */ public Annotation[] getAnnotations() { return getDeclaredAnnotations(); } /** * @since 1.5 */ public Annotation[] getDeclaredAnnotations() { throw new AssertionError("All subclasses should override this method"); }和自字段不覆盖 getAnnotations() : getDeclaredAnnotations()被调用。
因此,一个 java.lang.reflect.Field中对象上调用时,这两种方法做同样的。(这样的JavaDoc是错在我看来)
So both methods do the same when called on a java.lang.reflect.Field object. (so the JavaDoc is wrong in my opinion)
另一种情况是 java.lang.Class中它覆盖了两种方法(和它做什么的的的JavaDoc 说;)):
the other case is java.lang.Class which overrides both methods (and does what it's JavaDoc says ;) ):
/** * @since 1.5 */ public Annotation[] getAnnotations() { initAnnotationsIfNecessary(); return AnnotationParser.toArray(annotations); } /** * @since 1.5 */ public Annotation[] getDeclaredAnnotations() { initAnnotationsIfNecessary(); return AnnotationParser.toArray(declaredAnnotations); }更多推荐
现场#getAnnotations()和现场#getDeclaredAnnotations之间的差异()
发布评论