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 );
}
}