Java Annotation and javaannotation Annotation

Source: Internet
Author: User

Java Annotation and javaannotation Annotation

Reference: Java official documentation http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.6

I. annotation type:

Is a special type of interface, the difference is that the annotation declaration is @ interface

Ii. Example: (How do Android frameworks use annotations to save code)

1. annotation Declaration

@Retention(RetentionPolicy.RUNTIME)public @interface TestAnno {public int viewId();}

2. Use of annotations

public class MainActivity extends Activity {        @TestAnno(viewId = R.id.view)private View view;        @Overrideprotected void onCreate(Bundle savedInstanceState) {AnnoUtil.assignView(this);         }}

 

 

3. Use annotation information in the Code (which is often done in the Framework)

Public class AnnoUtil {public static void assignView (Activity context) {Field [] fields = context. getClass (). getDeclaredFields (); for (Field field: fields) {TestAnno anno = field. getAnnotation (TestAnno. class); if (anno! = Null) {// to access the private constant, cancel the read check field. setAccessible (true); try {field. set (context, context. findViewById (anno. viewId ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();}}}}}

(This is generally the case with the framework, which saves some code)

Iii. features:

1. java annotations cannot explicitly declare the parent class (in fact, they all inherit the Annotation class, but you cannot use the extends keyword during the Declaration) and cannot be generalized.

2. Method:

A) Each method defines an element in the annotation (Each method declaration in an annotation type declaration definesElementOf the annotation type ).

(Wipe! Without reading the document, I always thought there was no method for annotation, because he assigned a value using the method name, for example @

B) The method parameters in the annotation cannot have parameters.

C) the method in the annotation cannot throw an exception

D) the return value of the method (I .e. the element of the annotation) must be one of the following:A primitive type (basic type: int, float, etc ),String,Class, Parameterized invocationClass, Enum, Annotation, array

(Parameterized invocationClass: After java 5, java. lang. Class is a generic Class, which means that it can have type parameters in the form of Class <...>. For example: Class <? Extends Formatter> value ();)

E) if the default keyword is not added to the method declaration, the value must be assigned when the generic type is used.

D) If you want to build a framework and call the annotation through reflection during RUNTIME, add the annotation @ Retention (RetentionPolicy. RUNTIME) to your annotation; otherwise, the annotation may be eliminated during compilation.


3. Without special declarations, annotations are the same as interfaces (both static fields)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.