Dark Horse programmer-Summary of the problem of calling the main method through reflection (involving variable parameters) (reflection parameters are an Array Function with caution)

Source: Internet
Author: User

 

Today, I learned one day from instructor Zhang's video. in reflection, I mentioned the problem of calling the main method through reflection. One of them is not clear.  Method mainmethod
= Class. forname (startingclassname). getmethod ("Main", string []. Class );

 Mainmethod. Invoke (null,(Object)New
String [] {"111", "222", "333 "});

This sentence (object) conversion is not easy to understand. Since the main method requires parameters of the string [] type, why cannot it be used directly? Why do we need to convert it?I have been thinking about this question for a long time. If I plan to ask a question in the forum, I suddenly realized it when I wrote this question. So I will share it with you here.
First, the parameter list of the invoke method is as follows: invoke (Object arg0, object... arg1) throws ......, This indicates that the arg1 parameter is a variable parameter. Generally, we can understand the variable parameter as follows: pass a few parameters to it and then accept several parameters,For example: (1) invoke (null, "str1", "str2"); // variable parameters receive 2 string-type parameters(Yes) (2) invoke (null, new string [] {"str1", "str2", "str3"}); // variable parameters receive 1 string [] type parameter(Error) In fact, it is not that simple. variable parameters will automatically split the array into multiple parameters. Therefore, (2) is incorrect and it should have received three string-type objects. Why?The specific explanation is as follows: Before JDK 1.5, Java had no such thing as variable parameters,Use Arrays for multiple parametersTo be passed. Version 1.5 is added later.Variable parametersIn this way, the previousProgramIt will run incorrectly and cause incompatibility issues. To be compatible with programs earlier than 1.5,Where variable parameters are accepted in a method, if an array is input, multiple parameters are automatically decomposed.(2) An input string array is automatically divided into multiple strings, so (2) can be equivalent; (2 equivalent)Invoke (null, "str1", "str2", "str3"); // split it into three string objects.
 Now, let's go back to our question. If we don't need object conversion, we will discuss the following sentence: Mainmethod. Invoke (null,New
String [] {"111", "222", "333 "});
According to the above analysis, we know that the array in this sentence will be automatically decomposed, which is equivalent Mainmethod. Invoke (null,"111", "222", "333 "); In this way, the parameter passed to the main function is 3 string strings instead of 1 String Array (string []), so an error occurs in the number of parameters. There are two solutions: 1. Add object conversion like question settings, convert the array to the object type, and the array will not be split when the value is assigned to a variable parameter. 2. load the string array into the object array. After the split, the rest is the string array, which can be passed to the main function. Mainmethod. Invoke (null, new
Object [] {
New String [] {"111", "222", "333 "}});
Http://blog.csdn.net/joyep/article/details/7078865 in reflection, if the reflected object calls a function of the array type, such as method. invoke constructor. newinstance () their parameters are all variable parameters, but they are passed as parameters during the transfer process, while the parameters of the actually executed function are an array, therefore, an exception occurs. So we are in the process. It is transmitted as a two-dimensional array. It is still a one-dimensional array after one decomposition, so you can

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.