Java Custom Annotations

Source: Internet
Author: User

Some rules written by custom annotation classes:

1. The annotation type is defined as @interface, and all annotation automatically inherit the Java.lang.Annotation interface and can no longer inherit other classes or interfaces.

2. Parameter members can only be decorated with public or default

3. Parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and string, Enum, Class, annotations and other data types. As well as these types of arrays.

4. To get the annotation information for a class method and a field, you must get the annotation object through the Java reflection technique, because you have no other way to get the annotation object

5. Annotations can also have no members defined, but this makes the annotations useless.

When customizing the annotation class, you can specify the target (class, Method, field, constructor, and so on), the lifetime of the annotation (runtime, class file, or source code is valid), whether the annotations are included in the Javadoc and whether the subclasses are allowed to inherit annotations from the parent class, as follows:

[email protected] represents the annotation target, and the possible elemenettype parameters include:

Elemenettype.constructor Constructor Declaration

Elemenettype.field domain declarations (including enum instances)

Elemenettype.local_variable local variable declaration

Elemenettype.method Method declaration

Elemenettype.package Package Declaration

Elemenettype.parameter parameter declaration

Elemenettype.type class, interface (including annotation type) or enum declaration

[Email protected] indicates the lifetime of the annotation, and the optional Retentionpolicy parameters include

Retentionpolicy.source annotations will be discarded by the compiler

Retentionpolicy.class annotations are available in the CLASS file, but are discarded by the VM

Retentionpolicy.runtime VMS will also retain annotations at run time, so the information of annotations can be read through the reflection mechanism

[Email protected] indicates that the note is included in the Javadoc

4. @Inherited indicates that subclasses are allowed to inherit annotations from the parent class

Source

1 Myannotationclass.java

[Java]View PlainCopy 
  1. Package com.java.annotation;
  2. Import Java.lang.annotation.ElementType;
  3. Import java.lang.annotation.Retention;
  4. Import Java.lang.annotation.RetentionPolicy;
  5. Import Java.lang.annotation.Target;
  6. /**
  7. * Class annotations
  8. * */
  9. @Retention (Retentionpolicy.runtime)
  10. @Target (Elementtype.type)
  11. Public @interface Myannotationclass {
  12. Public String msg ();
  13. }

2 Myannotationmethod.java

[Java]View PlainCopy  
  1. Package com.java.annotation;
  2. Import Java.lang.annotation.ElementType;
  3. Import java.lang.annotation.Retention;
  4. Import Java.lang.annotation.RetentionPolicy;
  5. Import Java.lang.annotation.Target;
  6. /**
  7. * Method Annotations
  8. **/
  9. @Retention (Retentionpolicy.runtime)
  10. @Target (Elementtype.method)
  11. Public @interface Myannotationmethod {
  12. Public String Common ();
  13. }

3 Myannotationfield.java

[Java]View PlainCopy  
  1. Package com.java.annotation;
  2. Import Java.lang.annotation.ElementType;
  3. Import java.lang.annotation.Retention;
  4. Import Java.lang.annotation.RetentionPolicy;
  5. Import Java.lang.annotation.Target;
  6. @Retention (Retentionpolicy.runtime)
  7. @Target (Elementtype.field)
  8. Public @interface Myannotationfield {
  9. Boolean request ();
  10. }

4 Myannotationdemo.java

[Java]View PlainCopy 
  1. Package com.java.annotation;
  2. @MyAnnotationClass (msg = "This is a class annotation")
  3. Public class Myannotationdemo {
  4. Public Myannotationdemo () {
  5. }
  6. Public Myannotationdemo (String text) {
  7. this.text = text;
  8. }
  9. @MyAnnotationMethod (common = "This is a method annotation")
  10. public Void Method () {
  11. }
  12. @MyAnnotationField (Request = true)
  13. private String text;
  14. }

5 Myannotationtest.java

[Java]View PlainCopy 
  1. Package com.java.annotation;
  2. Import Java.lang.reflect.Field;
  3. Import Java.lang.reflect.Method;
  4. Public class Myannotationtest {
  5. public static void Main (string[] args) {
  6. Myannotationdemo demo = new Myannotationdemo ("Hello Rollen");
  7. Myannotationclass Annotationclass = Demo.getclass (). Getannotation (Myannotationclass.   Class);
  8. System.out.println (Annotationclass.msg ());
  9. method = null;
  10. try {
  11. method = Demo.getclass (). GetMethod ("method",new class[0]);
  12. } catch (SecurityException e) {
  13. E.printstacktrace ();
  14. } catch (Nosuchmethodexception e) {
  15. E.printstacktrace ();
  16. }
  17. Myannotationmethod Annotationmethod = method.getannotation (myannotationmethod.   Class);
  18. System.out.println (Annotationmethod.common ());
  19. Field field = null;
  20. try {
  21. field = Demo.getclass (). Getdeclaredfield ("text");
  22. } catch (SecurityException e) {
  23. E.printstacktrace ();
  24. } catch (Nosuchfieldexception e) {
  25. E.printstacktrace ();
  26. }
  27. Myannotationfield Annotationfield = field.getannotation (Myannotationfield.   Class);
  28. System.out.println (Annotationfield.request ());
  29. }
  30. }

Run results

csdn:http://download.csdn.net/detail/haishu_zheng/9569169

Github:https://github.com/zhenghaishu/annotationdemo

Java Custom 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.