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