J2se1.5 annotation syntax

Time:2021-11-30

Speaking of annotation syntax, it is familiar to any Java developer. We use @ author, @ param, etc. to write annotations every day, and then generate documents with Javadoc. This convenient document generation method of Java has been widely praised by developers. Since JDK 1.5, annotation syntax has provided more powerful functions.

Let’s talk about the annotation syntax itself, which is sometimes called meta data: “data describing data”. Generally speaking, they can be used to generate documents, check dependencies between codes, and help the compiler check syntax. Nowadays, the more popular tools are XDoclet and so on. For document generation, there has long been a perfect performance of Javadoc tools, and for code inspection, Java now also provides language level support.

As we know, Javadoc generates documents by extracting tag information from Java source files. Therefore, to learn the new annotation syntax, the first thing you should be familiar with is the new tags. The new annotation syntax supports two types of tags, system standard tags and user-defined tags. The symbol of the label is the same as before, and the @ symbol is added with the label name. Let’s start with the standard label of JDK1.5.

First of all, there is no need to be wordy about @ override. As the name suggests, it is used to explain the method override. We assume that a subclass must override the methods of the parent class

  ================================================================================
  public class Parent{

  public void foo(){
  System.out.println(“Original Implementation of foo”);
  }

  }

  public class Child extends Parent{

  @Override
  public void foo(){
  System.out.println(“Overide Implementation of foo”);
  }

  }
  ================================================================================

So far, we can’t see that this @ override has brought us any benefits, so let’s talk about what the compiler does when we compile with javac after adding this label? The compiler will check this method, and then find out whether there is this method from the parent class, otherwise there will be a compilation error. This feature can help us avoid some low-level errors. In the above example, the subclass wants to override the foo () method, but you may inadvertently write it as FOB (). For such “low-level errors”, if you don’t find them in the early stage, it may take you hours or even a day or two to find them during system integration testing. Now, the compiler will give errors when compiling,

  Child.java:3: method does not override a method from its superclass
  @Override
  ^
  1 error

Well, this function is pretty good.
Having seen how to use standard labels, let’s take a look at user-defined labels. First introduce @ interface, which is used to define a new annotation type. Creating a new annotation type looks like defining an interface. Mytag.java is used to create a user-defined tag. The code is as follows,

  ===============================================================================
  package tiger.annotation;
  /**
* user defined label?? MyTag
  */
  public @interface MyTag { }

After defining a tag, we can use it in any Java file,
  import tiger.annotation.MyTag;
  public class TagTest{

  @MyTag
  public void testTag(){
  }
  }
  ===============================================================================

Annotation types can also have member variables,

  ==============================================================================
  package tiger.annotation;
  /**
* user defined label?? Mytag with member variables
  */
  public @interface MyTag {

  String name();

  int age();
  }
  =============================================================================

Then we can use this label like this,

  @MyTag(name=”MyTag”,age=1)
  public void testTag(){
  }

The tag is ultimately used to help developers extract annotation information, and then further process it according to different needs. Let’s take a look at how to obtain annotation information.

  =============================================================================
  import java.lang.annotation.Annotation;
  import tiger.annotation.MyTag;
  public class TagTest{

  @MyTag(name=”MyTag”,age=1)
  public void test(){
  }

  public static void main(String[] args){
  TagTest tt = new TagTest();
  try {
  Annotation[] annotation =tt.getClass().getMethod(“test”).getAnnotations();
  for (Annotation tag :annotation) {
  System.out.println(“Tag is:” + tag);
  System.out.println(“tag.name()” + ((MyTag)tag).name());
  System.out.println(“tag.age()” + ((MyTag)(tag)).age());
  }
  } catch(NoSuchMethodException e) {
  e.printStackTrace();
  }
  }
  }
  ===============================================================================

It should be noted that we still have a little work to do before executing this code. We also need to add a description tag @ retention to our custom tag mytag, indicating that the annotation information can be obtained through the reflection mechanism at runtime. If you don’t add this tag, the above code will have no output. The modified mytag is as follows:

  ================================================================================
  /**
* user defined label?? Mytag with member variables
  */
  @Retention(RetentionPolicy.RUNTIME)
  public @interface MyTag {

  String name();

  int age();
  }
  ================================================================================

Then we execute tagtest to get the following output,

  Tag is:@tiger.annotation.MyTag(name=MyTag, age=1)
  tag.name()MyTag
  tag.age()1

Well, the basic usage of tiger’s new annotation syntax is so simple. Although the basic usage is simple, it is worth considering how to deal with it after obtaining annotation information. We can use them to do some syntax checks, file correlation checks, various statistics, etc. For more information about Tiger’s new annotation syntax, visit [link]= http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html ]。

The above code is passed under Win2K + j2se5 GA.

Recommended Today

Golden nine silver ten: byte three-sided warp at the front end of a year

background Recently, I also wrote an article in naked CIGolden nine silver ten: Interview sharing at the front of the year, after many days,Finally summoned up the courage to resume the interview, the reason why the byte face is written separately,Because you really need to know some big factory interview experience in advance, the problem […]