现场#getAnnotations()和现场#getDeclaredAnnotations之间的差异() [英] Difference between Field#getAnnotations() and Field#getDeclaredAnnotations()
问题描述
的JavaDoc说以下内容:
JavaDoc says the following:
<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/AccessibleObject.html#getAnnotations%28%29\">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.
<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Field.html#getDeclaredAnnotations%28%29\">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:
从<一个摘录href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/AccessibleObject.java\"相对=nofollow> 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");
}
和自<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/AccessibleObject.java#AccessibleObject.getAnnotations%28%29\"相对=nofollow>字段不覆盖 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)
另一种情况是<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/lang/Class.java\"相对=nofollow> 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之间的差异()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!