Java 1.5 new features Annotations__java

Source: Internet
Author: User
In fact, the annotation syntax is familiar to any Java developer, we use @author every day, @param, and so on, write comments, and then generate documents with Javadoc. This handy documentation generation method for Java is widely praised by developers. Starting with JDK1.5, annotation syntax provides a more powerful feature. Let's talk about the annotation syntax itself, which is sometimes called Meta-data: "data that describes data." In general, they can be used to generate documents, check dependencies between codes, and help the compiler make grammar checks. Nowadays the more popular tools have Xdoclet and so on. There is already a perfect performance of the Javadoc tool for document generation, and Java now provides language-level support for code checking. We know that Javadoc generates documents by extracting tag information from Java source files. So to learn the new annotation syntax, the first thing to be familiar with is the new tag. The new annotation syntax supports two types of tags, system standard tags, and user-defined labels. The symbol for the label is the same, the @ symbol plus the label name. Let's start with the standard label that JDK1.5. First introduced @override, also need not wordy, as the name suggests, is used to explain the method of the cover. We assume that there is a subclass that must override the method 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 start with adding this tag, and when we compile with javac, what does the compiler do? The compiler checks this method and then looks for this method from the parent class, or it compiles an error. This feature can help us avoid some low-level errors. The example above, the subclassTo override the Foo () method, however, you may inadvertently put it on the FOB (), for such a "low-level error", if you do not find in the early days, to the system integration test, you may be a few hours or even one or two days to find such a bug. Well now, the compiler will give an error at compile time, Child.java:3: method does not override a method from its superclass @Override ^ 1 Error How, this function is not bad it. Looking at the use of standard tags, let's look at user-defined tags. Introduce @interface first, which defines the new annotation type (annotation type). Creating a new annotation type appears to be no different from defining a interface, Mytag.java is used to create a new user-defined label, as follows, ================================================== ============================== package tiger.annotation; /** * user-defined label?? MyTag/Public @interface MyTag {} Defines a tag, we can use this tag 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/Public @interface MyTag with member variables {String name (); int age ();} =================================================== ========================== ThenWe can use this tag in this way, @MyTag (name= "MyTag", age=1) public void Testtag () {} uses the tag to help the developer extract the annotation information and then make further processing according to different requirements, Let's take a look at how to get the 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 ()); The catch (Nosuchmethodexception e) {e.printstacktrace ();}} The point that =============================================================================== need to be aware of is that we have a little bit of work to do before this code is executed, We also need to add a description tag to our custom label MyTag, @ Retention, which indicates that the annotation information will be available at runtime through the reflection mechanism. If you do not add this tag, the code above will not have any output. After the revision of the MyTag as follows, ================================================================================/** * User Defined label?? MyTag/@Retention (retentionpolicy.runtime) public @interface with a member variable MyTag {String name (); int age ();}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.