Java over reflection call method

Source: Internet
Author: User

 Public classInvoketester { PublicInvoketester () {} String str;  Publicinvoketester (String str) { This. str =str; }     Public intAddintPARAM1,intparam2) {        returnParam1 +param2; }     Publicstring Echo (String msg) {return"Echo:" +msg; }     PublicString getstr () {return"One param ctor" +str; }     Public Static voidMain (string[] args)throwsException {//Direct Fetch class//class<?> ClassType = Invoketester.class; //obtaining a class from a complete type pathclass<?> ClassType = Class.forName ("Com.top.utils.InvokeTester"); //create an object using Newinstance//Object invoketester = Classtype.newinstance (); //get an object using the default constructorObject Invoketester = Classtype.getconstructor (Newclass[]{}). newinstance (Newobject[]{}); //get the Add () method of the Invoketester classMethod Addmethod = Classtype.getmethod ("Add",Newclass[]{int.class,int.class}); //Call the Add () method on the Invoketester objectObject result = Addmethod.invoke (Invoketester,Newobject[]{NewInteger (100),NewInteger (200)});        System.out.println ((Integer) result); //get the Echo () method of the Invoketester classMethod Echomethod = Classtype.getmethod ("echo",NewClass[]{string.class}); //Call the Echo () method of the Invoketester objectresult = Echomethod.invoke (Invoketester,Newobject[]{"Hello"});        System.out.println ((String) result); //to create a class object with a parameter constructorObject InvokeTester1 = Classtype.getconstructor (NewClass[]{string.class}). newinstance (Newobject[]{NewString ("test a constructed call with parameters")}); //Get methods the same wayMethod Getstrmethod = Classtype.getmethod ("Getstr"); Object Str=Getstrmethod.invoke (InvokeTester1);    System.out.println (str); }}

In the main () method of the Invoketester class, a reflection mechanism is used to invoke the Add () and Echo () methods of the Add () method of an Invoketester object with the two arguments of the int type, which gets the expression add () The code for the method object of the methods is as follows: Method Addmethod=classtype.getmethod ("Add", New Class[]{int.class,int.class}); The Invoke (Object Obj,object args[]) method of the methods class must be an object, and if the parameter is a primitive type of data, it must be converted to an object of the corresponding wrapper type. The return value of the Invoke () method is always the object, and if the return type of the actual called method is the base type data, then the Invoke () method converts it to an object of the appropriate wrapper type and returns it. In this example, although the two parameters of the Add () method of the Invoketester class and the return value are of type int, when the Invoke () method of the Add method object is called, only the parameter of the integer type is passed, and the Invoke () The return type of the method is also an integer type, and the integer class is the wrapper class for the INT basic type: Object Result=addmethod.invoke (invoketester,new object[]{new Integer (100) , New Integer (200)}); System.out.println ((Integer) result); Result is an integer type

Java over reflection call method

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.