Java Custom Annotation Implementation

Source: Internet
Author: User

Package annotition;

Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Target (Elementtype.type)
@Retention (Retentionpolicy.runtime)
@Documented
Public @interface ClassInfo {
String name () default "";

}

++++++++++++++++++++++++++++++++++

Package annotition;

Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;

@Target (Elementtype.method)
@Retention (Retentionpolicy.runtime)
@Documented
Public @interface MethodInfo {
String value () default "";
}

++++++++++++++++++++++++++++++++++++++++++++++++

Package annotition;
@ClassInfo (name = "Hellow World")
public class Myannotition {
@MethodInfo ("Hello")
public void Java () {}
}

+++++++++++++++++++++++++++++++++++++++++++++++

Package annotition;

Import static org.junit.assert.*;

Import java.lang.annotation.Annotation;
Import Java.lang.reflect.Method;

Import Junit.framework.Test;

public class Annotationparse {
public static void Parse () {
Class clazz = Myannotition.class;
ClassInfo annotations exist on this class
if (Clazz.isannotationpresent (Classinfo.class)) {
Get the annotations of a class from a class
ClassInfo ClassInfo = (ClassInfo) clazz.getannotation (Classinfo.class);
Output the Name property of the annotation
System.out.println (Classinfo.name ());


}
Get all the methods of the class
Method[] methods = Clazz.getmethods ();
for (Method method:methods) {
If MethodInfo annotations exist on this method
if (Method.isannotationpresent (Methodinfo.class)) {
Get the MethodInfo annotations above
MethodInfo MethodInfo = method.getannotation (Methodinfo.class);
The Value property in the output annotation
System.out.println (Methodinfo.value ());
}

}

}
@org. junit.test
public void TestName () throws Exception {
Parse ();
}
}

Java Custom Annotation Implementation

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.