Java custom annotation and use reflection to read annotation, java Annotation

Source: Internet
Author: User

Java custom annotation and use reflection to read annotation, java Annotation

I. Custom annotations

Meta annotation:

@ Interface annotation: Define annotation interface

@ Target annotation: used to restrict the scope of use of the description annotation. If the description annotation exceeds the scope of use, the compilation fails. For example, ElementType. METHOD, ElementType. TYPE;

@ Retention annotation: used to restrict the scope of the defined annotation. There are three scopes:

1. RetentionPolicy. SOURCE: The SOURCE Code applies to Java files. This annotation is removed when javac is executed.

2. RetentionPolicy. CLASS: the binary code is included in the class file. This annotation is removed when Java is executed.

3. RetentionPolicy. RUNTIME: When the function scope is RUNTIME, the comment can be dynamically obtained.

@ Brief ented: specifies that the comment is displayed when javadoc generates an API document.

@ Inherited: used to specify that the description annotation can be Inherited by the subclass of the class described by it. By default, it cannot be Inherited by its subclass.

Custom annotation interface:

 

1 package com. java. annotation; 2 3 import java. lang. annotation. incluented; 4 import java. lang. annotation. elementType; 5 import java. lang. annotation. inherited; 6 import java. lang. annotation. retention; 7 import java. lang. annotation. retentionPolicy; 8 import java. lang. annotation. target; 9 10 @ Target ({ElementType. METHOD, ElementType. TYPE}) 11 @ Inherited12 @ incluented13 @ Retention (RetentionPolicy. RUNTIME) 14 public @ interface Annotation_my {15 16 String name () default "Zhang San"; // defalt indicates the default value 17 18 String say () default "hello world "; 19 20 int age () default 21; 21 22}

 

Next we define an interface:

Package com. java. annotation; @ Annotation_my // use the annotation public interface Person {@ Annotation_my public void name (); @ Annotation_my public void say (); @ Annotation_my public void age ();}

After the interface is defined, we can write the interface implementation class (the interface cannot be instantiated)

Package com. java. annotation; @ Annotation_my @ SuppressWarnings ("unused") public class Student implements Person {private String name; @ Override @ Annotation_my (name = "rogue hacker ") // The default value assigned to name is michael. // if no default value is specified during annotation definition, the initial value public void name () must be assigned here () {}@ Override @ Annotation_my (say = "hello world! ") Public void say () {}@ Override @ Annotation_my (age = 20) public void age (){}}

Then we write a test class to test our annotation.

Package com. java. annotation; import java. lang. annotation. annotation; import java. lang. reflect. field; import java. lang. reflect. method; public class Text {Annotation [] annotation = null; public static void main (String [] args) throws ClassNotFoundException {new Text (). getAnnotation ();} public void getAnnotation () throws ClassNotFoundException {Class <?> Stu = Class. forName ("com. java. annotation. student "); // static loading class boolean isEmpty = stu. isAnnotationPresent (com. java. annotation. annotation_my.class); // judge whether stu uses the annotation interface we just defined if (isEmpty) {annotation = stu. getAnnotations (); // obtain the for (Annotation a: annotation) {Annotation_my = (Annotation_my) a; // forcibly converts it to the Annotation_my type System. out. println (stu + ": \ n" + my. name () + "say:" + my. say () + "my age:" + my. age () ;}} Met Hod [] method = stu. getMethods (); // System. out. println ("Method"); for (Method m: method) {boolean ismEmpty = m. isAnnotationPresent (com. java. annotation. annotation_my.class); if (ismEmpty) {Annotation [] aa = m. getAnnotations (); for (Annotation a: aa) {Annotation_my an = (Annotation_my) a; System. out. println (m + ": \ n" +. name () + "say:" +. say () + "my age:" +. age () ;}}// get Fields by force System. out. pr Intln ("get Fileds by force! "); Field [] field = stu. getDeclaredFields (); for (Field f: field) {f. setAccessible (true); System. out. println (f. getName ();} System. out. println ("get methods in interfaces! "); Class <?> Interfaces [] = stu. getInterfaces (); for (Class <?> C: interfaces) {Method [] imethod = c. getMethods (); for (Method m: imethod) {System. out. println (m. getName ());}}}}

 

Related Article

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.