Java learning notes-annotations, java learning notes annotations

Source: Internet
Author: User

Java learning notes-annotations, java learning notes annotations

Annotations are undoubtedly a very interesting feature. I did not pay too much attention when I was reading a book. I recently saw custom annotations when I was reading Spring practices, so I couldn't help taking out the Java programming idea and re-reading the annotation chapter.

The form of annotation is written in the Code, such

@Override

Such code

Compared with annotations, the most important and interesting thing is that annotations can be customized, and reflection can also be used to call annotation classes and methods.

The format for creating custom annotations in Java is as follows:

/** file:Test.java*/package javaapplication;import java.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Test {    public int id();    public String text() default "Null";}

So we can use annotations in the Code, such

/** File: JavaApplication. java */package javaapplication; import java. lang. reflect. method; class TestObject {@ Test (id = 233, text = "Hello") public void testA (int id, String text) {System. out. println ("id:" + id + "\ ttext:" + text) ;}@ Test (id = 10) public void testB (int id, String text) {System. out. println ("id:" + id + "\ ttext:" + text) ;}} public class JavaApplication {public static void main (String [] args) throws Exception {TestObject to = new TestObject (); // uses reflection to dynamically call the Method for (Method m: TestObject. class. getDeclaredMethods () {Test t = m. getAnnotation (Test. class); if (t! = Null) {m. invoke (to, t. id (), t. text ());}}}}

I briefly talked about the annotation writing method and then talked about the Metadata Annotation.

The meta annotation is used to annotate the annotation.

It is @ Target and so on. The following table is provided in specific Java programming ideology.

@ Target

Indicates where the annotation can be used. Possible ElementType parameters include:

  • CONSTRUCTOR: CONSTRUCTOR Declaration
  • FIELD: FIELD Declaration (including enum instances)
  • LOCAL_VARIABLE: local variable Declaration
  • METHOD: METHOD declaration
  • PACKAGE: PACKAGE Declaration
  • PARAMETER: PARAMETER Declaration
  • TYPE: Class, interface (including annotation TYPE), or enum Declaration
@ Retention

The level at which the annotation information needs to be saved. Optional RetentionPolicy parameters include:

  • SOURCE: annotation will be discarded by the compiler
  • CLASS: Annotations are available in the class file, but are discarded by the VM.
  • RUNTIME: the VM retains the annotation at RUNTIME. Therefore, the annotation information can be read through the reflection mechanism.
@ Brief ented Include this annotation in Javadoc
@ Inherited Allow subclass to inherit the annotation in the parent class

 

In addition, annotation elements support the following types

  • All basic types (int, float, boolean, etc)
  • String
  • Class
  • Enum
  • Annotation
  • Arrays of the above types

The simple notes are now over.

Annotations can also be processed with apt, used in the Observer mode, for unit testing, etc. For more details, please read chapter 20th of Java programming ideas-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.