1. What is reflection?
A class has several components, such as member variables, methods, construction methods, and so on.
Reflection is the loading class and the anatomy of each component of the class.
2. Load Class
In Java, there is an classes class that represents the byte code of a class.
Since class classes represent the bytecode of a class, it is necessary to provide a class that loads
Byte-code method: Forname (). This method is used to load the byte code of a class
into memory and encapsulates it with a class object.
Another 2 ways to get class objects:
Class name. class
Object. GetClass ()
Create a simple person class first
Copy Code code as follows:
public class Reflectdemo {
public static void Main (String args[]) throws Exception
{//1.
Class clazz = Class.forName ("DSA.") Person ");
2.
Class clazz1 = new Person (). GetClass ();
3.
Classclazz2=person.class;
}
}
3. Reflection Construction Method
In the person class:
Copy Code code as follows:
/**
* Construction Method
*/
Publicperson () {
SYSTEM.OUT.PRINTLN ("null");
}
Publicperson (Stringname) {
SYSTEM.OUT.PRINTLN (name);
}
Publicperson (stringname,intpwd) {
System.out.println (name+ "+" +pwd);
}
Privateperson (listlist) {
System.out.println ("List");
}
In the test class:
Copy Code code as follows:
Reflective Publicperson ()
@Test
Publicvoidtest1 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
Constructorcr=clazz.getconstructor (null);//Get Constructor Object
personp= (person) cr.newinstance (null);//Instantiate object by constructor
System.out.println (P.name);
}
Reflective Publicperson (Stringname)
@Test
Publicvoidtest2 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
Constructorcr=clazz.getconstructor (String.class);
personp= (person) cr.newinstance ("haha");
System.out.println (P.name);
}
Reflective Publicperson (STRINGNAME,INTPWD)
@Test
Publicvoidtest3 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
Constructorcr=clazz.getconstructor (String.class,int.class);
personp= (person) cr.newinstance ("haha", 1);
System.out.println (P.name);
}
Reflective Publicperson (listlist)
@Test
Publicvoidtest4 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
Constructorcr=clazz.getdeclaredconstructor (List.class);
Cr.setaccessible (TRUE);//brute force cracking
personp= (person) cr.newinstance (Newarraylist ());
System.out.println (P.name);
}
Another way to create an object that works only with the parameterless construction method
@Test
PUBLICVOIDTEST5 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
personp= (person) clazz.newinstance ();
System.out.println (P.name);
}
When the construction method is private, we do brute force cracking!!!
4. Normal Reflection method
In the person class:
Copy Code code as follows:
/**
* Method
*/
PUBLICVOIDJF ()
{
}
PUBLICVOIDJF (STRINGNAME,INTPWD)
{
}
PUBLICCLASS[]JF (STRINGNAME,INT[]PWD)
{
Returnnewclass[]{string.class,int.class};
}
PRIVATEVOIDJF (Inputstreamin)
{
System.out.println (in);
}
PUBLICSTATICVOIDJF (Intnum)
{
SYSTEM.OUT.PRINTLN (num);
}
Publicstaticvoidmain (stringargs[])
{
SYSTEM.OUT.PRINTLN ("main!!!");
}
In the test class:
Copy Code code as follows:
/**
* Method of Reflection Class
*
* @authortanlvxu
*
*/
publicclassdemo2{
Methods of Reflection Class: PUBLICVOIDJF ()
@Test
Publicvoidtest1 () throwsexception{
Personp=newperson ();
Classclazz=class.forname ("Rflectordemo.person");
Methodmethod=clazz.getmethod ("JF", NULL);
Method.invoke (P,null);
}
Method of Reflection Class: Publicvoidjf (STRINGNAME,INTPWD)
@Test
Publicvoidtest2 () throwsexception{
Personp=newperson ();
Classclazz=class.forname ("Rflectordemo.person");
Methodmethod=clazz.getmethod ("JF", String.class,int.class);
Method.invoke (P, "DSA", 30);
}
Method of Reflection Class: Publicclass[]jf (STRINGNAME,INT[]PWD)
@Test
Publicvoidtest3 () throwsexception{
Personp=newperson ();
Classclazz=class.forname ("Rflectordemo.person");
Methodmethod=clazz.getmethod ("JF", String.class,int[].class);
Classcs[]= (class[]) Method.invoke (p, "Aads", newint[]{1,2,3});
System.out.println (Cs[0]);
}
Method of Reflection Class: Privatevoidjf (Inputstreamin)
@Test
Publicvoidtest4 () throwsexception{
Personp=newperson ();
Classclazz=class.forname ("Rflectordemo.person");
Methodmethod=clazz.getdeclaredmethod ("JF", Inputstream.class);
Method.setaccessible (TRUE);
Method.invoke (P,newfileinputstream ("D:\\qqclient.txt"));
}
Method of Reflection Class: Publicstaticvoidjf (Intnum)
@Test
PUBLICVOIDTEST5 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
Methodmethod=clazz.getmethod ("JF", Int.class);
Method.invoke (null,30);
}
Method of Reflection Class: Publicstaticvoidm (Intnum)
@Test
Publicvoidtest6 () throwsexception{
Classclazz=class.forname ("Rflectordemo.person");
Methodmethod=clazz.getmethod ("main", String[].class);
Method.invoke (null, (Object) newstring[]{"DS", "Das"});
Method.invoke (null,newobject[]{newstring[]{"DS", "Das"}});
}
5. Reflection Field
In the person class:
Copy Code code as follows:
/**
* Field
*/
Publicstringname= "SWR";
privateintpassword=45;
privatestaticintage=35;
In the test class:
Java code
/**
* The fields of the reflection class
* @authortanlvxu
*
*/
publicclassdemo3{
/**
* Reflection Field Publicstringname= "SWR";
* @throwsException
*/
@Test
Publicvoidtest1 () throwsexception
{
Personp=newperson ();
Classclazz=class.forname ("Rflectordemo.person");
Fieldf=clazz.getfield ("name");
Get the value of a field
Objectvalue=f.get (P);
Get the type of the field
Classtype=f.gettype ();
if (Type.equals (String.class)) {
Stringname= (String) F.get (p);
SYSTEM.OUT.PRINTLN (name);
}
Set the value of a field
F.set (P, "Dfafa");
System.out.println (P.name);
}
/**
* Reflection field Privateintpassword;
* @throwsException
*/
@Test
Publicvoidtest2 () throwsexception
{
Personp=newperson ();
Classclazz=class.forname ("Rflectordemo.person");
Fieldf=clazz.getdeclaredfield ("password");
F.setaccessible (TRUE);
F.set (p,36);
System.out.println (F.get (p));
}
/**
* Reflection field privatestaticintage=35;
* @throwsException
*/
@Test
Publicvoidtest3 () throwsexception
{
Classclazz=class.forname ("Rflectordemo.person");
Fieldf=clazz.getdeclaredfield ("Age");
F.setaccessible (TRUE);
F.set (null,24);
SYSTEM.OUT.PRINTLN (F.get (null));
}
In fact, regardless of the reflection construction method, or the fields are roughly the same, we can extrapolate!