Variable-parameter functions are invoked via Java emission mechanism

Source: Internet
Author: User

1. The function that gets the variable parameter gets the function of the variable parameter as the array type of the get parameter type. Clazz.getmethod ("test", object[]. Class);where object is a variable parameter or the data type of an arrayFor example:Public void Test (String ... params) {      ...   }The function that gets the variable parameter is as follows:Classclazz = Class. forname ("Com.demo003.MainThread");

method = Clazz.getmethod ("Test", string[].class);

   2. Execute call function with variable parameters
at this point, how can we pass multiple parameter values to a mutable parameter? Method.invoke (obj, new string[]{"A", "B", "C"}),or aMethod.invoke (Mainthread,"a" ,"B" ,"C" ); Is this called the right thing? the above two methods of invocation will be reported:Exception in thread "main"java.lang.IllegalArgumentException: Wrong number of arguments     does that feel weird? In fact, the parameter of the Invoke method is also a variable parameter, the following is the definition of Invoke     so when the parameternew string[]{"A", "B", "C"}) are automatically split into 3 parameters when passed to the Invoke method: "A", "B", "C",then the Invoke method naturally thinks that the target invocation function is with 3 parameters,so when the Invoke method invokes the target function, the 3 arguments are passed to the target function, and the target function is just a mutable parameter, so there is an inconsistency in the number of arguments. For reflection,is by passing the array type object to the variable argument of the target function,that is, it only needs to make a number of parameters into an array, and then pass the array to the mutable parameter, and then the variable parameters of the target function automatically split the array value into multiple parameter values automatically.      So how are we supposed to pass it? sinceYou know, if the array type and the parameter type of the Invoke function (object) can be converted, and is a one-dimensional array, it is automatically split into multiple parameters, resulting in an inconsistent number of final arguments and an exception. alsoknow the Reflectionis by passing the array object to the variable parameter of the target function. So also in order to mean this, you can transfer this need to pass an array type object to convert the Invoke method to an object that is not convertible split. The solution is as follows:1) Cast the data type object to an object type, so that it is not automatically split     Method.invoke ((Object) new string[]{"A", "B", "C"}) 2) Wrap the current array into a one-dimensional array, so that the split is an array objectobject[]p = new object[]{new string[]{"A", "B", "C"}}     Method.invoke (P) There may be other solutions, as long as the final reach of invoke cannot split the array parameter, or it is only the array object when the parameter is split. 





Variable-parameter functions are invoked via Java emission mechanism

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.