This is the 27th day of my participation in the More Text Challenge. For more details, see more text Challenge

Remember to write a json entity before the article

Remember before you wrote an article on the turn json entity, it USES Java reflection, but just to reflect this knowledge at the time, and there is no understanding, winter vacation be free and at leisure top further up the knowledge of the Java reflection, it is personal learning reflection, less than, still hope the great god give directions!!!!!!

Reflection, as the name suggests, is the opposite of Java compilation, where the effect is to instantiate and manipulate class objects using the class name. Specific look at the

Get the superclass and all the interfaces

  • First of all, our Java classes can inherit and implement multiple interfaces, so how do we get the superclass and interface of a Java class through Java reflection?

  • First we create a new Annimal here as the parent class

public class Annimal { private String AnnimalName; public void eat(){ System.out.println("Annimal is eatting"); } public void run(){ System.out.println("Annimal is running"); }}Copy the code
  • Then create a new Behaviour interface
public interface Behaviour {

	public String haviour="test";
	
	public void sayHello();
	
	public void isAngury();
	
	public void love();
}
Copy the code
  • Now let’s create a new Dog class and have the Dog class inherit from Annimal and implement Behavour
public class Dog extends Annimal implements Behaviour

Copy the code
  • Dog source
public class Dog extends Annimal implements Behaviour{ private String dogName; public void fuck(String name){ System.out.println("fuck To : "+name); } @Override public void sayHello() { // TODO Auto-generated method stub System.out.println("Dog sayHello"); } @Override public void isAngury() { // TODO Auto-generated method stub System.out.println("Dog isAngury"); } @Override public void love() { // TODO Auto-generated method stub System.out.println("Dog love"); }}Copy the code
  • test

  • Let’s start by getting the Class object from the Class Class

Class<? > target=Class.forName("tom.change.Reflect. Gets the superclass and all interfaces. Dog");Copy the code
  • You can then get the superclass and all the interfaces through the getSuperClass and getInterfaces methods
// Get Class<? > parent=target.getSuperclass(); Class<? Class<? Class<? Class<? > intefaces[]=target.getInterfaces();Copy the code
  • Finally, see the effect of the program running

Get all properties (getDeclaredFields)

  • There are class-to-class relationships in Java classes, and each class has specific properties. How do we use reflection to get properties in Java classes? So I’m going to write a user class that has a property of

Dog class:

	private String dogName;
Copy the code
  • So let’s see if we can get some of these properties. Class objects are also obtained through the classpath
Class<? > clazz=Class.forName("tom.change.Reflect. Gets the superclass and all interfaces. Dog");Copy the code
  • And then through the class object getDeclaredFields method of obtaining local attributes of a class, this method is unable to get to inherit properties of parent classes and interfaces, pass the class if you don’t want to get to get to the parent class, and then to obtain the parent class object, attribute in to get the parent class.
for (int i = 0; i < fields.length; System.out.println(modifiers (fields[I].getModifiers()))+"@@@"+fields[I].getName()); }Copy the code
  • This is the getModifiers class that gets the properties of this property, private, public or protected.

  • Once you get the Field, you can set the property using the set method of the Field, regardless of the external state of the property. The prerequisite is that the permission is enabled

field.setAccessible(true);
field.set(object, "21131084");
Copy the code
  • Here, object is an instantiated object of class

Get all the methods in a class (getMethods)

  • We’ve got all the properties in the class, so we’re going to get all the methods in the class. We’ve already done the same thing, so I won’t go over it here, but the difference is that after we get the class object we call getMethods.
public void tom.change.Reflect. Gets the superclass and all interfaces. Dog.fuck(java.lang.String) public void tom.change.Reflect. Gets the superclass and all interfaces. Dog.sayHello() public void tom.change.Reflect. Gets the superclass and all interfaces. Dog.love() public void tom.change.Reflect. Gets the superclass and all interfaces. Dog.isAngury() public void tom.change.Reflect. Gets the superclass and all interfaces. Annimal.run() public void tom.change.Reflect. Gets the superclass and all interfaces. Annimal.eat() public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public final void java.lang.Object.wait() throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll()Copy the code
  • If you’re wondering why some of these methods appear, some of them don’t even appear in our Dog and inheritance classes, what’s going on. First of all getMethods gets the methods of the class itself and that’s for sure. Inherited classes are also available to us, which explains why we have methods from class Annimal in our fetching methods. All classes inherit from the Object class. All classes inherit from the Object class.

Calling a method in a class (getMethod)

  • GetMethod returns a Method object. Then we invoke the specified Method using the invoke Method. We need to pass in the class instantiated object when we call it, and we need to pass in the arguments, if any.
public Object invoke(Object obj, Object... args)
Copy the code
  • The source code shows that you can pass in as many arguments as the method needs, but this is generic.

  • To get a Method using getMethod we need to pass in the type of the Method parameter

``` public Object invoke(Object obj, Object... args) ``` ``` public Method getMethod(String name, Class<? >... parameterTypes) ```Copy the code

Get all constructors (getConstructors)

  • The last thing we should have left in the Java class is the constructor. Since it is a reflection, we should be able to reflect the class in a crystal clear way. After obtaining the class object we get the Constructor using getConstructors, which returns the Constructor collection. GetParameterTypes gets the parameters in the Constructor class. This allows us to construct the class ourselves.

conclusion

  • At this point our reflection is fully capable of manipulating a class. Some people ask what’s the point of this, in our Javaweb, which well-known framework source must have reflection, through reflection we can implement the annotation function. Of course, annotations have other knowledge, later know in the introduction. Let’s say we’re mapping a map to an entity where we need the reflection area to get the properties in the Java Bean and assign the value of the map. In any case, reflection is an important part of our Java knowledge, and learning it will help you a lot in future architecture.

Download the source code