[Java] annotation (inner note) instance 1

Source: Internet
Author: User

For the next article "[Android]
Automatic access to data entities-SQLite "Preparation
* Reference: http://www.blogjava.net/ericwang/archive/2005/12/13/23743.html
* Http://www.blogjava.net/weidagang2046/articles/27958.html
* Http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html
* Official documentation:
* Http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html

The above link is comprehensive, but it is explained more. Directly add the code and back up only

Package lab. sodino. annotation; import Java. lang. annotation. documented; import Java. lang. annotation. elementtype; import Java. lang. annotation. retention; import Java. lang. annotation. retentionpolicy; import Java. lang. annotation. target; @ target (elementtype. field) // valid for modifying attribute names only @ retention (retentionpolicy. runtime) // retentionpolicy exists. runtime, retentionpolicy. class, retentionpolicy. source @ documented // javadoc can generate the corresponding document public @ interface unique {}

Package lab. sodino. annotation; import Java. lang. annotation. documented; import Java. lang. annotation. elementtype; import Java. lang. annotation. retention; import Java. lang. annotation. retentionpolicy; import Java. lang. annotation. target; @ target (elementtype. type) // elementtype. type is used to identify classes, interfaces (including inner injection itself), and enumeration @ retention (retentionpolicy. runtime) @ documentedpublic @ interface author {// modifier can only be public, protected, Private & Static combination public static Enum appenum {web, client, service, undesignated }; // public & abstract combination or default appenum type () default appenum. undesignated; string name () Default "unknown"; string website () Default "N/";}

Package lab. sodino. annotation; import Java. lang. annotation. annotation; import Java. lang. reflect. field;/*** Java annotation <br/> ** reference address: http://www.blogjava.net/ericwang/archive/2005/12/13/23743.html <br/> * http://www.blogjava.net/weidagang2046/articles/27958.html <br/> * http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html <br/> * official documentation: * http://docs.oracle.com/javase/tutorial/java/java OO/annotations.html <br/> **/public class annotationlab {public static void main (string [] ARGs) {testfield (); testclazz ();} Private Static void testclazz () {try {class <?> Clz = Class. forname (goodsbean. class. getname (); system. out. println (clz. isannotationpresent (author. class); author Au = clz. getannotation (author. class); system. out. println (AU. name () + "" + Au. website ();} catch (classnotfoundexception e) {e. printstacktrace () ;}} Private Static void testfield () {goodsbean bean = goodsbean. newinstance (); Class <? Extends goodsbean> clazz = bean. getclass (); field [] arrfiled = clazz. getdeclaredfields (); For (field F: arrfiled) {annotation [] arranno = f. getannotations (); system. out. println (arranno. length); If (F. isannotationpresent (unique. class) {system. out. println (F. getname () + "annotation: Unique");} else {system. out. println (F. getname () + "annotation: NULL ");}}}}

Package lab. sodino. annotation; @ author (name = "sodino", website = "sodino.com") // assign values in the form of "Key = value. Public class goodsbean {@ uniquepublic long ID; Public string name; public long time; Public int price; Public Boolean ispaid;/*** for testing. */Public byte testbyte;/** for testing. */Public short testshort; public float cicle; Public double testdouble; Public String tostring () {stringbuffer strbuffer = new stringbuffer (); strbuffer. append ("ID [" + ID + "] \ n"); strbuffer. append ("name [" + name + "] \ n"); strbuffer. append ("time [" + time + "] \ n"); strbuffer. append ("price [" + Price + "] \ n"); strbuffer. append ("ispaid [" + ispaid + "] \ n"); strbuffer. append ("cicle [" + cicle + "] \ n"); strbuffer. append ("testbyte [" + testbyte + "] \ n"); strbuffer. append ("testshort [" + testshort + "] \ n"); strbuffer. append ("testdouble [" + testdouble + "] \ n"); Return strbuffer. tostring ();} public static goodsbean newinstance () {goodsbean bean = new goodsbean (); bean. id = 123456789l; bean. name = "autosave"; bean. time = 12345234252342l; bean. price = 1024; bean. ispaid = true; bean. cicle = 2.356f; bean. testbyte = 8; bean. testshort = 128; bean. testdouble = 9856.2145d; return bean ;}}

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.