Java runs the Invoke method by reflecting the method name, and the name string must match exactly and case-sensitive
Method meth =cla.getmethod ("SetStr1", New Class[]{java.lang.string.class}); New class parameter type collection
Object Result=meth.invoke (object, new object[]{"Test ..."}); The new object is the value of the input parameter type and corresponds to the number above.
import java.lang.reflect.field;import java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;import java.lang.reflect.type;import java.util.iterator;public class TestMe { private String str1; private String str2; private String str3; public void method1 () { system.out.println (" Testme--------Method1 method: "); &NBSP;}&NBSP;PUBLIC&NBSP;VOID&NBSP;METHOD2 () { system.out.println ("TestMe--------Method2 Method:"); } public void method3 () { system.out.println ("TestMe--------Method3 Method:"); &NBSP;}&NBSP;PUBLIC&NBSP;VOID&NBSP;SETSTR1 (STRING&NBSP;STR1) { this.str1 = str1; system.out.println (STR1); &NBSP;}&NBSP;PUBLIC&NBSP;STRING&NBSP;GETSTR1 () { return &NBSP;STR1;&NBSP;}&NBSP;PUBLIC&NBSP;VOID&NBSP;SETSTR2 (STRING&NBSP;STR2) { this.str2 = str2;&nbsP;} &NBSP;PUBLIC&NBSP;STRING&NBSP;GETSTR2 () { return str2; } public void SETSTR3 (STRING&NBSP;STR3) { this.str3 = str3; } public String GETSTR3 () { return str3; } public static void main (String[] args) { test (); } private static void test () { class cla = testme.class; try { object object = cla.newinstance (); method[] methods = cla.getmethods (); Method method; for (int i = 0; i < methods.length; i++) { method = methods[i]; The return value type of the System.out.println (Method.getname ()); // method type returntype = method.geTreturntype (); system.out.println ("Method return value type" + returntype); The parameter type of the // method type[] types = method.getgenericparametertypes (); for (int j = 0; j < types.length; j++) { Type type = types[j]; System.out.println ("Method parameter type type: " + j + "----" + type); } } field[] fileds = cla.getdeclaredfields (); Field field; for (int i = 0; i < fileds.length; i++) { field = fileds[i]; System.out.println ("---------------------------"); system.out.println (Field.gettype ()); system.out.priNtln (Field.getname ()); } method meth = Cla.getmethod ("SetStr1", new class[]{java.lang.string.class}); object result= Meth.invoke (object, new object[]{"Test ..."}); } catch (instantiationexception e) { e.printstacktrace (); } catch (illegalaccessexception e) { e.printstacktrace (); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace (); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block E.printstacktrace (); } catch (illegalargumentexception e) { // todo auto-generated catch bLock e.printstacktrace (); } catch (invocationtargetexception e) { // TODO Auto-generated catch block E.printstacktrace (); } }}
Operation Result:
Main
method return value type void
Method parameter type type:0----class [Ljava.lang.String;
Method1
method return value type void
Method2
method return value type void
Method3
method return value type void
SetStr1
method return value type void
Method parameter Type type:0----Class java.lang.String
GetStr1
Method return value type class java.lang.String
SetStr2
method return value type void
Method parameter Type type:0----Class java.lang.String
GetStr2
Method return value type class java.lang.String
SetStr3
method return value type void
Method parameter Type type:0----Class java.lang.String
GetStr3
Method return value type class java.lang.String
Wait
method return value type void
Wait
method return value type void
Method parameter type type:0----long
Method parameter type type:1----int
Wait
method return value type void
Method parameter type type:0----long
Hashcode
method return value type int
GetClass
Method return value type Class Java.lang.Class
equals
method returns a value type of Boolean
Method parameter Type type:0----Class Java.lang.Object
Tostring
Method return value type class java.lang.String
Notify
method return value type void
Notifyall
method return value type void
---------------------------
Class Java.lang.String
Str1
---------------------------
Class Java.lang.String
str2
---------------------------
Class Java.lang.String
Str3
Test ...
Java Reflection Mechanism test