Define an annotation with @interface:
package annotationTest;
import java.lang.annotation.*;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodInfo {
String author(a) default "Jerry";
String version(a) default "1.0";
String date(a);
String comment(a);
}
Copy the code
Create a new class and annotate its methods with the annotations you just created:
package annotationTest;
// Jerry change for demo
public class AnnotationExample {
@Override
@methodinfo (author = "XXX ",version = "1.0",date = "2015/03/26",comment = "override toString()")
public String toString(a) {
return "AnnotationExample{}";
}
@Deprecated
@MethodInfo(comment = "deprecated method", date = "2015/03/26")
public static void oldMethod(a) {
System.out.println("old method, don't use it.");
}
@methodinfo (author = "Pankaj", comment = "Main method", date = "Nov 17 2012", version = "1.0")
public static void genericsTest(a) { oldMethod(); }}Copy the code
Printed output:
@annotationTest.MethodInfo(version="1.0", author="xxx", date="2015/03/26", comment="override toString()") in method:public java.lang.String annotationTest.AnnotationExample.toString()
Method with revision no 1.0 = public java.lang.String annotationTest.AnnotationExample.toString()
@annotationTest.MethodInfo(version="1.0", author="Pankaj", comment="Main method", date="Nov 17 2012") in method:public static void annotationTest.AnnotationExample.genericsTest()
Method with revision no 1.0 = public static void annotationTest.AnnotationExample.genericsTest()
@java.lang.Deprecated(forRemoval=false, since="") in method:public static void annotationTest.AnnotationExample.oldMethod()
@annotationTest.MethodInfo(version="1.0", author="Jerry", comment="deprecated method", date="2015/03/26") in method:public static void annotationTest.AnnotationExample.oldMethod()
Method with revision no 1.0 = public static void annotationTest.AnnotationExample.oldMethod()
Copy the code
Similarly, create a new description annotation that decorates the People class:
package annotationTest;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.*;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String desc(a);
String author(a);
int age(a) default 18;
}
Copy the code
package annotationTest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@Description(author = "Jerry", desc = "Class annotation", age = 35)
public class People {
@Description(author = "Jerry 2", desc = "method annotation", age = 35)
public void hello(a){}@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] arg){
try {
// Use the class loader to load classes
Class c = Class.forName("annotationTest.People");
// Find the annotation above the class
boolean isExist = c.isAnnotationPresent(Description.class);
// This method uses the class to determine if the class has an annotation such as Description
if (isExist) {
// Get the annotation instance and parse the annotation above the class
Description d = (Description) c.getAnnotation(Description.class);
System.out.println(d.desc());
}
// Get all the methods
Method[] ms = c.getMethods();
// Iterate over all the methods
for (Method m : ms) {
boolean isExist1 = m.isAnnotationPresent(Description.class);
if(isExist1) { Description d1=m.getAnnotation(Description.class); System.out.println(d1.desc()); }}// Another method of parsing
for (Method m : ms) {
// Get all the annotations on the method
Annotation[] as=m.getAnnotations();
for (Annotation a : as) {
// Use the binary operator to determine if a is an instance of Description
if (a instanceofDescription) { Description d=(Description) a; System.out.println(d.desc()); }}}}catch(ClassNotFoundException e) { e.printStackTrace(); }}}Copy the code
Output:
Class annotation
method annotation
method annotation