1 Annotations: annotation annotations, is a code-level description. It is a feature introduced in JDK1.5 and later versions, with classes, interfaces, enumerations at the same level, to the computer, and to the JVM to provide interpretation information.
2 The role of annotations: Compile check: Code analysis, writing documents:
Scope of annotations: can be defined on a class, method, on the Code
the same location can only be used once, same annotation
Common Annotations:
@Deprecated can be used to modify classes to represent obsolete classes, or to modify methods to represent obsolete methods
@Override: Method used to test whether a method is overridden
JDK1.5, @Override can only detect methods of class-inheriting class overrides
* JDK1.6 and above, @Override is able to detect class inheritance class rewrite method, but also detect the method of implementing interface rewrite
@SuppressWarnings represents a suppress warning, the modified class or method is ignored by the compiler if there is a compilation warning
Deprecation, or slightly outdated rawtypes, ignoring type safety
Unused, ignoring no unchecked, ignoring security checks
NULL, ignoring null pointer serial, ignoring serial number all, ignoring all
Custom Annotations
Define annotations Using keywords: @interface
Annotation properties are equivalent to member variables and member methods in a class
Attribute format: Modifier return value type property name () [Default defaults]
Modifier: Default value public abstract, write not write is public abstract.
Return value types: base type, string strings, Class, annotations, enumerations, and one-dimensional arrays of the above types
Property Name: Custom
Default defaults: [Default property value] can be omitted, [] not written in program
Meta Annotations
Meta-Annotations: the annotations provided by the JDK used to decorate the custom annotations
@Retention used to determine the modified custom annotation life cycle if the Retention comment does not exist in the annotation type declaration , the retention policy defaults to
Retentionpolicy.class
Retentionpolicy.source modified annotations can only exist in the source code, byte code class does not. Purpose: Provided to the compiler for use.
Retentionpolicy.class-Modified annotations can only exist in source code and bytecode, not in memory at run time. Purpose: JVM Java Virtual machine use
Retentionpolicy.runtime modified annotations exist in source code, bytecode, memory (runtime). Purpose: Replace XML configuration
@Target used to determine where custom annotations are being decorated
If a Target meta comment does not exist in the annotation type declaration, the declared type can be used on either program element.
Elementtype.type modifying classes, interfaces
Elementtype.constructor Modified Construction
Elementtype.method Modification Method
Elementtype.field modifier fields
Parsing of custom annotations
Parsing of custom annotations: Get property values for custom annotations using custom annotations
and parsing related interfaces:
*java.lang.reflect.annotatedelement interface
* All known implementation classes:
* AccessibleObject, Class, Constructor, Field, Method, package
* Methods in the interface:
*boolean isannotationpresent (Class annotationclass)
* Returns True if a comment of the specified type exists on this element, otherwise false is returned.
* Judgment (Constructor, Field, Method ...) There are no annotations specified
* Parameters: Annotated class file object that can be passed
Myannotation03.class (String.class,int.class)
*t getannotation (class<t> annotationclass)
* If there is a comment of the specified type for the element, the comments are returned, otherwise null is returned.
* Get (Constructor, Field, Method ...) annotation, which annotation class file object is passed by the parameter,
* Parameters: Annotated class file object, can pass Myannotation03.class
Java EE notes