现场#getAnnotations()和现场#getDeclaredAnnotations之间的差异()

编程入门 行业动态 更新时间:2024-10-07 23:26:27
本文介绍了现场#getAnnotations()和现场#getDeclaredAnnotations之间的差异()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

的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之间的差异()

本文发布于:2023-11-12 02:11:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:现场   差异   getAnnotations   getDeclaredAnnotations

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!