JAVA reflective learning

preface

Recently, I've been looking at the knowledge points related to android plug-in. I still feel a little weak about some basic knowledge, so I learn from some knowledge points. This blog is to record and review the knowledge points. Part of the article is based on online videos and blogs. If there are mistakes, I hope you can correct them and learn together for common progress.

What is reflection?

The importance of reflection goes without saying. In a word, it is to encapsulate the components of a class into other objects.

Three phases of Java code:

For example, we have a book java

public class Book {

    private String name;

    private int price;

    private int page;

    public String color;

    protected int realPrice;
    
    public String getColor() {
        return color;
    }

    public int getRealPrice() {
        return realPrice;
    }

    public void setRealPrice(int realPrice) {
        this.realPrice = realPrice;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public Book() {
    }

    public Book(String name, int price, int page) {
        this.name = name;
        this.price = price;
        this.page = page;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", page=" + page +
                '}';
    }


}

It is clearly explained in the figure that each component of a class (member variable, construction Method, member Method) is encapsulated into other objects (Fields, Constructor, Method)

Benefits:

  1. Decoupling improves the flexibility and expansibility of the program.
  2. You can manipulate objects while the program is running.

Disadvantages:

  1. Efficiency issues
  2. Security restrictions

Three ways to get Class

Source code stage: class Forname ("full class name") loads bytecode into the memory control

Class object stage: class name Class is obtained by class name

Runtime phase: objects getClass();

Conclusion: the same bytecode file will get the same class object no matter which way it is in the process of running a program.

        Class book1 = Class.forName("com.example.lib.Book");
        System.out.println(book1.getName());

        Class book2 = Book.class;
        System.out.println(book2.getName());

        Class book3 = new Book().getClass();
        System.out.println(book3.getName());

        System.out.println("===============");
        System.out.println(book1 == book2);
        System.out.println(book1 == book3);

Output result:

com.example.lib.Book
com.example.lib.Book
com.example.lib.Book
===============
true
true

Class object

There are many methods in the class object, which are basically get methods. There are many basic methods. The following describes some basic uses.

1 get member variables

getField()
getFields();
getDeclaredField()
getDeclaredFields()

2 get member method

getMethod()
getMethods()
getDeclaredMethod()
getDeclaredMethods()

3 get construction method

getConstructor()
getConstructors();
getDeclaredConstructor()
getDeclaredConstructors()

verification:

        System.out.println("===============");
        System.out.println(book1.getField("color"));
        System.out.println(book1.getDeclaredField("name"));
        System.out.println(Arrays.toString(book1.getFields()));
        System.out.println(Arrays.toString(book1.getDeclaredFields()));


        System.out.println("===============");
        System.out.println(book1.getMethod("getColor"));
        System.out.println(Arrays.toString(book1.getMethods()));
        System.out.println(Arrays.toString(book1.getMethods()));
        System.out.println(Arrays.toString(book1.getDeclaredMethods()));

        System.out.println("===============");
        System.out.println(book1.getConstructor());
        System.out.println(Arrays.toString(book1.getConstructors()));
        System.out.println(book1.getDeclaredConstructor());
        System.out.println(Arrays.toString(book1.getDeclaredConstructors()));
===============
public java.lang.String com.example.lib.Book.color
private java.lang.String com.example.lib.Book.name
[public java.lang.String com.example.lib.Book.color]
[private java.lang.String com.example.lib.Book.name, private int com.example.lib.Book.price, private int com.example.lib.Book.page, public java.lang.String com.example.lib.Book.color, protected int com.example.lib.Book.realPrice]
===============
public java.lang.String com.example.lib.Book.getColor()
[public java.lang.String com.example.lib.Book.toString(), public java.lang.String com.example.lib.Book.getName(), public void com.example.lib.Book.setName(java.lang.String), public void com.example.lib.Book.setColor(java.lang.String), public int com.example.lib.Book.getPrice(), public int com.example.lib.Book.getPage(), public void com.example.lib.Book.setPage(int), public void com.example.lib.Book.setPrice(int), public int com.example.lib.Book.getRealPrice(), public void com.example.lib.Book.setRealPrice(int), public java.lang.String com.example.lib.Book.getColor(), public final void java.lang.Object.wait() throws java.lang.InterruptedException, 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 boolean java.lang.Object.equals(java.lang.Object), 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()]
[public java.lang.String com.example.lib.Book.toString(), public java.lang.String com.example.lib.Book.getName(), public void com.example.lib.Book.setName(java.lang.String), public void com.example.lib.Book.setColor(java.lang.String), public int com.example.lib.Book.getPrice(), public int com.example.lib.Book.getPage(), public void com.example.lib.Book.setPage(int), public void com.example.lib.Book.setPrice(int), public int com.example.lib.Book.getRealPrice(), public void com.example.lib.Book.setRealPrice(int), public java.lang.String com.example.lib.Book.getColor(), public final void java.lang.Object.wait() throws java.lang.InterruptedException, 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 boolean java.lang.Object.equals(java.lang.Object), 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()]
[public java.lang.String com.example.lib.Book.toString(), public java.lang.String com.example.lib.Book.getName(), public void com.example.lib.Book.setName(java.lang.String), public void com.example.lib.Book.setColor(java.lang.String), public int com.example.lib.Book.getPrice(), public int com.example.lib.Book.getPage(), public void com.example.lib.Book.setPage(int), public void com.example.lib.Book.setPrice(int), public int com.example.lib.Book.getRealPrice(), public void com.example.lib.Book.setRealPrice(int), public java.lang.String com.example.lib.Book.getColor()]
===============
public com.example.lib.Book()
[public com.example.lib.Book(), public com.example.lib.Book(java.lang.String,int,int)]
public com.example.lib.Book()
[public com.example.lib.Book(), public com.example.lib.Book(java.lang.String,int,int)]

Summary: Field is the member variable, Constructor is the construction Method, Method is the Method, and with s is to return an array; Those with Declared can only get the attribute members of the class itself (including private, shared and protected). Those without Declared can only get the public attribute members of the class (and its parent class). The verification results are as follows. You can try it yourself.

Modify values by reflection

        Field color = book1.getField("color");
        Book book=new Book();
        color.set(book,"12346");
        System.out.println(book.color);

        // Print results:
        ===============
        12346

Here is only an example of a writing construction method. You can practice other API s yourself.

Write at the end

A good memory is not as good as a bad pen. To learn a knowledge, you need to write code manually to deepen your impression. Don't practice because it's simple. It's not simple to do simple things well!

 

Keywords: Java reflection

Added by rpieszak on Fri, 28 Jan 2022 05:55:40 +0200