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.
1. Get the attributes of an object
Source code printing?
- Public object getproperty (Object owner, string fieldname) throws exception {
- Class ownerclass = owner. getclass ();
- Field field = ownerclass. getfield (fieldname );
- Object Property = field. Get (owner );
- Return property;
- }
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.
Source code printing?
- Public object getstaticproperty (string classname, string fieldname)
- Throws exception {
- Class ownerclass = Class. forname (classname );
- Field field = ownerclass. getfield (fieldname );
- Object Property = field. Get (ownerclass );
- Return property;
- }
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
Source code printing?
- Public object invokemethod (Object owner, string methodname, object [] ARGs) throws exception {
- Class ownerclass = owner. getclass ();
- Class [] argsclass = new class [args. Length];
- For (INT I = 0, j = args. length; I <j; I ++ ){
- Argsclass [I] = ARGs [I]. getclass ();
- }
- Method method = ownerclass. getmethod (methodname, argsclass );
- Return method. Invoke (owner, argS );
- }
Class owner_class = owner. getclass (): first, you must obtain the class of this object.
5 ~ Row 9: 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
Source code printing?
- Public object invokestaticmethod (string classname, string methodname,
- Object [] ARGs) throws exception {
- Class ownerclass = Class. forname (classname );
- Class [] argsclass = new class [args. Length];
- For (INT I = 0, j = args. length; I <j; I ++ ){
- Argsclass [I] = ARGs [I]. getclass ();
- }
- Method method = ownerclass. getmethod (methodname, argsclass );
- Return method. Invoke (null, argS );
- }
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
Source code printing?
- Public object newinstance (string classname, object [] ARGs) throws exception {
- Class newoneclass = Class. forname (classname );
- Class [] argsclass = new class [args. Length];
- For (INT I = 0, j = args. length; I <j; I ++ ){
- Argsclass [I] = ARGs [I]. getclass ();
- }
- Constructor cons = newoneclass. getconstructor (argsclass );
- Return cons. newinstance (ARGs );
- }
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.
5th ~ Row 9th: 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
Source code printing?
- Public Boolean isinstance (Object OBJ, class CLs ){
- Return Cls. isinstance (OBJ );
- }
7. Obtain an element in the array.
Source code printing?
- Public object getbyarray (Object array, int index ){
- Return array. Get (array, index );
- }
Complete source code:
Source code printing?
- Import java. Lang. Reflect. array;
- Import java. Lang. Reflect. constructor;
- Import java. Lang. Reflect. field;
- Import java. Lang. Reflect. method;
- /**
- * Java reflection cookbook
- *
- * @ Author Michael Lee
- * @ Since 2006-8-23
- * @ Version 0.1a
- */
- Public class reflection {
- /**
- * Obtain the public attributes of an object.
- *
- * @ Param owner, fieldname
- * @ Return this property object
- * @ Throws exception
- *
- */
- Public object getproperty (Object owner, string fieldname) throws exception {
- Class ownerclass = owner. getclass ();
- Field field = ownerclass. getfield (fieldname );
- Object Property = field. Get (owner );
- Return property;
- }
- /**
- * Obtain a static public attribute
- *
- * @ Param classname Class Name
- * @ Param fieldname: attribute name
- * @ Return this property object
- * @ Throws exception
- */
- Public object getstaticproperty (string classname, string fieldname)
- Throws exception {
- Class ownerclass = Class. forname (classname );
- Field field = ownerclass. getfield (fieldname );
- Object Property = field. Get (ownerclass );
- Return property;
- }
- /**
- * Execute an object Method
- *
- * @ Param owner
- * Object
- * @ Param methodname
- * Method name
- * @ Param ARGs
- * Parameters
- * @ Return method Return Value
- * @ Throws exception
- */
- Public object invokemethod (Object owner, string methodname, object [] ARGs)
- Throws exception {
- Class ownerclass = owner. getclass ();
- Class [] argsclass = new class [args. Length];
- For (INT I = 0, j = args. length; I <j; I ++ ){
- Argsclass [I] = ARGs [I]. getclass ();
- }
- Method method = ownerclass. getmethod (methodname, argsclass );
- Return method. Invoke (owner, argS );
- }
- /**
- * Execute some static methods.
- *
- * @ Param classname
- * Class Name
- * @ Param methodname
- * Method name
- * @ Param ARGs
- * Parameter Array
- * @ Return the result returned by the execution Method
- * @ Throws exception
- */
- Public object invokestaticmethod (string classname, string methodname,
- Object [] ARGs) throws exception {
- Class ownerclass = Class. forname (classname );
- Class [] argsclass = new class [args. Length];
- For (INT I = 0, j = args. length; I <j; I ++ ){
- Argsclass [I] = ARGs [I]. getclass ();
- }
- Method method = ownerclass. getmethod (methodname, argsclass );
- Return method. Invoke (null, argS );
- }
- /**
- * Create an instance
- *
- * @ Param classname
- * Class Name
- * @ Param ARGs
- * Constructor Parameters
- * @ Return the newly created instance
- * @ Throws exception
- */
- Public object newinstance (string classname, object [] ARGs) throws exception {
- Class newoneclass = Class. forname (classname );
- Class [] argsclass = new class [args. Length];
- For (INT I = 0, j = args. length; I <j; I ++ ){
- Argsclass [I] = ARGs [I]. getclass ();
- }
- Constructor cons = newoneclass. getconstructor (argsclass );
- Return cons. newinstance (ARGs );
- }
- /**
- * Is it a class instance?
- * @ Param OBJ instance
- * @ Param CLS class
- * @ Return if obj is an instance of this type, true is returned.
- */
- Public Boolean isinstance (Object OBJ, class CLs ){
- Return Cls. isinstance (OBJ );
- }
- /**
- * Obtain an element in the array.
- * @ Param Array
- * @ Param Index
- * @ Return returns the value of the index component in the specified array object.
- */
- Public object getbyarray (Object array, int index ){
- Return array. Get (array, index );
- }
- }