preface

Reflection is more commonly used in real projects than Java proxies, so learning reflection is not only to understand the principles of framework design, but also to improve programming skills in daily work development

use

To use Java reflection, the first thing you need to get is the Java Class object, which is very important. To use reflection, you need to get the corresponding Class object, because only by getting the Class object can you get the instance of the Class, static methods, constructors, non-static methods, etc.

So how do you get a Class object? What are the ways to get a Class object?

There are four ways to get a Class object:

  • GetClass ()), and get the Class object from the object instance as follows:
Person person = new Person();
Class personClass = person.class
Copy the code

And that doesn’t work very well, because typically reflection is used to get the class object of the class and get the instance of the class, the method of the class, if you already know the Person class.

  • Class: The class object is obtained by the class, as follows
Class personClass = Person.class
Copy the code

And this is just like the one where you have to use it if you know the object, and this one if you know the class.

  • Get from the path passed to Class class.forname () :
Class testClass = Class.forName("com.xx.Test");
Copy the code

This is the most common, if you know the classpath

  • Obtained from the class loader xxxclassLoader.loadclass ()
Class testClass = ClassLoader.loadClass("com.xx.Test");
Copy the code

There are four types of class loaders: start class loaders, extend class loaders, application class loaders, and custom class loaders. The premise of this method is the same as the previous one, which is to know the classpath first

Some operations on reflection

So once we get the Class object, what can we get from the Class object? Suppose we’ve got a Class object in one of the above four ways.

  • You can get an object instance of the class
Class testClass = ClassLoader.loadClass("com.xx.Test");
Test testObject = (Test)testClass.newInstance();
Copy the code
  • You can get all defined methods of a class
Class testClass = ClassLoader.loadClass("com.xx.Test");
Method[] methods = testClass.getDeclaredMethods();
Copy the code

You can even get a specified method and call it, get a private method, and get a constructor

  • You can get attributes of the class such as fields
Class testClass = ClassLoader.loadClass("com.xx.Test");
Field field = testClass.getDeclaredField("name");
Copy the code

The principle of

When it comes to reflection, we talk about Class objects, Class objects, so what is a Class object, and how does it relate to the Class defined in our code and the instance generated?

We all know that Java is a compiled language, compiled and then run, so there is a compile time and a run time.

The first stage of a Java Class’s life cycle is loading, and the third thing a virtual machine does is generate a java.lang.Class object for that Class. Classes are loaded at run time, so we say JAVA reflection is in run state. So some people think that Class objects can get all kinds of methods and properties magically, but in fact they are Class files generated by classes in Java code, so they can get all kinds of information about classes.

For a better understanding of the original ideal of reflection, see this article: Java Reflection Principles – Important