Obtain the array type through reflect

Source: Internet
Author: User

Kingfish 2005.3

Today I saw a question about how to obtain the array type through reflection. I tried it and can solve it through the array class.

Make a record.

The Code is as follows:

Import java. Lang. Reflect .*;

/**
* @ Author Kingfish
*
* Todo Java reflect
*/
Class mystring {
Public mystring (string s ){
System. Out. println ("in constructor of the mystring ");
}
}

Public class testreflect {
 
/**
* Constructor
* @ Param I
*/
Public testreflect (int I ){
System. Out. println ("just for test ");
}

Public void test (){

}
Public void test (mystring s ){

}
Public void test (mystring [] ARGs ){

}

Public static void main (string [] ARGs) throws exception {
Class C = Class. forname ("testreflect ");

Method M = C. getmethod ("test", new class [] {});
System. Out. println (m );

// Reflection test (mystring) Method
M = C. getmethod ("test", new class [] {mystring. Class });
System. Out. println (m );

// Reflection test (mystring) method, and the mystring class also needs to be reflected to obtain
M = C. getmethod ("test", new class [] {class. forname ("mystring ")});
System. Out. println (m );

// Reflection test (mystring []) method, which is obtained by reflection of the mystring [] Type
// Array. newinstance returns an array of the type and does not generate an instance of the class.
M = C. getmethod ("test", new class [] {array. newinstance (class. forname ("mystring"), 1). getclass ()});
System. Out. println (m );

// Obtain it by using [lclassname; ". Note: Do not forget the semicolon!
M = C. getmethod ("test", new class [] {class. forname ("[lmystring ;")});
System. Out. println (m );
}
}

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.