Annotation annotation, which explains the code when writing code the annotation is not a thing at all, the annotation here refers to the special tags in the code that can be read at compile, class loaded, run time, and perform corresponding processing. With these tags, you can embed some additional information in the source file without changing the original logic. Annotation can be used to modify the declarations of packages, classes, constructors, methods, member variables, parameters, and local variables.
In fact, Java in the annotation and C # is the same thing, are used to set the metadata used, their difference is only a few places
The following is a very good post.
http://rednaxelafx.iteye.com/blog/464889
There are 3 built-in annotation in Java, @Override (qualify for overriding the parent class method), @Deprecated (marked obsolete), @SuppressWarningsAnnotation (suppress compiler Warnings)
Custom annotation such as the following format, you need to use the @interface keyword, to use annotation need to reference Java.lang package
Let's look at an example using annotation
Import java.lang.annotation.*;
@Retention (retentionpolicy.runtime)
@Target (Elementtype.method)
//Define a markup annotation that does not contain any member variables, that is, not passed-in metadata Public
@interface testable
{
}
public class MyTest {//Use @testable tag annotation to specify that the method is testable @Testable public static void M1 () {} public S tatic void m2 () {}///using @testable tag annotation Specifies that the method is testable @Testable public static void M3 () {throw new Runtime
Exception ("Boom"); public static void M4 () {}///use @testable tag annotation to specify that the method is testable @Testable public static void M5 () {} publi c static void M6 () {}///use @testable tag annotation to specify that the method is testable @Testable public static void M7 () {throw new Runti
Meexception ("Crash"); public static void M8 () {}}
Import java.lang.reflect.*;
public class Testprocessor
{public
static void process (String clazz)
throws ClassNotFoundException
{
int passed = 0;
int failed = 0;
All methods for the Obj object are traversed for
(method M:class.forname (Clazz). GetMethods ())
{
//if the @testable tag annotation is included if
( M.isannotationpresent (Testable.class))
{
try
{
//Call M method
M.invoke (null);
Passed plus 1
passed++;
}
catch (Exception ex)
{
System.out.printf ("method + M +" Run failed, exception: "+ ex.getcause () +" \ n ");
failed++
}
}} Statistical test results
System.out.printf ("Running:" + (passed + failed) + "method, where: \ n" +
"failed:" + failed + ", \ n" + "
succeeded:" + P Assed + "one. \ n ");
}
}
Main Program Class
Import java.lang.reflect.*;
public class runtests
{public
static void Main (string[] args) throws Exception
{
//Processing MyTest class
testprocessor.process ("MyTest");
}
where M3 and M7 two methods throw their own exceptions, leaving two empty methods to execute successfully