Java annotations (annoation) Facts speak

Source: Internet
Author: User

1 examples of @Test in JUnit: 1.1 before annotations (@Test)
Private Boolean Istestmethod (Method m) {          return               = = 0 &&               m.getname (). StartsWith ( "test") &&               m.getreturntype (). Equals (Void.type);  }  

Before using annotations (prior to JUNIT4), JUnit generally obtains a test method in a test class by similar code with the above, obtains the method object through reflection, and then judges that the logic is essentially to see a test method should be: start with Test , no parameters, no return value.

1.2 After using annotations (@Test)
@Test  Public void throws Exception {}

Here the main role of @test is to mark testname This method for a test method , the role of labeling, can be regarded as a marker annotation. As with the above, the method labeled for @test is public, parameterless, and has no return value, and a compilation error occurs if the two rules are violated.

1.2.1 Getting the test method flow
    1. First, get the class object for the classes you want to test
    2. You can then get an array of methods corresponding to all of the public method.
    3. Traverse method object to get each method object
    4. By calling the isannotationpresent (Test.class) method, you can check whether the method is named Test annotation,
    5. If this annotation is available, the method object's invoke () method is called to execute the method.
 for (Method Method:foo. class . Getdeclaredmethods ()) {    if (Method.isannotationpresent (org.junit.Test).  Class) {        System.out.println ("method" + Method + "has junit @Test annotation.") );    }}

definition of 1.2.2 @Test:

@Retention (retentionpolicy.runtime) @Target ({Elementtype.method}) Public@InterfaceTest {/*** Default Empty exception*/    Static classNoneextendsThrowable {Private Static Final LongSerialversionuid = 1L; PrivateNone () {}}/*** Optionally specify <CODE>EXPECTED</CODE>, a throwable, to cause a test method to succeed IFF *     An exception of the specified class was thrown by the method. */Class<?extendsThrowable> expected ()defaultNone.class; /*** Optionally specify <code>timeout</code> in milliseconds to cause a test method to fail if it *     Takes longer than that number of milliseconds. */    LongTimeout ()default0L;}

Ignore the other details first, @interface, like class, Enum, interface, etc. in Java. This keyword declaration implies a message that it inherits the Java.lang.annotation.Annotation interface and does not declare a interface.

Two members were declared inside:

    • Expected, used to verify that the test method throws the expected exception
    • Timeout, which verifies whether the test method can run out within a specified time.
Meta annotations

Java meta annotations are used to modify annotations, which may be used when customizing annotations. There are roughly the following types:

    1. @Document represents this annotation or appears in the document that corresponds to the destination where it is used.
    2. @Retention Retention Policies
    3. @Target annotations Apply to target
    4. @Inherited By default a note is used somewhere in the parent class, and it is not inherited to the subclass, and if you add @inherited to the annotation, it is inherited to the child class.
annotation retention Policies

As the above @Retention (retentionpolicy.runtime) indicates that the annotation is retained at run time, where @Retention is a meta-annotation, the role of the meta-annotation is to be responsible for annotating other annotations. @Retention control the retention policy for this annotation here, the optional values are:

    1. Sourc Valid in source code
    2. Class: Valid in class file (i.e. class reserved, default )
    3. Runtime: Valid at run time (that is, runtime retention)
    Application scope of annotations

  The @Target ({Elementtype.method}) indicates that the annotation applies to the method, and the optional values are:

    1. TYPE,/** Field declaration (includes enum constants) */
    2. FIELD, /** Method declaration * /
    3. METHOD, /** Parameter declaration * /
    4. PARAMETER, /** Constructor declaration * *
    5. CONSTRUCTOR, /** Local Variable declaration * /
    6. local_variable, /** Annotation type declaration * /
    7. Annotation_type, /** Package declaration * /
    8. Package
Annotation effect
    1. Generate the document. This is the most common, and also the earliest annotations that Java provides. Commonly used @see @param @return, etc.
    2. Tracking code dependencies, implementing alternative profile features. The most common is the annotation-based configuration that starts with spring 2.5. The function is to reduce the configuration. Now the framework basically uses this configuration to reduce the number of configuration files.
    3. A format check is made at compile time. such as @override before the method, if you do not override the method of the superclass method, compile time can be checked out.
Other basic built-in annotations

@Override Annotations Enable compile-time checking, you can add the comment to your method to declare that the method is used to overwrite the method in the parent class. If the method is not a method that overrides the parent class, it will be wrong at compile times. For example, we rewrite the ToString () method for a class and write ToString (), and we add a @override comment to the method;

  The role of @Deprecated is to add comments to the methods that should not be used, and when programmers use them, they will display a hint at compile time, which has the same function as the @deprecated tag in Javadoc, and, to be exact, It's not as good as Javadoc @deprecated, because it doesn't support parameters,

@SuppressWarnings is different from the first two comments, you need to add a parameter to use correctly, these parameter values are already defined, we use selectively, the parameters are as follows:

Standing on the shoulders of giants, reference materials:

Http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html

Http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

Http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html

Http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html

Http://www.cnblogs.com/phoebus0501/archive/2011/02/21/1960077.html

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.