During Android development, we found that some system methods could not be called. We know that reflection mechanisms can be used to call them. Today we have tested the Java reflection mechanism successfully. Mark it here.
Directly Add code
Package thinking. java. example_c11;
Import java. Lang. Reflect. constructor;
Import java. Lang. Reflect. invocationtargetexception;
Import java. Lang. Reflect. method;
Import java. util. arrays;
//: Showmethod. Java
// Using Java 1.1 reflection to show all
// Methods of a class, even if the methods are
// Defined in the base class
Public class showmethod {
Static final string usage =
"Usage:/N" +
"Showmethods qualified. Class. Name/N" +
"To show all methods in class or:/N" +
"Showmethods qualified. Class. Name word/N" +
"To search for methods involving 'word '";
Public static void main (string [] ARGs ){
System. Out. println (arrays. deeptostring (ARGs ));
If (ARGs. Length <1 ){
System. Out. println (usage );
System. Exit (0 );
}
Try {
Class C = Class. forname (ARGs [0]);
Class [] parametertypes = new class [1];
Parametertypes [0] = char []. Class;
Try {
Method TT = C. getmethod ("valueof", parametertypes );
Char [] pp = {'A', 'B', 'C', 'D '};
Try {
String back = (string) TT. Invoke (C. newinstance (), pp );
System. Out. println ("Reflection call method valueof (char []) returns string:" + back );
} Catch (illegalargumentexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalaccessexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (invocationtargetexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (instantiationexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
} Catch (securityexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (nosuchmethodexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Method [] M = C. getmethods ();
Constructor [] ctor = C. getconstructors ();
If (ARGs. Length = 1 ){
For (INT I = 0; I <M. length; I ++)
System. Out. println (M [I]. tostring ());
For (INT I = 0; I <ctor. length; I ++)
System. Out. println (ctor [I]. tostring ());
}
Else {
For (INT I = 0; I <M. length; I ++)
If (M [I]. tostring ()
. Indexof (ARGs [1])! =-1)
System. Out. println (M [I]. tostring ());
For (INT I = 0; I <ctor. length; I ++)
If (ctor [I]. tostring ()
. Indexof (ARGs [1])! =-1)
System. Out. println (ctor [I]. tostring ());
}
} Catch (classnotfoundexception e ){
System. Out. println ("No such class:" + E );
}
}
}
Note the following when running the example:
1. If it is not in the eclipse environment but executed on the command line, pay attention to the relationship between the compiled showmethod. Class and the execution path.
It needs to be executed in the bin directory of the Project. my options are:/work/workspace/Th-Java/bin.
My class file is located at/work/workspace/Th-Java/bin/thinking/Java/example_c11.
2. If it is executed in eclipse. Configure the java. Lang. string parameter in run configuration-> arguments.
Some running results:
[Java. Lang. String]
The reflection call method valueof (char []) returns string: ABCD
Public Boolean java. Lang. String. Equals (Java. Lang. Object)
Public java. Lang. String java. Lang. String. tostring ()
Public int java. Lang. String. hashcode ()
Public int java. Lang. String. compareto (Java. Lang. String)
Public int java. Lang. String. compareto (Java. Lang. Object)
Public int java. Lang. String. indexof (INT)
Public int java. Lang. String. indexof (INT, INT)
Public int java. Lang. String. indexof (Java. Lang. String)
...