Java custom Annotation, parse Annotation through reflection

Source: Internet
Author: User

Java custom Annotation, parse Annotation through reflection
Create a custom Annotationimport java. lang. annotation. *; import java. lang. reflect. method; @ brief ented @ Target (ElementType. METHOD) @ Retention (RetentionPolicy. RUNTIME) public @ interface MethodInfo {String author () default "hupeng"; String version () default "1.0"; String date (); String comment ();} annotation methods can't have parameters. annotation methods return types are limited to primitives, String, Enums, Nnotation or array of these. annotation methods can have default values. @ Target: @ Target indicates the range of objects modified by Annotation: Annotation can be used for packages and types (class, interface, enumeration, Annotation type) type members (methods, constructor methods, member variables, enumerated values), method parameters, and local variables (such as cyclic variables and catch parameters ). In the Annotation type declaration, target can be used to better clarify the object to be modified. Purpose: Describe the scope of use of the annotation (that is, where the description can be used) value (ElementType): 1. CONSTRUCTOR: used to describe the CONSTRUCTOR 2. FIELD: used to describe the domain 3. LOCAL_VARIABLE: used to describe local variables 4. METHOD: used to describe METHOD 5. PACKAGE: used to describe the PACKAGE 6. PARAMETER: used to describe PARAMETER 7. TYPE: used to describe classes, interfaces (including Annotation types), or enum declarations @ Retention: @ Retention defines the duration of Annotation Retention: Some Annotation only appears in the source code, while others are compiled in the class file. The Annotation compiled in the class file may be ignored by the virtual machine, others will be read when the class is loaded (note that the execution of the class is not affected because the Annotation and class are separated in use ). This meta-Annotation can be used to limit the "Life Cycle" of Annotation. Purpose: indicates the level at which the annotation information needs to be saved. It is used to describe the life cycle of the annotation (that is, the scope of the description Description in which the annotation is valid) values (RetentionPoicy) include: 1. SOURCE: valid in the SOURCE file (that is, the SOURCE file is retained) 2. CLASS: valid in the class file (that is, the class is retained) 3. RUNTIME: valid at run time (that is, reserved at run time) @ brief ented: @ brief ented is used to describe other types of annotation public APIs that should be used as annotation Program Members, therefore, it can be documented by tools such as javadoc. Incluented is a tag Annotation with no members. @ Inherited: @ Inherited meta annotation is a tag annotation. @ Inherited explains that a marked type is Inherited ., Then this annotation will be used as a subclass of this class. Note: The @ Inherited annotation type is Inherited by the subclass of the labeled class. The class does not inherit annotation from the interface it implements, and the method does not inherit annotation from the method it loads. Java's built-in Annotation starts from java 5 and comes with three standard annontation types, namely, Override and Java. lang. Override, which are a marker annotation type and are used as Annotation methods. It indicates that the labeled method overload the parent class method and plays the role of assertion. If we use this annotation, the java compiler will warn you of a compilation error when a method that does not overwrite the parent class method. This annotaton often adds a guaranteed verification process when we try to override the parent class method and make sure that the method name is wrong. Deprecated and Deprecated are also marker annotation ., The compiler will not encourage the labeled program element. Therefore, using this modifier has a certain degree of "continuity": if we use this obsolete type or member in code by inheritance or override, although the inherited or overwritten type or member is not declared as @ Deprecated, the compiler still needs to issue an alert. Note: The annotation type @ Deprecated is different from the @ deprecated tag in javadoc. The former is recognized by the java compiler, the latter is identified by the javadoc tool to generate documents (including descriptions of why Program Members are outdated, how they should be disabled or replaced ). SuppressWarnings, which tells the Java compiler to disable warnings for classes, methods, and member variables. Sometimes some warnings are generated during compilation. Some of these warnings hide bugs, some are unavoidable, and some do not want to see warning information, you can use this annotation to block them. SuppressWarning is not a marker annotation. It has a member of the String [] type. The value of this Member is the forbidden warning name. For the javac compiler, the compiler ignores unrecognized warning names. Next we will use the built-in Java Annotation and custom Annotation public class AnnotationExample {@ Override @ MethodInfo (author = "xxx", version = "1.0", date = "2015/03/26 ", comment = "override toString ()") public String toString () {return "AnnotationExample {}" ;}@ Deprecated @ MethodInfo (comment = "deprecated method ", date = "2015/03/26") public static void oldMethod () {System. out. println ("old method, don't use it. ");}@ SuppressWarnings ({"unchecked", "deprecation"}) @ MethodInfo (author = "Pankaj", comment = "Main method", date = "Nov 17 2012 ", version = "1.0") public static void genericsTest () {oldMethod () ;}} use reflection to parse Annotation. Note that the Retention Policy of our Annotation must be RUNTIME, otherwise, we cannot obtain any data from it at runtime. Import java. lang. annotation. annotation; import java. lang. reflect. method;/*** Created by Administrator on 2015/3/26. */public class AnnotationParsing {public static void main (String [] args) {for (Method method: AnnotationExample. class. getMethods () {if (method. isAnnotationPresent (MethodInfo. class) {for (Annotation annotation: method. getAnnotations () {System. out. println (annotation + "in method:" + method);} MethodInfo methodInfo = method. getAnnotation (MethodInfo. class); if ("1.0 ". equals (methodInfo. version () {System. out. println ("Method with revision no 1.0 =" + method );}}}}

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.