Reflection is the dynamic execution API provided by Java, which can dynamically load classes, dynamically create objects, dynamically access properties, and invoke methods dynamically.
# # Static execution and dynamic execution
Static execution: After compiling, the sequence of program execution is determined and executed in accordance with the established order.
Such as:
Foo foo = new Foo ();
int n = foo.times;
SYSTEM.OUT.PRINTLN (n);
Dynamic execution: During "Run Time" to determine the load that class, create that object, Execute method ...
Dynamic execution can be achieved using the reflection API.
### Dynamic Loading class
Api
Class CLS = Class.forName ("class name");
The following two methods can get CLS objects, but they do not reflect dynamic attributes
Class cls = Foo.class;
Class cls = Foo.getclass ();
Case:
Scanner in = new Scanner (system.in);
System.out.print ("Enter class Name:");
String Classname=in.nextline ();
Class cls = Class.forName (className);
SYSTEM.OUT.PRINTLN (CLS);
Principle:
### dynamically Create objects
During runtime, objects are created based on any class name.
Api
Object obj = cls.newinstance ()
> Tip: Create an object using the parameterless constructor of the class, and the reflected constructor API can invoke the parameter builder to create the object.
Give me a chestnut:
Scanner in = new Scanner (system.in);
System.out.print ("Enter class Name:");
String Classname=in.nextline ();
Class cls = Class.forName (className);
SYSTEM.OUT.PRINTLN (CLS);
Object obj = cls.newinstance ();
System.out.println (obj);
### Dynamic Access Properties
Use steps:
1. Locate the property declaration information field based on the property name
-Field Fld=cls.getdeclaredfield (property name)//age Property declaration
2. The value of the property on the object, find/Create an object
3. When the object gets the value of the property
-Object val = value of Fld.get (object)//age
Case:
Scanner in = new Scanner (system.in);
System.out.print ("Enter class Name:");
String Classname=in.nextline ();
Class cls = Class.forName (className);
SYSTEM.OUT.PRINTLN (CLS);
Object obj = cls.newinstance ();
System.out.println (obj);
Find Property Declaration Information
System.out.print ("Property name:");
String name = In.nextline ();
Dynamically Find property declaration information
Field fld = Cls.getdeclaredfield (name);
System.out.println (FLD);
To read an object's properties: Read the value of a property on an object
Open access rights
Fld.setaccessible (TRUE);
Object val = fld.get (obj);
System.out.println (Val);
The ### reflection API can "foresee"
Classic Interview Questions:
Eclipse's Hotkey menu, with what technology is implemented.
### reflection can dynamically get information about a class
Reflect all of the properties:
Field[] Getdeclaredfields ()
Reflect all of the properties:
Method[] Getdeclaredmethods ()
Case:
Scanner in = new Scanner (system.in);
System.out.print ("Enter class Name:");
String Classname=in.nextline ();
Class cls = Class.forName (className);
All properties of Reflection class
Field[] Fields=
Cls.getdeclaredfields ();
for (Field field:fields) {
System.out.println (field);
}
All methods of Reflection class
Method[] Methods=
Cls.getdeclaredmethods ();
For (method Method:methods) {
System.out.println (method);
}
### reflection can access private properties and methods!!
Use before accessing private members
E.setaccessible (TRUE);
You can turn on access rights.
Case:
Open access rights
Fld.setaccessible (TRUE);
Object val = fld.get (obj);
System.out.println (Val);
Interview topic:
1. Spring If you inject a value to a private property
2. If you access private members of a class
1. Find the method of "declaration" information Methods
-Methods Getdeclaredmethod (method name, parameter type ...)
-Methods Getdeclaredmethod (method name)
2. Create or Get objects
3. Invoke the response method on the Object!
-Object Method.invoke (objects, Parameters ...)
Object v = M1.invoke (object);
Object v = M2.invoke (object, 5, "666");
Case:
Scanner in = new Scanner (system.in);
Dynamic Load Class
System.out.print ("Enter class Name:");
String Classname=in.nextline ();
Class cls = Class.forName (className);
SYSTEM.OUT.PRINTLN (CLS);
# # The bottom of the frame is basically reflective technology.
# # annotation is to use reflection to parse!
Principle:
Case:
@Retention (Retentionpolicy.runtime)
Public @interface Test {
}
public class TestCase {
@Test
public void Demo () {
System.out.println ("demo");
}
@Test
public void Test () {
SYSTEM.OUT.PRINTLN ("test");
}
}
public class Demo03 {
public static void Main (string[] args)
Throws exception{
JUnit 4 Prototypes
Scanner in = new Scanner (system.in);
System.out.print ("Class name:");
String className = In.nextline ();
Class is a reflection of the portal
Class cls = Class.forName (className);
Find all methods in a class
Method[] Methods =
Cls.getdeclaredmethods ();
Object obj = cls.newinstance ();
For (method Method:methods) {
Dynamic parsing annotations
Object A = method
. Getannotation (Test.class);
System.out.println (a);
if (a!=null) {
Method.invoke (obj);
}
}
}
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