Java notes (7) -- Dynamic Array of reflection

Source: Internet
Author: User

/**
* Variable parameters must be considered as their corresponding array type parameters, such as show (XX... is) is called as show (XX [] is). If the Variable Parameter Element type is of the reference type:
* After JDK receives parameters internally
* The parameter is automatically removed from the package and assigned to the underlying method. Therefore, we need to package the real parameters of this array into an Object or use the actual parameters as the elements of an Object one-dimensional array before passing them.
* If the Variable Parameter Element type is basic:
* After the JDK receives the parameters internally, it does not split the package, so it does not need to be encapsulated. but it will not be wrong if it is encapsulated. therefore, we recommend that you use Object [] to encapsulate a layer regardless of the basic type or reference type to ensure that the layer is correct.
*
*/


/**
* Call Methods Using Reflection later: invoke is recommended (the underlying Object of the method, new Object [] {actual parameters required by the underlying method}). This should be said to be omnipotent.
*/


Package cn.com. java. wwh. www;



Import java. lang. reflect. Method;
Import java. util. Arrays;


/**
* @ Class: the input parameter used for exercise reflection is a dynamic array.
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 8:21:14 AM
*
*/
Class Test {
Public static void main (String [] args ){
System. out. println (Arrays. toString (args ));
}

Public static void show1 (String... args)
{
System. out. println (Arrays. toString (args ));
}
Public static void show2 (int [] args ){
System. out. println (Arrays. toString (args ));
}
}

Public class InvokeMethodDemo {

Public static void main (String [] args) throws Exception {
// Obtain the class bytecode
Class Test = Test. class;
Method method = test. getMethod ("show1", String []. class );
System. out. println (method );

// When a static method is called, the first parameter of invoke can be null. Normally, it is written
// Method. invoke (test. newInstance (), "flat boat", "22"); // error!
// Method. invoke (null, "flat boat", "22"); // error! It is similar to show1 (String s1, String s2)
// Method. invoke (null, new String [] {"", "22"}); // error!
// Method. invoke (null, new Object [] {"", "22"}); // error!
Method. invoke (null, (Object) new String [] {"", "22"}); // write the statement correctly
Method. invoke (null, new Object [] {new String [] {"", "22"}); // write the statement correctly


// ----------------- Case 2 ----------------------
Method meth = test. getMethod ("show2", int []. class );
// Meth. invoke (null, 1, 2, 3, 4); // Error
Meth. invoke (null, new int [] {1, 2, 3}); // correct
// Meth. invoke (null, new Object [] {1, 2, 3}); // error!
Meth. invoke (null, (Object) new int [] {1, 2, 3}); // correct
Meth. invoke (null, new Object [] {new int [] {1, 2, 3, 4}); // correct

// Add the main method that calls Test
Method me = test. getMethod ("main", String []. class );
Me. invoke (null, new Object [] {new String [] {"one leaf fl", "No regrets "}});

}


}

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.