Summary of Knowledge Points of Basic Annotations in java

  • 2021-10-13 07:33:08
  • OfStack

1. java. lang. Override is a tag type annotation, which is used as a annotation method. He explains that this method inherits from the parent class, that is, overrides the method with the same name in the parent class.


@Override
public void onCreate(Bundle savedInstanceState)

{ ... .}

This writing is correct, if you write:


@Override
public void oncreate(Bundle savedInstanceState)
{ ... .}

The compiler reports the following error: The method oncreate (Bundle) of type HelloWorld must override or
implement a supertype method to make sure you override the onCreate method correctly (because oncreate should be onCreate)
If you don't add @ Override, the compiler will not detect an error, but will assume that you have defined a new method for the subclass: oncreate

2. SuppressWarnings

It tells the compiler to ignore possible warnings. In fact, there are many parameters in it, and different parameters set different warnings.

-deprecation, warning when obsolete classes or methods are used
-unchecked, warning when an unchecked conversion is performed
-fallthrough, a warning when the switch block leads directly to the next case without break
-path, warning when there is a path that does not exist in the ClassPath, Source File Path, and so on
-serial, warning when an serialVersionUID definition is missing on a serializable class
-finally, warning if any finally clause does not complete properly
-all, warning for all of the above

Content extension:

Custom annotation test

Defines the annotation @ RepeatMethod with the int type parameter value.


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatMethod {

  int value() default 1;

}

Apply annotations to the doSomeThing method. If the annotation parameter to be specified is value and no other parameter value is specified, you can use abbreviation to omit the parameter name and equal sign.


public class Demo {
  @RepeatMethod(5)
  public void doSomeThing(){
    System.out.println("---- Annotation test ----");
  }
}

Obtain annotation information through reflection, and then do corresponding processing. For example, suppose that the annotation of @ RepeatMethod is to call the annotated method repeatedly, and the parameter value specifies the number of times to call the method repeatedly:


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyDemo {
  public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
    Class cla = Demo.class;
    Method[] methods = cla.getMethods();
    Object demo = cla.newInstance();
    for (Method method : methods){
      // Whether there is a mark on the judgment method @RepeatMethod Annotation 
      if(method.isAnnotationPresent(RepeatMethod.class)){
        // Get RepeatMethod Parameter values for annotations 
        RepeatMethod repeatMethod = method.getAnnotation(RepeatMethod.class);
        for (int i = 0;i < repeatMethod.value(); i++)
        method.invoke(demo, null);
      }
    }
  }
}

Output:


---- Annotation test ----
---- Annotation test ----
---- Annotation test ----
---- Annotation test ----
---- Annotation test ----

Related articles: