【Java SE】Annotation
2017-04-13 本文已影响20人
KingJA
1# Annotation
Provides library support for the Java programming language annotation facility.
2# Key Class
2.1# java.lang
- @Override
Indicates that a method declaration is intended to override a method declaration in a supertype. - @Deprecated
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
values:deprecation,unchecked,fallthrough,path,serial,finally,all - @SuppressWarnings
Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element). - @SafeVarargs (since 1.7)
A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter. - @FunctionalInterface (since 1.8)
An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.
2.2# java.lang.Annotation | Annotation Types
- @Target
Indicates the contexts in which an annotation type is applicable. - @Relention
Indicates how long annotations with the annotated type are to be retained. - @Documented
Indicates that annotations with a type are to be documented by javadoc and similar tools by default. - @Inherited
Indicates that an annotation type is automatically inherited. - @Native (since 1.8)
Indicates that a field defining a constant value may be referenced from native code. - @Repeatable (since 1.8)
The annotation type java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it (meta-)annotates is repeatable.
2.2# java.lang.Annotation | Enum
- ElementType
- TYPE:Class, interface (including annotation type), or enum declaration
- FIELD:Field declaration (includes enum constants)
- METHOD:Method declaration
- PARAMETER:Formal parameter declaration
- CONSTRUCTOR:Constructor declaration
- LOCAL_VARIABLE:Local variable declaration
- ANNOTATION_TYPE:Annotation type declaration
- PACKAGE:Package declaration
- TYPE_PARAMETER:Type parameter declaration
- TYPE_USE:Use of a type
- RetentionPolicy
表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)- SOURCE:Annotations are to be discarded by the compiler.在源文件中有效(即源文件保留)
- CLASS:Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.在class文件中有效(即class保留)
- RUNTIME:Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.在运行时有效(即运行时保留)
3# Common Annotation Class
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
4# Annotation Elements Returns
- int,float,boolean,byte,double,char,long,short
- String
- Class
- enum
- Annotation
- Array type all of above
PS. You can use 'default ' to statement the default value the element returns
5# Custom Annotation
5.1# Annotation Class
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Robot {
public enum RobotSize {BIG, MED, MIN;}
int robotcount() default 1;
String robotName() default "ET";
RobotSize robotSize() default RobotSize.MED;
}
5.2# applied to a method
@Robot(robotcount = 5, robotName = "XMAN", robotSize = Robot.RobotSize.BIG)
public void buildRobot() {}
5.3# get the annotation info by reflection
public class RobotBuilder {
public static void main(String[] args) {
getAnnotationInfo(RobotBuilder.class);
}
private static void getAnnotationInfo(Class<RobotBuilder> robotBuilderClass) {
Method[] methods = robotBuilderClass.getDeclaredMethods();
for (Method method : methods) {
Robot annotation = method.getAnnotation(Robot.class);
if (annotation != null) {
int robotcount = annotation.robotcount();
String robotName = annotation.robotName();
Robot.RobotSize robotSize = annotation.robotSize();
System.out.println(">>>robotcount:"+robotcount);
System.out.println(">>>robotName:"+robotName);
System.out.println(">>>robotSize:"+robotSize);
}
}
}
@Robot(robotcount = 5, robotName = "XMAN", robotSize = Robot.RobotSize.BIG)
public void buildRobot() {}
}
output
>>>robotcount:5
>>>robotName:XMAN
>>>robotSize:BIG