Getting started with custom annotations for Java

Source: Internet
Author: User

Defining annotations is similar to defining an interface, except that there is an @ symbol more than an interface, and you can define the method in the annotations, a simple example is as follows:

Import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target, @Description @target (Elementtype.type) @ Retention (retentionpolicy.runtime) public @interface Description {String value () default "no Description";}

It has the following characteristics:

1, the annotation method can not have parameters.

2. The annotation method returns value types that are limited to primitive types, strings, enumerations, annotations, or arrays that consist of these types.

3, the annotation type may contain the default value, as in the above code, the default "no description";

4, annotations can contain the following four kinds of meta-annotations:

@Documented – elements that use this annotation will be documented by Javadoc or similar tools, applied to type declarations, and annotations of type declarations affect the client's use of annotation elements. If a type declaration adds the annotation, its annotations become part of the public API for the annotated element.


@Target – The type of annotation program element that is supported, which can be type,method,constructor,filed, and so on, if the annotation is not written, it is not restricted and can be used on any program type.


          @Inherited  – 


@retention – indicates the duration of the annotation type retention,


Getting Started Example: (Reference network)

Define annotations:

Import java.lang.annotation.*; @Target (Elementtype.method) @Retention (retentionpolicy.runtime) @Documentedpublic @    Interface Name {String originate (); String community ();}


Annotations use:

@Description (value = "Javaeyer,is better") public class Javaeyer {@Name (originate = "Founder: abc", community = "Javaeye")    Public String Geteyename () {return null;    } @Name (originate = "Founder: NBA", community = "Americ") public String Getsidename () {return "Excuse me"; }}


Test class:

import com.sun.corba.se.impl.orbutil.concurrent.sync;import java.lang.reflect.method;import  java.util.hashset;import java.util.set;public class annotationtest {      @SuppressWarnings ("uncheked")     public static void main (string[]  args)  {        final string class_name =   "Javaeyer";        try {             class test = class.forname (CLASS_NAME);             method[] methods = test.getmethods ( );            boolean flag =  Test.isannotationpresent (Description.class);            &NBSP;IF (flag) {                description des =  ( Description) test.getannotation (description.class);                 system.out.println ("Description:" +des.value ());                 system.out.println ("----------------------------") ;            }             Set<Method> set = new HashSet<Method> ();             for (int i = 0;i< methods.length;i++) {                 boolean otherflag = methods[i].isannotationpresent (Name.class);        &nbSp;        if (Otherflag) {                     set.add (Methods[i]);                 }             }             for (Method method:set) {                 name name = method.getannotation (Name.class);                 system.out.println ( Name.originate ());                &NBSP;SYSTEM.OUT.PRINTLN ("Community Created:" + name.community ());             }        } catch  (classnotfoundexception e)  {             e.printstacktrace ();         }    }}


This article is from the "Yao Sea" blog, please be sure to keep this source http://tl2011.blog.51cto.com/6006611/1789194

Getting started with custom annotations for Java

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.