1.What is reflection?
A class has multiple components, such as member variables, methods, constructors, etc.
Reflection is to load the class and dissect the various components of the class.
2. Loading the class
There is a Class class in Java that represents the bytecode of a certain class.
Since the Class class represents the bytecode of a certain class, it must provide loading of a certain class.
Method of bytecode: forName(). This method is used to load the bytecode of a certain class.
into memory and encapsulate using class objects.
Another two ways to get class objects:
Class name.class
Object.getClass()
Create a simple Person class first
The code copy is 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 Person class:
The code copy is 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:
The code copy is as follows:
//Reflection publicPerson()
@Test
publicvoidtest1()throwsException{
Classclazz=Class.forName("RflectorDemo.Person");
Constructorcr=clazz.getConstructor(null);//Get the constructor object
Personp=(Person)cr.newInstance(null);//Instantiate the object through the constructor
//System.out.println(p.name);
}
//Reflection 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);
}
//Reflection 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);
}
//Reflection 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 objects is only applicable to constructing methods without parameters
@Test
publicvoidtest5()throwsException{
Classclazz=Class.forName("RflectorDemo.Person");
Personp=(Person)clazz.newInstance();
System.out.println(p.name);
}
When the construction method is private, we will brute-force crack! ! !
4. Conventional methods of reflection
In Person class:
The code copy is 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:
The code copy is as follows:
/**
*Reflective method
*
*@authortanlvxu
*
*/
publicclassDemo2{
//Reflection class method: publicvoidjf()
@Test
publicvoidtest1()throwsException{
Personp=newPerson();
Classclazz=Class.forName("RflectorDemo.Person");
Methodmethod=clazz.getMethod("jf",null);
method.invoke(p,null);
}
//Reflection class method: 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);
}
//Reflection class method: 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]);
}
//Reflection class method: 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"));
}
//Reflection class method: publicstaticvoidjf(intnum)
@Test
publicvoidtest5()throwsException{
Classclazz=Class.forName("RflectorDemo.Person");
Methodmethod=clazz.getMethod("jf",int.class);
method.invoke(null,30);
}
//Reflection class method: 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 fields
In PerSon class:
The code copy is as follows:
/**
*Field
*/
publicStringname="swr";
privateintpassword=45;
privatestaticintage=35;
In the test class:
Java code
/**
*Reflective class fields
*@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 the field
Objectvalue=f.get(p);
//Get the field type
Classtype=f.getType();
if(type.equals(String.class)){
Stringname=(String)f.get(p);
System.out.println(name);
}
//Set the value of the 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, no matter the reflection construction method or the fields, we can learn from it one by one!