01

concept

Reflection is one of the features of Java. It is a mechanism for indirectly manipulating target objects. The core is that the JVM dynamically loads classes at runtime, and for any class, it can know all the properties and methods of that class, call the methods/access the properties, without knowing the running object in advance at compile time. It allows a running Java program to retrieve information about a class and manipulate properties inside a class or object.

The type of object in the program are generally fixed at compile time, and when our program at runtime, you may need to dynamic load some classes, these classes because in less than before, so there is no loaded into the JVM, then, using the Java reflection mechanism can be created dynamically at run-time object and call its properties, it is according to the need to load at runtime.

02

use

Just knowing the concept doesn’t make it any easier to understand what reflection is, right? For what purposes is reflection used? What situations do we use it in development?

Generally reflection is used as a framework, to do some low-level code with a high degree of abstraction.

Reflection is the soul of frame design.

What are the uses of reflection?

1, the first is decompile, decompile is.class–>.java

Access Java object properties, methods, constructors, etc

3. When we use an IDE such as Ecplise, when we enter an object or class and want to call its properties and methods, the compiler automatically lists its properties and methods as soon as we press the dot.

4. The most important use of reflection is the development of common frameworks.

03

The anatomy of the class

We know that a class typically has constructors, methods, and member variables (fields/attributes).

The Class object provides the following common methods:

public Constructor getConstructor(Class<? >… parameterTypes)

public Method getMethod(String name,Class<? >… parameterTypes)

public Field getField(String name)

public Constructor getDeclaredConstructor(Class<? >… parameterTypes)

public Method getDeclaredMethod(String name,Class<? >… parameterTypes)

public Field getDeclaredField(String name)

These methods are used to help us dissect constructors, methods, and member variables (attributes) from the class, respectively.

Then the dissected parts are represented by Constructor, Method and Field objects respectively.

The first thing to understand is that the code we write is stored in a.java file, but it will be compiled, and the compiled.class file will actually execute.

Java is an object-oriented language, everything is an object, so Java thinks that these compiled class files, this kind of thing is also an object, and it’s abstracted into a class, and that class is called class, and you can look at that class in AIP.

You can look at the API

04

Commonly used class

Java.lang.Class;

Java.lang.reflect.Constructor;

Java.lang.reflect.Field;

Java.lang.reflect.Method;

Java.lang.reflect.Modifier;

05

The instance

1. Methods in reflection classes

Add a Person class and add methods for several parameters.

package com.cj.test; import java.util.Date; Public class Person {public Person(){system.out.println (" default no-argument constructor executes "); } public Person(String name){system.out.println (" name :"+name); } public Person(String name,int age){ System.out.println(name+"="+age); } private Person(int age){system.out.println (" age :"+age); } public void m1() { System.out.println("m1"); } public void m2(String name) { System.out.println(name); } public String m3(String name,int age) { System.out.println(name+":"+age); return "aaa"; } private void m4(Date d) { System.out.println(d); } public static void m5() { System.out.println("m5"); } public static void m6(String[] strs) { System.out.println(strs.length); } public static void main(String[] args) { System.out.println("main"); }}Copy the code

Let’s look at the call.

package com.cj.test; import java.lang.reflect.Method; import java.util.Date; import org.junit.Test; public class Test1 { @Test//public void m1() public void test1() throws Exception{ Class clazz = Class.forName("com.cj.test.Person"); Person p = (Person)clazz.newInstance(); Method m = clazz.getMethod("m1", null); m.invoke(p, null); } @Test//public void m2(String name) public void test2() throws Exception{ Class clazz = Person.class; Person p = (Person) clazz.newInstance(); Method m = clazz.getMethod("m2", String.class); M. invoke(P, "Procedural Workplace "); } @Test//public String m3(String name,int age) public void test3() throws Exception{ Class clazz = Person.class; Person p = (Person) clazz.newInstance(); Method m = clazz.getMethod("m3", String.class,int.class); String returnValue = (String).invoke(p, "program career ",23); System.out.println(returnValue); } @Test//private void m4(Date d) public void test4() throws Exception{ Class clazz = Person.class; Person p = (Person) clazz.newInstance(); Method m = clazz.getDeclaredMethod("m4", Date.class); m.setAccessible(true); m.invoke(p,new Date()); } @Test//public static void m5() public void test5() throws Exception{ Class clazz = Person.class; Method m = clazz.getMethod("m5", null); m.invoke(null,null); } @Test//private static void m6(String[] strs) public void test6() throws Exception{ Class clazz = Person.class; Method m = clazz.getDeclaredMethod("m6",String[].class); m.setAccessible(true); m.invoke(null,(Object)new String[]{"a","b"}); } @Test public void test7() throws Exception{ Class clazz = Person.class; Method m = clazz.getMethod("main",String[].class); m.invoke(null,new Object[]{new String[]{"a","b"}}); }}Copy the code

2. Attribute fields in reflection classes

package com.cj.test; import java.util.Date; Public class Person {public String name=" "; private int age = 18; public static Date time; public int getAge() { return age; } public Person(){system.out.println (" default no-argument constructor executed "); } public Person(String name){system.out.println (" name :"+name); } public Person(String name,int age){ System.out.println(name+"="+age); } private Person(int age){system.out.println (" age :"+age); } public void m1() { System.out.println("m1"); } public void m2(String name) { System.out.println(name); } public String m3(String name,int age) { System.out.println(name+":"+age); return "aaa"; } private void m4(Date d) { System.out.println(d); } public static void m5() { System.out.println("m5"); } public static void m6(String[] strs) { System.out.println(strs.length); } public static void main(String[] args) { System.out.println("main"); }}Copy the code

Result call.

package com.cj.test; import java.lang.reflect.Field; import java.util.Date; import org.junit.Test; Public class Demo3 {//public String name=" Demo3 "; @Test public void test1() throws Exception{ Class clazz = Person.class; Person p = (Person)clazz.newInstance(); Field f = clazz.getField("name"); String s = (String)f.get(p); System.out.println(s); // change the value of name f.et (p, "wang liu "); // change the value of name f.et (p," Wang liu "); System.out.println(p.name); } @Test//private int age = 18; public void test2() throws Exception{ Class clazz = Person.class; Person p = (Person)clazz.newInstance(); Field f = clazz.getDeclaredField("age"); f.setAccessible(true); int age = (Integer)f.get(p); System.out.println(age); f.set(p, 28); age = (Integer)f.get(p); System.out.println(age); } @Test//public static Date time; public void test3() throws Exception{ Class clazz = Person.class; Field f = clazz.getField("time"); f.set(null, new Date()); System.out.println(Person.time); }}Copy the code

3. Other uses of reflection methods – to get past generic checking by reflection

import java.lang.reflect.Method; import java.util.ArrayList; For example: If I have a set of String generics, how can I add a value of type Integer to the set? */ public class Demo { public static void main(String[] args) throws Exception{ ArrayList<String> strList = new ArrayList<>(); strList.add("aaa"); strList.add("bbb"); // strList.add(100); Class listClass = strList.getClass(); // Get the Class object of ArrayList, call add() in the reverse direction, add data Class listClass = strlist.getClass (); M = listClass.getMethod("add", object.class); m = listClass.getMethod("add", Object. // call the add() method m.invoke(strList, 100); For (Object obj: strList){system.out.println (obj); }}}Copy the code

Say so much, hurriedly try, in the project use more convenient.

Github (Code cloud Gitee synchronization)Github.com/ProceduralZ…Welcome star.

Original is not easy, useful “attention”, wechat search [programmer cartoon programming] attention to me. If it helps you, triple company. Thanks for your support. Note: If there are any problems with the article, please kindly correct them.