Java invoke (go to pick)

Source: Internet
Author: User
Tags instance method reflection

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.

Package COM.JAVA.JVM;Import java.lang.reflect.*;Class Invoketest {PublicStaticvoid 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",NewClass[]{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 call to the static method M = C.getmethod ("GetUserName",NewClass[]{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 ( "does not have permission to call this method");} 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 ();}}     
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

The method provides information about a single method on a class or interface (and how to access it). 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:

Package COM.JAVA.JVM;PublicClass 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 StaticStringGetUserName(String name) {return name;PublicStringGetName(){return name;Public void SetName(String name){THIS.name = name;}PublicIntegerGetage(){return age;}Public void Setage(Integer Age){This.age = age;}PublicStringGetAddress(){return address;}Public void setaddress(String address) {this.address = address;} @Overridepublic String toString() {return ' User [name= + name + ', age= ' + age + ", address=" + address+ "]";}}             

Java invoke (go to)

Related Article

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.