This article describes the usage of Java programming reflection mechanism. Share it for your reference, as follows:
Preface: Reflection: Dynamically obtain the class (bytecode file such as Person.class in this article) and run its members. Reflection may be slightly less encountered in the development of Android application layer, but for those who want to get through the underlying layer, they must be proficient in using it.
Entity Class
Person.java
package com.sunwenou.reflect;// package name public class Person { private String name; private int age; public Person() {// No parameter} public Person(String name, int age) {// with parameter super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return name+","+age; } public void show() { // empty parameter System.out.println("show"); } public void fun(String ss) { // with parameter System.out.println(ss); } public static void function() { // Static System.out.println("static"); }}Methods to dynamically obtain bytecode files
In order to make the blog post look concise, an Exception is thrown and the import is omitted, the same below.
package com.sunwenou.reflect;public class Demo1 { /** * Method for dynamically obtaining bytecode files* 1: Use the Class getClass() method provided by the Object class* This method requires objects* 2: Each data type has a static class attribute, and the data type returned by this attribute belongs to the bytecode file object* int.class Person.class * 3: Use the forName() method provided by Class* Only a string is needed, which consists of the package name + class name*/ public static void main(String[] args) throws Exception { //getClaz(); //getCalz2(); getClaz3(); } //Use the forName() method provided by Class public static void getClaz3() throws Exception { Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); Class<?> claz2 = Class.forName("com.sunwenou.reflect.Person"); System.out.println(claz==claz2); } //Each data type has a static class attribute public static void getCalz2() { Class<Person> p1 = Person.class; Class<Person> p2 = Person.class; System.out.println(p1==p2); } //Use the Class getClass() method provided by the Object class public static void getClaz() { Person person1 = new Person(); Class<? extends Person> claz = person1.getClass();//Person.class Person person2 = new Person(); Class<? extends Person> claz2 = person2.getClass();//Person.class System.out.println(claz==claz2); }}Dynamically get the class and create the object
package com.sunwenou.reflect;public class Demo2 { public static void main(String[] args) throws Exception { //createObj(); createObj2(); } public static void createObj2() throws Exception { //Person person = new Person("lisi",23); //Get the bytecode file object Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); //Person.class //Get the object of type Constructor to which the constructor method with parameters belongs. Constructor constructor = claz.getConstructor(String.class,int.class); //Create the object using the method of creating an object provided by the Constructor class Person person = (Person)constructor.newInstance("lisi",23); System.out.println(person); } public static void createObj() throws Exception { //Person person = new Person(); //Get the bytecode file object Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); Object obj = claz.newInstance();//By default, use the constructor of empty parameters to create the object System.out.println(obj); }}Dynamically get the class and assign values to member variables
package com.sunwenou.reflect;public class Demo3 { public static void main(String[] args) throws Exception { //Person p = new Person(); //p.name = "lisi"; //get the bytecode file object Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); //get the object of Field type to which the member variable belongs//Field field = claz.getField("name"); //get the member with public permission Field field = claz.getDeclaredField("name");//Get all declared fields System.out.println(field); //Non-static member variables are Object dependent on the object obj = claz.newInstance(); field.setAccessible(true);//Brute force cracking, set to accessible field.set(obj, "Zhang San"); System.out.println(obj); }}Dynamically get the class and execute the method
package com.sunwenou.reflect;public class Demo4 { public static void main(String[] args) throws Exception { //method1(); method2(); method3(); } public static void method3() throws Exception { //Get the bytecode file object Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); Method m = claz.getMethod("function", null); m.invoke(null, null); } ////Execute the method with parameters public static void method2() throws Exception { //Get the bytecode file object Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); Method m = claz.getMethod("fun", String.class); Object obj = claz.newInstance(); m.invoke(obj, "hello"); } //Execute the method without parameters public static void method1() throws Exception { //Person person = new Person(); person.show(); //Get the bytecode file object Class<?> claz = Class.forName("com.sunwenou.reflect.Person"); //Get the bytecode file object to which the executed method belongs. Method m = claz.getMethod("show", null); //Non-static methods are Object dependent on the object obj = claz.newInstance(); //Execute method m.invoke(obj, null); }}This is the basic usage of reflection. We can create objects through the object's bytecode file when we cannot create objects normally and execute them. Have you learned how to do it? ?
For more information about Java related content, please check out the topics of this site: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.