Get fewer method callouts

Source: Internet
Author: User

Comments do not interfere with the operation of the original code. Play a role in the logo. Annotation tools allow third parties to solicit

Take notes to complete the required tasks.

Package Two.zj;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;@ Documented@retention<span style= "color: #ff0000;" > (</span><span style= "Background-color:rgb (255, 0, 0);" >retentionpolicy.runtime</span><span style= "color: #ff0000;" >) </span> @Target (elementtype.method) public @interface TESTZJ {public int id () default 1;public String name () Default "Nametest";p ublic String value ();p ublic abstract int [] Intt ();p ublic abstract testenum[] Tenum ();}
Package Two;import Two.zj.testenum;import Two.zj.testzj;public class Userann {<span style= "White-space:pre" > </span>/**<span style= "White-space:pre" ></span> * @param user<span style= "White-space:pre" > </span> * @return user<span style= "White-space:pre" ></span> */<span style= "White-space:pre" > </span> @TestZJ (id=22,name= "Liyy", Intt = {0,1,2,3}, value = "", Tenum = {testenum.test1}) <span style= "White-spa Ce:pre "></span>public user getUser (user user) {<span style=" white-space:pre "></span>return user ; <span style= "White-space:pre" ></SPAN>}}
package;


Import Java.lang.reflect.Method;


Import Two.zj.TestZJ;


public class Testmain {


public static void Main (string[] args) throws ClassNotFoundException {
// Class Class1 = Class.forName (". Userann ");
Class Class1 = Userann.class;
Method [] methods = Class1.getdeclaredmethods ();
for (Method method:methods) {
TESTZJ annotations = method.getannotation (Testzj.class);
if (annotations!=null) {
System.out.println (Annotations.id ());
}
}
}
}

When the @Retention (retentionpolicy.runtime) in the annotation class Testzj is either source or class, the reflection is not obtained for the annotation class. Because of the JDK instructions

public enum Retentionpolicy {
   /**
     * Annotations is discarded by the compiler.
     */
    SOURCE,


   /**
     * Annotations is recorded in the class file by the compiler
     * and need not being retained by the VM at run Ti Me.  this is the default
     * behavior.
     */
    CLASS,


   /**
     * Annotations is to be Recorded in the class file by the compiler and
     * retained by the VM at run time, so they could be re Ad reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

Source removes annotations from the compiled C-byte code. Annotations in CLASS bytecode are not loaded into the virtual machine. The runtime is able to obtain annotation information through reflection, and the annotation framework is the principle used.


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Get fewer method callouts

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.