Annotation
1 , APT :
The tools for accessing and processing annotation are collectively known as the annotation process tool.
2 , Java.lang the five basic types of Annotation :
@Override, @Deprecated, @Suppress Warning, @Safe Varargs, @FunctionalInterface
3 , qualify overriding the parent class method: @Override
Example:
public class Fruit
{
public void info ()
{
System.out.println ("Info method for Fruit");
}
}
Class Apple extends Fruit
{
@Override
public void info ()
{
System.out.println ("Apple's Info method");
}
}
Description: The main purpose of @Override is to tell the compiler to check if the method under it has the correct way to rewrite the parent class to avoid unnecessary low-level errors.
4 , the token is obsolete: @Deprecated
Class Apple
{
@Deprecated
public void info ()
{
System.out.println ("Apple's Info method");
}
}
public class Deprecatedtest
{
public static Vodi main (string[] args)
{
New. Apple.info ();
}
}
Description: @Deprecated used to indicate that a program element (class, method, and so on) is obsolete, and the compiler will give a warning when other programs use the tagged element.
5 , suppress compiler warnings: @SuppressWarnings
Example:
To turn off compiler warnings for the entire class
@SuppressWarning (value= "unchecked")
public class Suppresswarningstest
{
public static void Main (string[] args)
{
List<string>mylist=new ArrayList ();
}
}
Note: The entire class in the above example does not see a warning that no generics are used, and when using @suppresswarning, you need to set the value for the member variable of the annotation in the form of a name=value in ().
6 , Java7 the "heap pollution" warning and @SafeVarargs
7 , Java8 the functional interface and @FunctionalInterface
Example:
@FunctionalInterface
Public Interface Funinterface
{
static void Foo ()
{
System.out.println ("Foo class method");
}
default void Bar ()
{
System.out.println ("bar default method");
}
void Test ();//define only an abstract method
}
Description
A, functional interface: There is only one abstract method in the interface (can contain more than one static method or default method).
b, the role of @FunctionalInterface is to ensure that the interface below is a functional interface, to avoid low-level errors.
8 , JDK the meta Annotation
Description: The Java.lang.annotation package provides 6 meta Annotation (meta Annotation), of which 5 meta Annotation are used to decorate other Annotation definitions.
9 , using @Retention
Format 1:
Keep To run time
@Retention (Value=retentionpolicy.runtime)
Public @Interface testable ()
Format 2:
Directly discarded by the compiler
@Retention (Retentionpolicy.source)//Only value is assigned, you can omit value=
Public @interface testable ()
Description
A, @Retention used to modify the annotation, determine its retention time.
B, @Retention type has a value variable, which must be specified.
The value of the C, value variable can only be as follows three:
A, RetentionPolicy.CLASS:Annotation by the compiler in the CLASS file, run, the JVM can not get annotation information, this is the default value.
B, RetentionPolicy.RUNTIME:Annotation is recorded in the class file by the compiler, and when run, the JVM can obtain annotation information, which is obtained through reflection.
C, RetentionPolicy.SOURCE:Annotation only in the source code, the compiler directly discard this annotation.
Ten , using @Target
Format:
Specifies that the following code fragment can only modify member variables
@Target (Elementtype.field)//@retention consistent with the value= can be omitted
Public @Interface actionlistenerfor ()
Description
A, @Target is used to modify which program units annotation can modify.
The value member variable of type B, @Target can only be as follows:
A, Elementtype.annotation_type: Specifies that the ANNOTATION can only modify ANNOTATION.
B, Elementtype.constructor: Specifies that the annotation can only decorate the constructor.
C, Elementtype.field: Specifies that the annotation of the policy can only modify member variables.
E, elementtype.local_variable: Specifies that the annotation of the policy can only modify local variables.
F, Elementtype.method: Specifies that the annotation of the policy can only modify the method definition.
G, Elementtype.package: Specifies that the annotation of the policy can only decorate the package definition.
H, Elementtype.parameter: Specifies that the annotation of the policy can only modify parameters.
J, Elementtype.type: Specifies that the policy's annotation can only be decorated with classes, interfaces (including annotation classes), and enumeration classes.
One , using @Documented
Example:
@Retention (Retentionpolicy.runtime)
@Target (Elementtype.method)
@Documented
Public @interface testable
{
}
public class MyTest
{
@Testable
public void info ()
{
System.out.println ("info method")
}
}
Use the Javadoc tool to generate the API documentation as follows:
Method detail Information
Info
@Testable
public void info ()
Description: The role of the @Document is to generate an API document that is more like a red labeled statement.
A , using @Inherited
Description: Decorated with @inherited will have inheritance.
- , Custom Annotation
Format:
Public @Interface Test
{
Member variables are declared in the form of a method of an invisible parameter
String name ();
}
"Java" Annotation_ Learning Notes