Java reflection classic instance Java reflection cookbook

Source: Internet
Author: User

 

 

Java provides a set of mechanisms to dynamically execute methods, constructor methods, and array operations. This mechanism is called reflection. Reflection mechanisms are the basis for implementation of many popular frameworks today, including spring and hibernate. The principle issue is not the focus of this Article. Next, let's learn about this wonderful mechanism in the instance.

Public object getproperty (Object owner, string fieldname) throws exception {<br/> class ownerclass = owner. getclass (); </P> <p> Field field = ownerclass. getfield (fieldname); </P> <p> Object Property = field. get (owner); </P> <p> return property; <br/>}

 


Class ownerclass = owner. getclass (): obtains the class of the object.

Field field = ownerclass. getfield (fieldname): obtains the attribute declared by the class through the class.

Object Property = field. Get (owner): obtains the instance of this attribute through the object. If this attribute is not public, illegalaccessexception is reported here.

 

2. Obtain the static attributes of a class.

Public object getstaticproperty (string classname, string fieldname) <br/> throws exception {<br/> class ownerclass = Class. forname (classname); </P> <p> Field field = ownerclass. getfield (fieldname); </P> <p> Object Property = field. get (ownerclass); </P> <p> return property; <br/>}

Class ownerclass = Class. forname (classname): first obtain the class of this class.

Field field = ownerclass. getfield (fieldname): Same as above, get the attribute declared by class through class.

Object Property = field. Get (ownerclass): This is somewhat different from the above. Because this property is static, it is directly retrieved from the class of the class.

3. Execute the method of an object

Public object invokemethod (Object owner, string methodname, object [] ARGs) throws exception {<br/> class ownerclass = owner. getclass (); <br/> class [] argsclass = new class [args. length]; <br/> for (INT I = 0, j = args. length; I <j; I ++) {<br/> argsclass [I] = ARGs [I]. getclass (); <br/>}< br/> method = ownerclass. getmethod (methodname, argsclass); <br/> return method. invoke (owner, argS); <br/>} 

Class owner_class = owner. getclass (): first, you must obtain the class of this object.

3 ~ Row 6: configure the class array of the parameter as the condition for finding the method.

Method method = ownerclass. getmethod (methodname, argsclass): Obtain the method to be executed through the method name and the class array of the parameter.

Method. Invoke (owner, argS): executes this method. The parameters of the invoke method are the objects that execute this method, and the parameter arrays. The returned value is an object and the return value of this method.

4. Execute the static method of a class

Public object invokestaticmethod (string classname, string methodname, <br/> object [] ARGs) throws exception {<br/> class ownerclass = Class. forname (classname); </P> <p> class [] argsclass = new class [args. length]; </P> <p> for (INT I = 0, j = args. length; I <j; I ++) {<br/> argsclass [I] = ARGs [I]. getclass (); <br/>}</P> <p> method = ownerclass. getmethod (methodname, argsclass); </P> <p> return method. invoke (null, argS); <br/>}

The basic principle is the same as that of instance 3. The difference is that the last line, one of the parameters of invoke is null, because this is a static method and does not need to be run by the instance.

 

5. Create an instance

Public object newinstance (string classname, object [] ARGs) throws exception {<br/> class newoneclass = Class. forname (classname); </P> <p> class [] argsclass = new class [args. length]; </P> <p> for (INT I = 0, j = args. length; I <j; I ++) {<br/> argsclass [I] = ARGs [I]. getclass (); <br/>}</P> <p> constructor cons = newoneclass. getconstructor (argsclass); </P> <p> return cons. newinstance (ARGs); </P> <p>}

The method described here is to execute a constructor with parameters to create an instance. If no parameter is required, you can use newoneclass. newinstance () directly.

Class newoneclass = Class. forname (classname): the first step is to obtain the class of the instance to be constructed.

6th ~ Row 10th: obtains the class array of the parameter.

Constructor cons = newoneclass. getconstructor (argsclass): Obtain the constructor.

Cons. newinstance (ARGs): creates an instance.

6. Determine whether it is an instance of a class

Public Boolean isinstance (Object OBJ, class CLs) {<br/> return Cls. isinstance (OBJ); <br/>}

7. Obtain an element in the array.

Public object getbyarray (Object array, int index) {<br/> return array. Get (array, index); <br/>}

Complete source code:

Import Java. lang. reflect. array; <br/> Import Java. lang. reflect. constructor; <br/> Import Java. lang. reflect. field; <br/> Import Java. lang. reflect. method; </P> <p>/** <br/> * Java reflection cookbook <br/> * @ author Michael Lee <br/> * @ since 2006-8-23 <br/> * @ version 0.1a <br/> */</P> <p> public class reflection {<br/>/** <br/> * Get an object <br/> * @ Param owner, fieldname <br/> * @ return this property object <br/> * @ throws exception <br/> */<br/> Public object getproperty (Object owner, string fieldname) throws exception {<br/> class ownerclass = owner. getclass (); </P> <p> Field field = ownerclass. getfield (fieldname); </P> <p> Object Property = field. get (owner); </P> <p> return property; <br/>}</P> <p>/** <br/> * obtain a static public attribute <br/> * @ Param classname class name <br/> * @ Param fieldname attribute name <br/> * @ return this attribute object <br/> * @ throws exception <br/> */<br/> Public object getstaticproperty (string classname, string fieldname) <br/> throws exception {<br/> class ownerclass = Class. forname (classname); </P> <p> Field field = ownerclass. getfield (fieldname); </P> <p> Object Property = field. get (ownerclass); </P> <p> return property; <br/>}</P> <p>/** <br/> * execute an object method <br/> * @ Param owner <br/> * object <br/> * @ Param methodname <br/> * method name <br/> * @ Param ARGs <br/> * parameter <br/> * @ Return Method return value <br/> * @ throws exception <br/> */<br/> Public object invokemethod (Object owner, string methodname, object [] ARGs) <br/> throws exception {</P> <p> class ownerclass = owner. getclass (); </P> <p> class [] argsclass = new class [args. length]; </P> <p> for (INT I = 0, j = args. length; I <j; I ++) {<br/> argsclass [I] = ARGs [I]. getclass (); <br/>}</P> <p> method = ownerclass. getmethod (methodname, argsclass); </P> <p> return method. invoke (owner, argS ); <br/>}</P> <p>/** <br/> * execute a static method <br/> * @ Param classname <br /> * class name <br/> * @ Param methodname <br/> * method name <br/> * @ Param ARGs <br/> * parameter array <br/> * @ return result returned by the execution method <br/> * @ throws exception <br/> */<br/> Public object invokestaticmethod (string classname, string methodname, <br/> object [] ARGs) throws exception {<br/> class ownerclass = Class. forname (classname); </P> <p> class [] argsclass = new class [args. length]; </P> <p> for (INT I = 0, j = args. length; I <j; I ++) {<br/> argsclass [I] = ARGs [I]. getclass (); <br/>}</P> <p> method = ownerclass. getmethod (methodname, argsclass); </P> <p> return method. invoke (null, argS ); <br/>}</P> <p>/** <br/> * Create an instance <br/> * @ Param classname <br/> * class Name <br/> * @ Param ARGs <br/> * constructor parameters <br/> * @ return the newly created instance <br/> * @ throws exception <br/> */<br/> Public object newinstance (string classname, object [] ARGs) throws exception {<br/> class newoneclass = Class. forname (classname); </P> <p> class [] argsclass = new class [args. length]; </P> <p> for (INT I = 0, j = args. length; I <j; I ++) {<br/> argsclass [I] = ARGs [I]. getclass (); <br/>}</P> <p> constructor cons = newoneclass. getconstructor (argsclass); </P> <p> return cons. newinstance (ARGs ); </P> <p >}</P> <p>/** <br/> * is an instance of a certain class? <br/> * @ Param OBJ instance <br/> * @ Param CLS class <br/> * @ return if obj is an instance of this class, returns true <br/> */<br/> Public Boolean isinstance (Object OBJ, class CLs) {<br/> return Cls. isinstance (OBJ ); <br/>}</P> <p>/** <br/> * obtain an element in the array. <br/> * @ Param array <br/> *@ param index <br/> * @ return returns the value of the index component in the specified array object <br/> */<br/> Public object getbyarray (Object array, int index) {<br/> return array. get (array, index); <br/>}< br/> 

 

 

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.