Java SE-reflect

Source: Internet
Author: User

Test class

 Package  Testreflect;  /**  * @ Package testreflect * @ classname: HELLO * @ Description: Todo ()*  @ Author  Andy * @ date 2013-5-31 09:59:29 AM  */  Public   Class  Hello {  Public  String name;  Private   Int Id = 1;  Public  Hello (){}  Public Hello (string name, Int  ID ){  This . Name = Name;  This . ID = ID ;}  Public  String GetUserName (){  Return "123" ;} Public  String getname (){  Return  Name ;}  Public   Void  Setname (string name ){  This . Name = Name ;}  Public   Int  GETID (){  Return  ID ;}  Public  Void Setid ( Int  ID ){  This . ID = ID ;}} 

View code

Three methods to obtain the object of a class or object runtime class

 Package  Testreflect;  /**  * @ Package testreflect * @ classname: instancingclass * @ Description: Todo (three methods for obtaining the class object during running of a class or object )*  @ Author  Andy * @ date 2013-5-31 09:59:05 AM  */ Public   Class Instancingclass <t> {  Public   Static   Void Main (string [] ARGs) Throws  Classnotfoundexception {  //  First Class <Hello> classtype1 = hello. Class  ;  //  Second Class <?> Classtype2 = Class. forname ("testreflect. Hello" );  //  Third Hello = New  Hello (); Class <? Extends Hello> classtype3 = Hello. getclass (); system. Out. println (classtype1 + "\ N" + classtype2 + "\ n" + classtype3 + "\ n" ); System. Out. println (classtype1.getname () + "\ N" + classtype2.getname () + "\ n" + classtype3.getname () + "\ n" ); System. Out. println (classtype1.getsimplename () + "\ N" + classtype2.getsimplename () + "\ n" + classtype3.getsimplename () + "\ n" );}} 

View code

Three methods for instantiating objects without the New Keyword

 Package  Testreflect;  Import  Java. Lang. Reflect. constructor;  Import  Java. Lang. Reflect. invocationtargetexception;  /**  * @ Package testreflect * @ classname: instancingobject * @ Description: Todo (three methods to instantiate an object without the New Keyword )*  @ Author Andy * @ date 2013-5-31 10:08:34 AM  */  Public   Class  Instancingobject {  Public   Static   Void Main (string [] ARGs) Throws  Instantiationexception, illegalaccessexception, nosuchmethodexception, securityexception, illegalargumentexception, invocationtargetexception {Hello hello0 = Hello. Class  . Newinstance (); Class <Hello> classtype = hello. Class  ;  //  When the constructor has no parameters, the first is equivalent to the second.  //  First Hello hello1 = (Hello) classtype. newinstance ();  //  Second Constructor <Hello> con = Classtype. getconstructor (); Hello hello2 = (Hello) con. newinstance ();  // Third Constructor <Hello> con2 = classtype. getconstructor ( New Class [] {string. Class , Int . Class  }); Hello hello3 = (Hello) con2.newinstance ( New Object [] {"123", 123 }); System. out. println (hello0.getusername (); system. out. println (hello1.getusername (); system. out. println (hello2.getusername (); system. out. println (hello3.getusername ());}} 

View code

Operations on fields through reflection

Package  Testreflect;  Import  Java. Lang. Reflect. accessibleobject;  Import  Java. Lang. Reflect. field;  /**  * @ Package testreflect * @ classname: accessfieldbyreflect * @ Description: Todo (operations on fields through reflection )*  @ Author  Andy * @ date 2013-5-31 10:48:13 AM  */  Public   Class  Accessfieldbyreflect { Public   Static   Void Main (string [] ARGs) Throws  Illegalargumentexception, illegalaccessexception, instantiationexception {hello = Hello. Class  . Newinstance (); field [] fields = Hello. getclass (). getdeclaredfields ();  //  Retrieve all fields through Traversal          For  (Field: fields ){  // System. Out. println (field );  System. Out. println (field. getname ();} system. Out. println ( "Before modification:" + Hello. getname (); system. Out. println ( "Before modification:" + Hello. GETID ());  For  (Field: fields ){  If ("ID" . Equals (field. getname ())){  //  Allow access to all private fields, methods, and constructor Elements Field. setaccessible ( True );  //  Field that can only be accessed Accessibleobject [] accessparam = {Field}; field. setaccessible (accessparam,  True  );  //  If the field is int, It is setint. If it is double, It is setdouble. Field. setint (hello, 123 );}  Else  {  //  The field is a string Field. Set (hello, "James" ) ;}} System. Out. println ( "Modified by reflection:" + Hello. GETID (); system. Out. println ( "Modified by reflection:" + Hello. getname ());}} 

View code

Operations on methods through reflection

 Package  Testreflect;  Import  Java. Lang. Reflect. accessibleobject;  Import  Java. Lang. Reflect. invocationtargetexception;  Import Java. Lang. Reflect. method;  /**  * @ Package testreflect * @ classname: accessmethodbyreflect * @ Description: Todo (operations on methods through reflection )*  @ Author  Andy * @ date 2013-5-31 11:25:39 AM  */  Public   Class  Accessmethodbyreflect {  Public   Static   Void Main (string [] ARGs) Throws Illegalargumentexception, illegalaccessexception, instantiationexception, invocationtargetexception, nosuchmethodexception, securityexception {hello = Hello. Class  . Newinstance (); method [] Methods = Hello. getclass (). getdeclaredmethods ();  //  Retrieve all fields through Traversal          For  (Method: Methods ){  //  System. Out. println (field ); System. Out. println (method. getname ();} system. Out. println ( "Before modification:" + Hello. getname (); system. Out. println ( "Before modification:" + Hello. GETID ());  For  (Method: Methods ){  If ("Setid" . Equals (method. getname ())){  //  Allow access to all private fields, methods, and constructor Elements Method. setaccessible ( True  ); //  Only allow access to the specified Method Accessibleobject [] accessparam = {Method}; method. setaccessible (accessparam,  True  );}  Else   If ("Setname" . Equals (method. getname () {method. Invoke (hello, "James" ) ;}} String OBJ = (String) Hello. getclass (). getdeclaredmethod ("getname" ). Invoke (Hello); system. Out. println ( "Returned value by reflecting the getname method:" +OBJ); system. Out. println ( "Modified by reflection:" + Hello. GETID (); system. Out. println ( "Modified by reflection:" + Hello. getname ());}} 

View code

 

 

 

Int returned by integer. type, that is, the native data class objects corresponding to them (all the packaging classes are like this ),

Integer. Class returns the corresponding class Object java. Lang. integer.

 

 

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.