Reflection in the Detailed JAVA based --JAVA

JAVA reflection
    JAVA reflection is in the operating state, for any class, are made known to all properties and methods of this class; for any object, call it can be any of a method; this dynamic and dynamic information obtained function call the object's method is called reflection java language.
Java reflection mechanism mainly provides the following features: Analyzing arbitrary object's class belongs at runtime; object to construct any class at runtime; Analyzing member variables and methods any class has at runtime; call arbitrary at runtime a method object; generating dynamic proxies.
1. The resulting properties of an object

. 1 public Object getProperty (Object owner, String the fieldName) throws Exception {
2 Class ownerClass owner.getClass = ();
. 3 
. 4 = ownerClass.getField Field, Field (the fieldName);
. 5 
. 6 = Object Property Field .get (owner);
. 7 
. 8 Property return;
. 9}
Class ownerClass = owner.getClass (): Class of the object obtained.

Field field = ownerClass.getField (fieldName): property class declaration obtained by Class.

Object property = field.get (owner): the attribute obtained by the object instance, if the property is a non-public, there will be reported IllegalAccessException.

2. The static properties of a class obtained

 . 1 getStaticProperty public Object (String className, String the fieldName)
 2 throws Exception {
 . 3 = Class ownerClass the Class.forName (className);
 . 4 
 . 5 = ownerClass.getField Field, Field (the fieldName);
 . 6 
 . 7 Object = Field.get Property (ownerClass);
 . 8 
 . 9 Property return;
10}

class = ownerClass the Class.forName (className): first, to obtain the class class.

Field field = ownerClass.getField (fieldName): Same as above, to give attribute class declaration by Class.

Object property = field.get (ownerClass): somewhat different from the above and here, because the property is static, taken in directly from the Class class.

3. The method of execution of an object

 Public Object the invokeMethod. 1 (Object owner, String methodName, Object [] args) throws Exception {
 2 
 . 3 = Class ownerClass owner.getClass ();
 . 4 
 . 5 Class [] = new new Class argsClass [args.length];
 . 6 
 . 7 for (int 0 = I, J = args.length; I <J; I ++) {
 . 8 argsClass [I] = args [I] .getClass ();
 . 9}
10 
. 11 Method, Method = ownerClass.getMethod (methodName, argsClass);
12 is 
13 is Method.invoke return (owner, args);
14}
Class owner_class = owner.getClass (): above all, the object must be Class.

5-9 OK: Class array configuration parameters, as looking Method conditions.

Method method = ownerClass.getMethod (methodName, argsClass ): Method to be performed to obtain the array by Method Class name and parameters.

method.invoke (owner, args): the implementation of the Method, the invoke method is to perform parameter of this method the object, and parameters of the array. The return value is Object, but also both the return value of the method.

4. Perform static methods of a class

 . 1 invokeStaticMethod public Object (String className, String methodName,
 2 Object [] args) throws Exception {
 . 3 = Class ownerClass the Class.forName (className);
 . 4 
 . 5 Class [] = new new Class argsClass [ args.length];
 . 6 
 . 7 for (int I = 0, J = args.length; I <J; I ++) {
 . 8 argsClass [I] = args [I] .getClass ();
 . 9}
10 
. 11 Method, Method = ownerClass .getMethod (methodName, argsClass);
12 is 
Method.invoke return 13 is (null, args);
} 14

the same basic principles and Example 3, is different from the last line, the Invoke parameter is null, since this is a static method, no need to run by way of example.

The new instance
 . 1 
 2 the newInstance public Object (String className, Object [] args) throws Exception {
 . 3 = Class newoneClass the Class.forName (className);
 . 4 
 . 5 Class [] = new new Class argsClass [args.length];
 . 6 
 . 7 for (int I = 0, J = args.length; I <J; I ++) {
 . 8 argsClass [I] = args [I] .getClass ();
 . 9}
10 
. 11 cons = newoneClass.getConstructor the Constructor (argsClass);
12 is 
13 is cons.newInstance return (args);
14 
15}

Where said method is a method performed with parameters constructor to create a new instance. If no parameter can be directly used newoneClass.newInstance () is achieved.

Class newoneClass = Class.forName (className): The first step, to obtain a configuration example of Class.

Lines 5 to 9: Class array parameter obtained.

Constructor cons = newoneClass.getConstructor (argsClass): obtained constructor.

cons.newInstance (args): new instance.

6. determines whether an instance of a class

. 1 isInstance public Boolean (Object obj, Class CLS) {
2 return cls.isInstance (obj);
. 3}

7. The obtained an element of the array
1 public Object getByArray (Object array, index int) {
2 return Array.get (Array, index);
. 3}

Reproduced in: https: //www.cnblogs.com/licheng/archive/2008/07/30/1256799.html

Guess you like

Origin blog.csdn.net/weixin_33921089/article/details/92633195