Construction java reflection reflective object and method invocation

Java reflection technology is widely applied, which can be arranged: the fully qualified name of the class, method and parameters to complete initialization of an object, provided is a reflective certain methods. Java can be enhanced configurability.

1.1 Construction of the object (without parameters) by reflection:

  For example, we use this example to explain the class ReflectServiceImpl

1 public class ReflectServiceImpl {
2     public void sayHello(String name){
3         System.out.println("hello"+name);
4     }
5 }

We reflected by a method to build it.

1 public ReflectServiceImpl getInstance(){
2         ReflectServiceImpl object=null;
3         try {
4             object=(ReflectServiceImpl)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();
5         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException  e) {
6             e.printStackTrace();
7         }
8         return object;
9 }

其中第4行:object=(ReflectServiceImpl)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();

Class loader is to sign up for a class ReflectServiceImpl naming rights, after initializing a class object by newInstance method.

1.2 Construction of the object (class constructor with parameters) by reflection:

We use this class to understand ReflectServiceImpl2:

1 public class ReflectServiceImpl2 {
2     private String name;
3     public ReflectServiceImpl2(String name) {
4         this.name=name;
5     }
6     public void sayHello(String name){
7         System.out.println("hello"+name);
8     }
9 }

At this time, with a constructor parameter ReflectServiceImpl2  public ReflectServiceImpl2 (String name) {} XXXX;

At this time, how do we use reflection to generate object? Only need to register in the class loader uses getConstructor (parameter) method. Where the parameter is the type of argument we constructor's. code show as below:

 1 public ReflectServiceImpl2 getInstance(){
 2         ReflectServiceImpl2 object=null;
 3             try {
 4            object=(ReflectServiceImpl2)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
 5                    .getConstructor(String.class).newInstance("张三");
 6         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | InvocationTargetException |NoSuchMethodException e) {
 7             e.printStackTrace();
 8         }
 9         return object;
10 }

As shown in row 4 and 5:

  To be loaded by the class loader forName. Then getConstructor method, it may be a plurality of parameters, String.class defined herein, it means there is only one argument of type String construction method. This method can be carried out by the method of exclusion of the same name, this time again newInstance method to generate the object, but also more than a newInstance method parameter "Joe Smith" only. In fact, equivalent = new new Object  ReflectServiceImpl2 ( "Joe Smith"). Just use to generate the object reflector only.

1.3 Reflection Method

  We need to get the object before using the reflection method, the method to be able to get a reflection.

We use ReflectServiceImpl class example. 

ReflectServiceImpl ClassCode:

1 public class ReflectServiceImpl {
2  public void sayHello(String name){
3         System.out.println("hello"+name);
4     }
5 }

Call the method:

. 1   public   Object the reflect () {
 2          ReflectServiceImpl Object = null ;
 . 3          Object returnObj = null ;
 . 4          // reflective generating object 
. 5          the try {
 . 6              Object = (ReflectServiceImpl) the Class.forName ( "com.lean.ssm.chapter2.reflect.ReflectServiceImpl " ) .newInstance ();
 . 7              // reflective generation method and schedule 
. 8              method, method object.getClass = () getMethod (." the sayHello ", String. class );
 . 9              returnObj = Method.invoke (Object," John Doe " ) ;
 10          } the catch( ClassNotFoundException| NoSuchMethodException| InvocationTargetException|  IllegalAccessException|  InstantiationException e ) {
11             e.printStackTrace();
12         }
13         return returnObj;
14 }

When the specific target object (type ReflectServiceImpl), without knowing exactly which class, may be used object.getClass () getMethod ( "the sayHello", String.. Class ); to replace it, wherein the first parameter is the name of the method, the second parameter is the parameter type, is a list of multiple parameters can continue to write more than one type, so that we can get the reflection method object. When using reflection methods method.invoke (object, "Joe Smith" ); invoked, the first argument for the object, the object is to determine which method call, and "Joe Smith" is the parameter, which is equivalent to object.sayHello ( "Joe Smith"); if there is a plurality of parameters can be written Method.invoke (target, obj1, obj2.obj3 ... ), the method to be determined according to the specific object.

1.4 Test

To ReflectServiceImpl example:

. 1  Package com.lean.reflect;
 2  Import java.lang.reflect.InvocationTargetException;
 . 3  Import the java.lang.reflect.Method;
 . 4  public  class ReflectServiceImpl {
 . 5      // property 
. 6      Private String name;
 . 7      // default constructor 
8      public ReflectServiceImpl () {
 . 9          Super ();
 10      }
 . 11      // constructor arguments 
12 is      public ReflectServiceImpl (String name) {
 13 is          the this .name = name;
 14     }
15     //方法 sayHelo
16     public void sayHello(String name){
17         System.out.println("hello "+name);
18     }
19     //调用方法
20     public  Object reflect(){
21         ReflectServiceImpl object=null;
22         Object returnObj=null;
23         //反射生成对象
24         try {
25             object = (ReflectServiceImpl) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();
26             //反射生成方法并调度
27             Method method = object.getClass().getMethod("sayHello", String.class);
28             returnObj= method.invoke(object, "张三");
29         }catch( ClassNotFoundException| NoSuchMethodException| InvocationTargetException|  IllegalAccessException|  InstantiationException e ) {
30             e.printStackTrace();
31         }
32         return returnObj;
33     }
34     //获取对象
35     public ReflectServiceImpl getInstance(){
36         ReflectServiceImpl object=null;
37         try {
38             object=(ReflectServiceImpl)Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();
39         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException  e) {
40             e.printStackTrace();
41         }
42         return object;
43     }
44     //测试
45     public static void main(String[] args) {
46             ReflectServiceImpl rsl= new ReflectServiceImpl();
47             rsl.reflect();
48     }
49 }

Renderings:

Guess you like

Origin www.cnblogs.com/zyxsblogs/p/11225233.html