Citation:
In the Java project, we listened to a lot of ORM concept, I have been brooding, how to convert from Rs to object it? Do you have to write a lot of judgments? The answer is definitely no, we are going to explore how to solve this problem, just in the study of our system underlying architecture, the excavation of this point, and now share with you:
In- depth:
The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods; This dynamically acquired and dynamically invoked method of the object is called the reflection mechanism of the Java language.
The Java Reflection mechanism provides the following functions: To determine the class to which any object belongs at run time, to construct an object of any class at run time, to determine the member variables and methods that any class has at run time, to invoke a method of any object at run time, and to generate a dynamic proxy.
<span style= "FONT-SIZE:18PX;" >package COM.JAVA.JVM; Import java.lang.reflect.*; Class Invoketest {public static void main (string[] args) {try {Class C = class.forname ("Com.java.jvm.User"); Object o = c.newinstance (); Method m = C.getmethod ("GetName", null); Object ret = M.invoke (o, null); System.out.println ("com.java.jvm.User.getName () =" + ret); m = C.getmethod ("SetName", New Class[]{string.class}); ret = M.invoke (o, New object[]{"Shengjian"}); System.out.println ("com.java.jvm.User.setName () =" + ret); m = C.getmethod ("GetName", null); ret = M.invoke (o, null); System.out.println ("com.java.jvm.User.getName () =" + ret); The invocation of the static method M = C.getmethod ("GetUserName", New Class[]{string.class}); RET = M.invoke (null, "Fuwang"); System.out.println ("com.java.jvm.User.getUserName () =" + ret); } catch (ClassNotFoundException ex) {System.out.println ("This category not Found")} catch (Nosuchmethodexception ex) { System.out.println ("This method does not exist"); } catch (Illegalaccessexception ex) {System.ouT.PRINTLN ("Call this method without permission"); } catch (InvocationTargetException ex) {System.out.println ("The following exception occurred when calling this method: \ n" + ex.gettargetexception ());} catch ( IllegalArgumentException e) {e.printstacktrace ();} catch (Instantiationexception e) {e.printstacktrace ();}}} </span>
C.getmethod ("GetName", NULL) returns a method object that reflects the specified public member methods of the class or interface represented by this class object. The name parameter is a string that specifies the short name of the method you want. The Parametertypes parameter is an array of Class objects that identify the type of the method parameter in the order in which they are declared. If parametertypes is null, it is processed by an empty array. Name-method name Parametertypes-parameter list method provides information about a single method (and how to access the method) on a class or interface. The method that is reflected may be a class method or an instance method, including an abstract method.
Invoke ()
Invokes the underlying method represented by this method object for the specified object with the specified argument. Individual parameters are automatically unpacked to match the basic parameters, and both the basic and reference parameters are subject to the method invocation transformation. If the underlying method is static, the specified obj parameter can be ignored. The parameter can be null. If the underlying method requires a shape parameter of 0, the supplied args array can be 0 or null in length. If the underlying method is an instance method, it is called using the dynamic method lookup, which is recorded in the 15.12.4.4 section of Java Language specification, Second Edition, and should be done in the case of overrides based on the runtime type of the target object. If the underlying method is static and the class declaring the method has not been initialized, it will be initialized. If the method completes normally, the value returned by the method is returned to the caller, and if the value is a base type, it is first wrapped appropriately in the object. However, if the type of the value is a set of basic types, the array element is not wrapped in the object, in other words, an array of the base type is returned. If the underlying method return type is void, the call returns NULL.
Obj-the object from which the underlying method is called args-the parameter used for the method invocation.
User code:
<span style= "FONT-SIZE:18PX;" >package COM.JAVA.JVM; public class User {static{System.out.println ("Static---"); Public User () {System.out.println ("name=" +name); Name= "Xiaotian"; } private String name; Private Integer age; Private String address; public static string GetUserName (string name) {return name; } public String GetName () {return name; } public void SetName (String name) {this.name = name; } public Integer Getage () {return age; public void Setage (Integer age) {this.age = age; } public String getaddress () {return address; The public void setaddress (String address) {this.address = address; } @Override Public String toString () {return ' User [name= + name + ', age= "+ Age +", address= "+ address + "]"; }}</span>
Summary:
In order to some realization and tossing and turning, night can not sleep, one day we found that the original all this is so simple, Java, the structure of the province and the bottom, with endless beauty guide us to explore and discover! In this process, we solve the Java ajar of the door, to see the endless scenery inside!
Programming is wonderful, sometimes, needs us extensively, and sometimes needs us to go deep!
Java Project accumulation--java reflection invoke