Java reflection can bypass the access permission and access the private methods and members of the class. This may cause security discussions. The use of reflection helps solve many complicated problems, such as the type check during running, dynamic calling, and proxy implementation. Reflection brings us great flexibility to write programs, many functions are based on reflection.
Reflection also allows you to access private attributes of internal and anonymous internal classes.
You can use the decompilation command of java-private class name to view the complete definition of the class. (Refer to think in java)
The following is an example. First define an Interface
Java code
Public interface Ref {
Public void f ();
}
Public interface Ref {
Public void f ();
}
Interface implementation class
Java code
Public class RefImpl implements Ref {
// Interface Implementation Method
Public void f (){
System. out. println ("public method f ()");
}
Void g (String args ){
System. out. println ("package method g ():" + args );
}
Private void w (){
System. out. println ("private method w ()");
}
}
Public class RefImpl implements Ref {
// Interface Implementation Method
Public void f (){
System. out. println ("public method f ()");
}
Void g (String args ){
System. out. println ("package method g ():" + args );
}
Private void w (){
System. out. println ("private method w ()");
}
}
Test class
Java code
Public class TestRef {
Public static void main (String [] args ){
Ref ref = new RefImpl ();
System. out. println (ref. getClass (). getSimpleName (); // RefImpl type
Ref. f (); // call the Interface Method
// Ref. g (); // The method to add classes after the upward transformation cannot be called.
If (ref instanceof RefImpl ){
RefImpl ref1 = (RefImpl) ref; // transformation after Type Recognition
Ref1.g ("zhouyang ");
// Ref1.w (); // Private methods cannot be accessed
}
// Call the method through reflection
Try {
Ref ref2 = new RefImpl ();
Method m = ref2.getClass (). getDeclaredMethod ("f ");
Method m1 = ref2.getClass (). getDeclaredMethod ("g", String. class); // Method with Parameters
Method m2 = ref2.getClass (). getDeclaredMethod ("w ");
System. out. println ("================ ");
M. invoke (ref); // call method f ()
M1.invoke (ref, "yangzhou ");
M2.setAccessible (true); // a key sentence for calling the private Method
M2.invoke (ref );
} Catch (Exception e ){
E. printStackTrace ();
}
// Java javap decompilation allows you to view class information. The-private switch enables all information.
// Javap-private class name class must be compiled into a. calss File
// Use reflection to access a private member and change the value of the Private member. However, the final domain cannot be accessed.
PrivateField pf = new PrivateField ();
// Ps. ss; // Private Members cannot access
// Print the original member Value
Pf. print ();
Try {
// Reflection access and Change Original Value
Field [] f = pf. getClass (). getDeclaredFields ();
For (int I = 0; I <f. length; I ++ ){
F [I]. setAccessible (true );
System. out. println (f [I]. getType (); // print the field type.
System. out. println (f [I]. get (pf); // print the value
If ("ss". equals (f [I]. getName ())){
F [I]. set (pf, "hehe"); // modify the member Value
} Else {
F [I]. setInt (pf, 55 );
}
}
// Re-print the modified member value. The final Domain value remains unchanged.
Pf. print ();
} Catch (Exception e ){
E. printStackTrace ();
}
/* Print the output result
* RefImpl
Public method f ()
& Nb