(This article is about the preparation of dependency injection container spring and analysis of JUnit source code)
Java Annotation (Callout)
Java.lang.annotation.Annotation is the parent interface for all Java annotations. It has only one method except that override/overwrites the Equals (object), Hashcode (), and ToString () of object
class<? Extends Annotation> Annotationtype ()
Returns the type of this callout.
The callout Java.lang defined in the JDK.OverrideFor:
@Target (Value=method) @Retention (value=source) public @interface override{}
Again like Java.lang.Functionalinterface。
@Documented @retention (value=runtime) @Target (value=type) public @interface functionalinterface ...
These examples reflect some of the basics of labeling: ① defines a callout. such as public@interfaceMyannotation. You do not need to extends annotation but use @ interface.
There are a number of callouts used to decorate their own defined labels-called meta-callout/meta-annotation, such as @target, @Retention, and @documented in the override sample. The parentheses are followed by the name value pairs that are cut with commas. (Name=value).
@Target Description of the applicable occasion/target of the decorated callout.
The value is limited by the enumeration Java.lang.annotation.ElementType, including Annotation_type, CONSTRUCTOR, FIELD, local_variable, METHOD, package, PARA METER, type, and Type_parameter.
For example, Target's own application is @target (Value=annotation_type), assuming that the callout has only a single attribute member (name value pair), and that the member named "value=" can be omitted as @target ( Annotation_type).
@Retention describes the retention policy of the decorated callout, which is enumerated by Retentionpolicy.
- Source is only kept in the program source code. or only valid for compilers, such as @override. Compiler Check
- When the default or class is specified, the label is written to the class file, but the information is not loaded into the JVM;
- RUNTIME indicates that the callout can be obtained through a reflection mechanism .
@Documented indicates that the callout is reflected in the Javadoc. ② Labels The elements (classes, methods) used to decorate the source code. attribute, number of parameters. Local variables, packages, meta callouts). The use of ③ annotations has a clearBottom Line:They do not affect the operation of program code。 Regardless of whether you add or remove annotations, the code runs without any impact.
However, the program can parse the RUNTIME annotations. and determine the behavior of the code .
Example:JUNIT4 self-defined annotation
Org.junit.Test
Org.junit.Ignore @Target ({elementtype.method, elementtype.type})
The method of @Before and @after can only be used to replace the setup and Teardown methods in the JUnit version number.
Org.junit.BeforeClass @Target (Elementtype.method)
Org.junit.Before @Target (Elementtype.method)
Org.junit.AfterClass @Target (Elementtype.method)
Org.junit.After @Target (Elementtype.method)
Org.junit.runner.RunWith
Org.junit.runners.Suite.SuiteClasses
Org.junit.runners.Parameterized.Parameters
Parse Runtime callout ① defines a callout.
Package Mytest;import java.lang.annotation.*; @Target (Elementtype.method) @Retention (retentionpolicy.runtime) Public @interface myannotation{ String value ();}
② Use classes for annotations
Package Mytest;public class HelloWorld { @MyAnnotation ("Test") public double Add (double m,double n) { return m+n; } @MyAnnotation ("Ignore") public double Add2 (double m,double n) { return m+n; }}
③ parsing retentionpolicy.runtime annotations.
Get the all public method through C, get its callout annotation[for each method], find its own defined myannotation, assuming its value () is "Test", (Double) Method.invoke (h,1,2.0) ;
Package Mytest;import Java.lang.annotation.*;import java.lang.reflect.*;p ublic class testhelloworld{public static void Main (string[] args) throws exception{//direct using reflection mechanism class<?> c = MyTest.HelloWorld.class; HelloWorld h = (HelloWorld) c.newinstance (); Method[] methods = C.getmethods (); for (Method method:methods) {annotation[] annotations = method.getdeclaredannotations (); for (Annotation annotation:annotations) {if (Annotation instanceof myannotation) {Myanno Tation myannotation = (myannotation) annotation; System.out.println ("Value:" + myannotation.value ()); if (Myannotation.value (). Equals ("Test")) {double d = (double) method.invoke (h,1,2.0); System.out.println (d); } } } } }}Output: Value:test
3.0
Value:ignore
1.2.4 Java Annotation Feed