Annotate parameters and default values

Annotation parameters

Each Annotation is associated with one RetentionPolicy and is associated with 1 to n ElementTypes

ElementType == @target ==

TYPE: Annotation "class, interface (including annotation TYPE), or enumeration declaration".Copy the code
FIELD: indicates "FIELD declaration".Copy the code
METHOD: Mark "METHOD".Copy the code
PARAMETER: indicates "PARAMETER".Copy the code
CONSTRUCTOR: Annotate "CONSTRUCTOR".Copy the code
LOCAL_VARIABLE: annotate "local variable".Copy the code

RetentionPolicy keyword: == @retention ==

SOURCE: Annotation exists only for the duration of the compiler's processing; after the compiler's processing, the Annotation is no longer useful. For example, the "@Override" flag is an Annotation. When it decorates a method, it means that the method overrides the parent method. And syntax checks are done during compilation! After the compiler has processed it, @override has no effect.Copy the code
CLASS: The compiler stores the Annotation in the corresponding.class file of the CLASS, which is the default behavior of the Annotation.Copy the code
RUNTIME: The compiler stores the annotations in a class file that can be read in by the JVM.Copy the code

@Deprecated

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Deprecated {
}
Copy the code

Description:

  1. @ interface – it’s used to modify Deprecated, means Deprecated implements the Java lang. The annotation. The annotation interface; Deprecated is a note.
  2. Documented — Identifies the annotation that can appear in Javadoc.
  3. @Retention(retentionPolicy.runtime) — This specifies that the Deprecated policy is retentionPolicy.runtime. This means that the compiler keeps Deprecated information in a.class file that can be read by the virtual machine.
  4. Deprecated content is no longer recommended.

For example, if a method is tagged @deprecated, it is no longer recommended. If a developer tries to use or override a method marked with @deprecated, the compiler prompts the developer.

@Inherited

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
Copy the code

Description:

  1. Used to modify Inherited @ interface – it means Inherited a Java implementation. Lang. The annotation. The annotation interface; Inherited is an annotation.
  2. Documented — Identifies the annotation that can appear in Javadoc.
  3. @Retention(retentionPolicy.runtime) — This specifies that Inherited policies are RetentionPolicy.runtime. This means that Inherited information is kept in a.class file by the compiler and can be read by the virtual machine.
  4. @target (elementtype. ANNOTATION_TYPE) — This specifies Inherited is ANNOTATION_TYPE. This means that @Inherited can only be used to annotate Annotation types.
  5. And what @Inherited means is that the annotations that it annotates will be Inherited.

For example, we defined an Annotaion whose name is MyAnnotation, and MyAnnotation is annotated @Inherited. Now, some class Base that uses MyAnnotation, Base has “has annotation MyAnnotation”; Now, Sub inherits from Base, and since MyAnnotation is @Inherited, Sub also has MyAnnotation.

@SuppressWarnings

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    String[] value();
}
Copy the code

Description:

  1. @ interface – it’s used to modify SuppressWarnings, means SuppressWarnings implements the Java lang. The annotation. The annotation interface; SuppressWarnings is an annotation.
  2. @Retention(retentionPolicy.source) — This is used to specify that SuppressWarnings is retentionPolicy.source. This means that SuppressWarnings information exists only for the duration of the compiler’s processing, after which SuppressWarnings has no effect.
  3. @target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) — this specifies the SuppressWarnings TYPE and includes both TYPE, FIELD, and SuppressWarnings. METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE.
  4. String[] value(); SuppressWarnings means that SuppressWarnings can specify parameters
  5. SuppressWarnings allows the compiler to remain silent on certain warnings for “what it tagged”.

For example: “@SuppressWarnings(value={“deprecation”, “unchecked”})” represents silence on” SuppressWarnings no longer recommended “and” warnings when unchecked transitions “in” what it’s labeled “.

Added: SuppressWarnings Common keywords

Deprecation -- Warning when using deprecated classes or methods -- warning when performing unchecked conversions, such as failing to specify the types held by the collection when using it. Fallthrough - warning when the Switch block leads directly to the next case without a Break. Path - Warning if there is a non-existent path in the classpath, source file path, etc. Serial - Warning when serialVersionUID definition is missing on a serializable class. Finally -- A warning when any finally clause does not complete properly. All -- a warning about all of the above.Copy the code

The default value

Use the following syntax to define default values for custom annotations:

public @interface MyAnnotation {
    String value(a) default"Default value";
}
Copy the code

The key default specifies the default value.

The default value must be a type compatible with the element’s data type.

The following code creates the Version annotation type by setting the default value of its minor element to zero, as shown below:

public  @interface  Version {
    int major(a);
    int minor(a) default 0; 
}
Copy the code

example

The following code shows how to use comments with default values.

@Version(major=1) // Minor is not specified here
@Version(major=2, minor=1) // 
Copy the code

The following code shows how to specify default values for arrays and other data types:

public @interface Version {
  double d(a) default 1.89;

  int num(a) default 1;

  int[] x() default { 1.2 };

  String s(a) default "Hello";

  String[] s2() default { "abc"."xyz" };

  Class c(a) default Exception.class;

  Class[] c2() default { Exception.class, java.io.IOException.class };
}
Copy the code