Examples of using Java annotations Annotaton

Source: Internet
Author: User

1. Definition of annotations

Import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.target;/** * created on: 2015-7-1 2:36:37 * Customizing an annotation method * Meta-Annotations: Annotated annotations are commonly used to have *       @Target, the location of the annotation function, @Retention, the time of the annotation action; *       @Document, annotations are included in Javadoc; @Inherited, allowing subclasses to inherit annotations from the parent class; *       @Override, the current method overrides the hyper-tired method, @Deprecated, does not advocate the code; *       @SuppressWarnings, turns off improper compiler warning messages. */@Target (Elementtype.method) @Retention (retentionpolicy.runtime)//1, annotated elements look like the method of an interface, the only difference is that you can set a default value for it. 2. The types of annotations available include the following: All basic types, String, Class, enum, Annotation, and array forms of the above types. 3, the element can not have an indeterminate value, that is, there is either a default value, or the use of annotations when the value of the element is provided. And the element cannot use NULL as the default value. 4, note in the case that there is only one element and the name of the element is value, you can omit "value=" when using annotations and write the required values directly. Public @interface usercase{public int id ();p ublic String password () default "jelly"; String[] value1 () default "ABC"; MyEnum value2 () default Myenum.sunny;} Enum myenum{    Sunny,rainy}
2. Use of annotations

/** * created on: 2015-7-1 2:50:08 * Class Description: Use annotations */public class Passwordutils {@UserCase (id = $, Password = "Kanji Jell") Public    Boolean ValidatePassword (String password) {        return (password.matches ("\\w*\\d\\w*"));    }    @UserCase (id = $) public    String Encryptpassword (string password) {        return new StringBuilder (password). Reverse (). toString ();    }}
3, the test of annotations

Import Java.lang.reflect.method;import java.util.arraylist;import java.util.collections;import java.util.List; Import org.junit.test;/** * created on: 2015-7-1 3:02:12 * Class Description: Test note usage */public class Definetest {/** * @date 2015-7-1 PM 1:45:2        4 */@Testpublic void TestDefine1 () {list<integer> usecases = new arraylist<integer> ();        Collections.addall (Usecases, 47, 48, 49, 50);    Trackusecases (usecases, Passwordutils.class);    }//The annotation processor obtains the annotation information on the method through the reflection mechanism (CL is the resulting class object) and then makes specific processing based on the value of the annotation element. public static void Trackusecases (List<integer> usecases, class<?> cl) {for (Method m:cl.getdeclaredm            Ethods ()) {Usercase UC = m.getannotation (Usercase.class); if (UC! = null) {System.out.println ("Found User case:" + uc.id () + "" + Uc.pas                Sword ());            Usecases.remove (New Integer (Uc.id ())); }} for (int i:usecases) {System.out.println ("MissinG user case-"+ i); }    }}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Examples of using Java annotations Annotaton

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.