Details of the Java reflection mechanism example

  • 2020-04-01 02:54:23
  • OfStack

1. What is reflection?
A class has multiple components, such as member variables, methods, constructors, and so on.
Reflection is loading a class and dissecting its components.

2. The class is loaded
There is a Class Class in Java that represents the bytecode of a Class.
Since the Class Class represents the bytecode of a Class, it provides the loading of a Class
Method of bytecode: forName().    This method is used to load the bytecode of a class
Into memory and wrapped with a class object.
Two other ways to get class objects:
The name of the class. The class
Object. GetClass ()

Start by creating a simple Person class


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:



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:


//Reflection publicPerson ()
@Test
publicvoidtest1()throwsException{
Classclazz=Class.forName("RflectorDemo.Person");
Constructorcr=clazz.getConstructor(null);//Gets the constructor object
Personp=(Person)cr.newInstance(null);//Instantiate an object through a 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
Personp=(Person)cr.newInstance(newArrayList());
System.out.println(p.name);
}
//Another way to create an object is for constructors that have no arguments
@Test
publicvoidtest5()throwsException{
Classclazz=Class.forName("RflectorDemo.Person");
Personp=(Person)clazz.newInstance();
System.out.println(p.name);
}

When the constructor is private, we do brute force!!

Reflect conventional methods

In the Person class:



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:


publicclassDemo2{
//Method 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]);
}
//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"));
}
//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:



publicStringname="swr";
privateintpassword=45;
privatestaticintage=35;
 In the test class: 
Java code 

publicclassDemo3{

@Test
publicvoidtest1()throwsException
{
Personp=newPerson();
Classclazz=Class.forName("RflectorDemo.Person");
Fieldf=clazz.getField("name");
//Gets the value of the field
Objectvalue=f.get(p);
//Gets the type of the field
Classtype=f.getType();
if(type.equals(String.class)){
Stringname=(String)f.get(p);
System.out.println(name);
}
//Sets the value of the field
f.set(p,"dfafa");
System.out.println(p.name);
}

@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));
}

@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 constructor, or field is roughly the same, we can draw inferences!


Related articles: