Field usage and instructions for Java reflection

Field usage and instructions for Java reflection

1. What is reflection?

The purpose of reflection is to obtain the structure of the object (including member variables, methods, constructors, etc.) during runtime, and to access these properties and methods if permitted. The advantage
of reflection is obvious, that is, it can dynamically obtain instances of classes at runtime, improving flexibility; it can be combined with dynamic compilation. However, using reflection will make the program performance lower because it needs to parse the bytecode and the objects in the memory.

Java reflection api:
Field class: A class in the Java.lang.reflec package, which represents the member variables of the class and can be used to get and set attribute values ​​in the class.
Method class: A class in the Java.lang.reflec package, which represents the method of the class and can be used to obtain method information or execute methods in the class.
Constructor class: A class in the Java.lang.reflec package, representing the constructor method of the class.

Field

一切反射相关的代码都从获得类对象开始。
 1) 先获取类对象,获取类对象后才可以获取到 Field 属性。
 获取类对象的3种获取方式:        
         1、类名.class;  // Class usr = User.class;
         2、对象名.getClass();   //Class usr= user.getClass();
         3、Class.forName(全限定名/全路径名)  //Class usr= Class.forName("com.reflect.entity.User");
         
 2) 反射机制实例化对象
		User user= (User) usr.newInstance();   
		
  此外:     
         获取全限定名:  usr.getName();            
         获取类名:      usr.getSimpleName();   
         获取访问修饰符: usr.getModifiers();   
         获取包名 :      usr.getPackage();      

Get attributes:

 1)获取单个公共的属性 【非私有化】成员变量
	   Field field = user.getClass().getField("age"); //获取类或接口的指定的已声明字段
	   Field field = usr.getField("age");
	   
 2)获取单个公共的、私有的、最终的等等属性 【包括私有化】成员变量【暴力反射】 
		//属性的访问修饰符1:代表public  2:代表private  0:代表没有修饰符
	   Field field = user.getClass().getDeclaredField("age");
	   Field field = usr.getDeclaredField("age");
		
 3)获取所有公共的属性【只有非私有化的】【暴力反射】
	   Field[] fields = usr.getFields();	//然后foreach
		
 4)获取所有公共的、私有的、受保护的、最终的等等属性 【包括私有化】成员变量【暴力反射】 
		Field[] fields = usr.getDeclaredFields();  

 5)获取K类父类
         Class clazz = req.getClass().getSuperClass();
      
 6)获取属性名:field.getName();    
                    
 7)获取属性类型: filed.getType();           
          
 8)获取属性访问修饰符 :field.getModifiers();    

How to get it:

1)获取单个公共的、私有的、最终的等等方法
        Method method = sysUser.getClass().getDeclaredMethod("setName", String.class);
        method.invoke(sysUser, "花和尚");//调用
        System.err.println(sysUser.getName());//打印结果:花和尚
	       
2)获取所有公共的方法
	Method[] methods = user.getClass().getMethods();	//foreach
		
3)获取所有公共的、私有的、受保护的、最终的等等方法
	Method[] methods = user.getClass().getDeclaredMethods();

Manipulate properties through the reflection API :

        SysUser u4 = sysUser.getClass().newInstance();
        Field f = sysUser.getClass().getDeclaredField("name");
        f.setAccessible(true);//表示不用不需要做安全检查了,可以直接访问。
        f.set(u4, "hhhh");//给哪个对象设置属性,通过反射直接写属性,这里不能访问私有属性会报错
        System.out.println(u4.getName());//通过反射直接读属性
        System.out.println(f.get(u4));//把f的值 获取到
        
打印结果:
hhhh
hhhh

Guess you like

Origin blog.csdn.net/Little_Arya/article/details/129188745