Annotation (65), annotation 65

Source: Internet
Author: User

Annotation (65), annotation 65
1: All annotations are Class 2: All Annotation classes are sub-classes of the Annotation interface by default. 3: definition method:

Public @ interface SomeAnotation {

}

 

4: location where annotations can be made

Class

Method

Member variable.

Return Value

Parameter

Local variable

import org.junit.Test;@MyTestpublic class RunTest {    @MyTest    private int age;    @MyTest()    public void tt(@MyTest()int a){        @MyTest        int xx=0;    }}

5: if an annotation is defined without indicating the location where the annotation can be annotated, the annotation can be annotated at all locations

 

The following defines that an annotation can only be annotated to the method: import java. lang. annotation. elementType; import java. lang. annotation. target; // set the location where the annotation can be annotated @ Target (value = {ElementType. METHOD}) public @ interface MyTest {}

 

6: Purpose

6.1: limits the compilation process.

    public class MyServlet extends HttpServlet {    @Override    public void doGet(ServletRequest req,String name)            throws ServletException, IOException {    } }

 6.2: Use group reflection during runtime

 

Bytecode of all classesClass, Method, Field, ConstractorAll have one method:

 

boolean

isAnnotationPresent(Class<? extends Annotation> annotationClass)
If the annotation of the specified type exists on this element, true is returned; otherwise, false is returned.

 

Annotation scope:

 

One class:Retention, used to define the policy for the annotation:

Java. lang. annotation

Enumerate RetentionPolicy

 

Three constants:

 

Enumeration constant Summary

CLASS
The compiler will record the annotation in the class file, but the VM does not need to keep the annotation at runtime. Exists in. class and erased at runtime

 

RUNTIME
The compiler will record the annotation in the class file, and the VM will keep the annotation at runtime, so it can be read in a reflective manner. It exists during running.

 

SOURCE
Comments to be discarded by the compiler. Only @ Overied exists in the. java File

 

The following are commonly used standard definitions:

import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(value={ElementType.METHOD})public @interface MyTest {}
The following is the test class of myUnit: Core Method: public class MyUnit {public static void main (String [] args) throws Exception {System. err. println ("Enter the class to be tested:"); required SC = new slave (System. in); String clsName = SC. nextLine (); // clsName = "cn. itcast. demo. runTest "; // obtain the Byte Class cls = Class of the Class based on the Class name. forName (clsName); // instantiate this class and call the default constructor Object obj = cls. newInstance (); // get all methods in this class Method [] MS = cls. getDeclaredMethods (); // only obtain user-defined methods, private & public // cls. getMethods (); get the Method of this class, including the Method inherited from the parent class // traverse to determine whether there is an annotation for (Method m: ms) {boolean boo2 = m. isAnnotationPresent (MyTest. class); // false if (boo2) {if (m. getModifiers () = Modifier. PRIVATE) {System. err. println ("this private method:" + m. getName () + ", does not support running... "); continue;} // run this method m. invoke (obj );}}

 

 

7. annotation instantiation

Never instantiate annotation classes because annotations are instantiated by the system through reflection.

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.