Java Learning Note 10 (annotations)

Source: Internet
Author: User

1. Introduction

Annotations are a new feature that JavaSE5.0 provides to provide some descriptive narrative information to a program through a specific annotation tag.

These descriptive narrative information can provide additional information to the compiler and execution environment when compiling or executing. thus simplifying development.


2. Define your own annotations

1. Statement of your own annotations

@interface < note name >{< annotation attribute type > < annotation property name >[default< default;]}

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > @interface mytest{//itself defines the annotation attribute string msg ();} </span>

2. Determine the use target for annotations

For different purposes, annotations can have different use targets.

For example, the method annotation, the constructor annotation, the Variable field annotation, the class or the interface annotation and so on

@Target (elementtype.< use target point >)

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//declares that this annotation can only annotate a class or interface @target (elementtype.type) @interface mytest{//define its own annotation property, String msg (); </span>


3. Determining the use of annotations

Annotations can be used in different ways depending on the purpose of use.

Class: Annotations exist in the Thunder file. But the virtual machine is not able to get annotations confidence when executing

Source: Annotations exist only in the source code. be removed at compile time

Runtime: Annotations exist in the class file, and the virtual machine can obtain annotation information when executing

@Retention (retentionpolicy.< aging value >)

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//declares that this annotation can only annotate a class or interface @target (Elementtype.type)/declares that this annotation can be obtained @retention (retentionpolicy.runtime) at execution time @interface mytest{//itself defines the annotation attribute string msg ();} </span>


4. Get annotations Using Reflection

Some of the previous blogs have talked about reflections, which can also be used to get the annotated information through reflection, similar to the above.

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package com. Annotation;import Java.lang.annotation.*;import java.lang.reflect.*;//declares the use of annotations with the target type @retention ( Retentionpolicy.runtime) @Target (elementtype.type) @interface Myannotationforclass {java.lang.String msg ();} Declares an annotated @retention (retentionpolicy.runtime) @Target (Elementtype.method) using the target METHOD @interface Myannotationformethod {java.lang.String msgPart1 (); java.lang.String msgPart2 ();} The class @myannotationforclass (msg = "This is an annotation to a class") that declares the use of annotations is MyClass {@MyAnnotationForMethod (MsgPart1 = "The first part of the method annotation", MsgPart2 = "Second part of Method annotation") public void SayHello () {System.out.println ("Congratulations on your successful call to the SayHello Method!

! ");}} Main class public class Sample34_10 {public static void main (string[] args) throws Nosuchmethodexception {//Get class using annotations MyClass corresponding Class object class AC = myclass.class;//Gets the annotations of the MyClass class Myannotationforclass MAFC = (myannotationforclass) ac.getannotation ( Myannotationforclass.class)///Print the annotation information for the class SYSTEM.OUT.PRINTLN (the MyClass class's annotation information is: "" + mafc.msg () + "".

");//Gets the corresponding method object for the specified methods = Ac.getmethod (" SayHello ", new Class[0]);//Gets the corresponding annotation myannotationformethod MAFM = ( Myannotationformethod) method.getannotation (myannotationformethod.class);//printing method The corresponding annotation information System.out.println (" The first part of the SayHello method annotation information is: "" + mafm.msgpart1 () + "".

"); System.out.println (the second part of the SayHello method, the annotation information is: "" + mafm.msgpart2 () + "". ");}} </span>


Two types of annotation information are defined in the above examples. An application to a class or interface. An application to a method, followed by a reflection mechanism to obtain the corresponding annotation class, and then be able to manipulate the annotation information


Java Learning Note 10 (annotations)

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.