Basic knowledge annotation

1. What is a comment

  • Annotation is from the beginning of the introduction of new technology JDK5.0
  • Annotation role
    • Not the program itself, can explain (and this comment (Comment) makes no difference) program
    • Other programs may be: reading (such as compilers, etc.)
  • Annotation
    • Notes on a "@ annotation name" exists in the code, you can also add some parameters, such as: @SuppressWarnings (value = "unchecked").
  • Annotation using Where
    • Can be attached to the above package, class, method, field, etc., to give them the equivalent of adding an extra auxiliary information, we can achieve access to these metadata through programming reflection.

 

2, built-in annotations (annotation system already exists)

  • @Override: definition java.lang.Override in this comment applies only to rhetorical approach represents a method declaration is intended to rewrite another method declaration in a superclass.
  • @Deprecate: java.lang.Deprecate definition, then this annotation can be used to modify the methods, properties, classes, represents program does not encourage the use of such elements apes, typically because it is dangerous or there is a better choice.
  • @SuppressWarnings: definition java.lang.SuppressWarnings, the suppression with a warning message when compiled.
    • The previous two comments differ, you need to add a parameter to the proper use of these parameters are well defined, we selectively use just fine
      • @SuppressWarning("all")
      • @SuppressWarning("unchecked")
      • @SuppressWarning(value={"unchecked", "deprecation"})
      • and many more.......
 

3, meta-annotation

  • Meta-annotation role is responsible for other annotation annotation, java defines four standard meta-annotation type, they are provided to other annotation types stated,
  • These types are supported and their classes can be found in the package java.lang.annotation (@Target, @Retention, @Documented, @Inherited)
    • @Target: used to describe the use of annotations (i.e.: annotations are described may be used in any place)
    • @Retention: indicate what level you need to save the annotation information for annotation describing the life cycle
      • SOURCE<CLASS<RUNTIME
    • Document: indicates that the annotation will be included in the javadoc
    • Inherited: Description subclass can inherit the parent class notes

 

4, custom annotations

  • @Interface use custom annotations automatically inherits java.lang.annotation.Annotation Interface
  • analysis:
    • @interface used to declare an annotation format: public @ interface defined annotation content name} {
    • Each of which method is actually declares a configuration parameter
    • Name is the name of the method parameter
    • The return value type is a type parameter (return value can only be a primitive type, Class, String, enum)
    • You can declare the default value of the parameter by default
    • If only one member of parameters, general parameter named value, when in use you can omit the parameter name
    • Notes element must have a value, when we define annotation elements, often use the empty string as the default value 0

Deprecated to annotate an example:

 1 @Documented
 2 @Retention(RetentionPolicy.RUNTIME)
 3 @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
 4 public @interface Deprecated {
 5     /**
 6      * Returns the version in which the annotated element became deprecated.
 7      * The version string is in the same format and namespace as the value of
 8      * the {@code @since} javadoc tag. The default value is the empty
 9      * string.
10      *
11      * @return the version string
12      * @since 9
13      */
14     String since() default "";
15 
16     /**
17      * Indicates whether the annotated element is subject to removal in a
18      * future version. The default value is {@code false}.
19      *
20      * @return whether the element is subject to removal
21      * @since 9
22      */
23     boolean forRemoval() default false;
24 }
Deprecated

 

 

 

Guess you like

Origin www.cnblogs.com/zitai/p/12234217.html