Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Field#getAnnotations() and Field#getDeclaredAnnotations()

Tags:

JavaDoc says the following:

AccessibleObject#getDeclaredAnnotations:

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:

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.

Since getAnnotations is inherited from class java.lang.reflect.AccessibleObject, have Field objects access to it.

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.

like image 517
Philipp Sander Avatar asked Aug 29 '13 13:08

Philipp Sander


People also ask

What is the difference between field and field?

The field. keyword is the not analyzed value of your text field value while field contains all the token after the value have been analyzed.

What is difference between record and field?

A record: Contains specific data, like information about a particular employee or a product. A field: Contains data about one aspect of the table subject, such as first name or e-mail address. A field value: Each record has a field value.

What is difference between field and column?

A column is a collection of cells alligned vertically in a table. A field is an element in which one piece of information is stored, such as the received field. Usually a column in a table contains the values of a single field. However, you can show several fields in a column by using a Formula or a Combination field.

What is difference between field and property?

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.


2 Answers

Looking in the source-code gives the answer:

excerpt from 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"); } 

And since Field does not override getAnnotations(): getDeclaredAnnotations() is called.

So both methods do the same when called on a java.lang.reflect.Field object. (so the JavaDoc is wrong in my opinion)

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); } 
like image 176
Philipp Sander Avatar answered Nov 12 '22 04:11

Philipp Sander


getDeclaredAnnotations() provides direct implemented annotations only where as getAnnotations() provide direct implemented as well as inheritable annotations(@Inherited) from its parent class.

like image 23
Shantonu Avatar answered Nov 12 '22 04:11

Shantonu