Home >Java >javaTutorial >What are the knowledge points about Java reflection mechanism?
java source code----->javac-------------->java bytecode file----- --------->java----------------->Class object (memory space: metaspace, local memory)------ ------------------new--------->Instantiated object----------------- -gc------------->Unload object
##Class objects can be obtained at different stagespackage com.reflect; public class TestReflectPerson { public static void main(String[] args) throws ClassNotFoundException { //1.class.forName() Class class1=Class.forName("com.reflect.Person"); System.out.println(class1); //2.类名.class Class class2=Person.class; System.out.println(class2); //2.对象名.getClass() Class class3=new Person().getClass(); System.out.println(class3); System.out.println(class1==class2); //true System.out.println(class2==class3); //true } }Function of class objectGet member variables: Get all: class object.getDeclaredFields(), get one: class object.getDeclaredField()
//获得构造方法对象, Constructor cons1 = pcla.getDeclaredConstructor(String.class, int.class); Person p2 = (Person)cons1.newInstance("李四",19); System.out.println("p2:"+p2.getName());newInstance() If you create a parameterless constructor to create an object, you can Use class objects to create objects, skip getting the constructor objectGetGet the name of the class: getName() Print out the full name: class name package name only Want to print a separate class name: getSimpleName()Get the member variable name of the class Properties file: The content is connected with an equal sign, such as k=v, Code example:
package com.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class TestReflectPerson { public static void main(String[] args) throws Exception { /* //1.class.forName() Class class1=Class.forName("com.reflect.Person"); System.out.println(class1); //2.类名.class Class class2=Person.class; System.out.println(class2); //2.类名.getClass() Class class3=new Person().getClass(); System.out.println(class3); System.out.println(class1==class2); System.out.println(class2==class3);*/ //获取对象 Class tclass=Class.forName("com.reflect.Person"); //通过类对象获取成员变量们 Field[] fields = tclass.getDeclaredFields(); System.out.println("获取Person对象的所有属性对象"); for (Field field:fields){ System.out.println(field); } //指定获取Person对象的属性对象 System.out.println("指定获取Person对象的属性对象"); Field age=tclass.getDeclaredField("age"); System.out.println("age:"+age); //通过类对象获取所有的构造方法 Constructor[] constructors = tclass.getDeclaredConstructors(); System.out.println("获取Person的所有构造方法对象"); for (Constructor constructor:constructors){ System.out.println(constructor); } //通过类对象获取无参的构造方法 Constructor constructor = tclass.getDeclaredConstructor(); System.out.println("constructor:"+constructor); //通过类对象获取有参的构造方法 Constructor constructor1 = tclass.getDeclaredConstructor(String.class,int.class); System.out.println("constructor1:"+constructor1); //通过类对象获取所有的成员方法 Method[] methods = tclass.getDeclaredMethods(); for (Method method:methods){ System.out.println("method:"+method); } //通过类对象获取getAge成员方法 Method getAge = tclass.getDeclaredMethod("getAge"); System.out.println("getAge:"+getAge); //通过类对象获取getAge成员方法 Method setAge = tclass.getDeclaredMethod("setAge", int.class); System.out.println("setAge:"+setAge); } }Get member variable code example:
package com.reflect; import java.lang.reflect.Field; public class TestField { public static void main(String[] args) throws Exception { Class pcla=Person.class; /*//获取公共访问权限的成员变量 Field[] fields = pcla.getFields(); for (Field field:fields){ System.out.println("getFild:"+field); } System.out.println(); //获取所有访问权限的成员变量 Field[] fielddes = pcla.getDeclaredFields(); for (Field field:fielddes){ System.out.println("field:"+field); }*/ Field name = pcla.getDeclaredField("name"); System.out.println(name); Person person=new Person(); //暴力反射:获取任意访问权限修饰符的安全检查 name.setAccessible(true); //获取公共成员变量的值 Object value = name.get(person); System.out.println(value); //获取任意访问权限的成员变量的值 Object value2 = name.get(person); System.out.println("value2:"+value2); //设置任意访问权限的成员变量的值 name.set(person,"张三"); Object value3=name.get(person); System.out.println("name:"+value3); } }How to get the value of a private variable
//暴力反射:获取任意访问权限修饰符的安全检查 name.setAccessible(true);
The above is the detailed content of What are the knowledge points about Java reflection mechanism?. For more information, please follow other related articles on the PHP Chinese website!