Differences between Java Reflection getXXX and getDeclaredXXX

Keywords: Java

I mentioned it in my class today, Reflex.In order to further strengthen the basic principles of learning, take the initiative to understand the reflection.Write a Demo with someone else, some of which are not clear. Write it down here.

package com.ctgu.reflect;
//Test Class
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class test08 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("com.ctgu.reflect.User");


        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());

        //Get the properties of the class
        Field[] fields = c1.getFields();
        for (Field field : fields) {
            System.out.println("**"+field);
        }


        fields = c1.getDeclaredFields();

        for (Field field : fields) {
            System.out.println("@@"+field);
        }

        Field name = c1.getDeclaredField("name");
        System.out.println(name);
        Method[] declaredMethods = c1.getMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("##"+declaredMethod);
        }
        declaredMethods = c1.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("Natural"+declaredMethod);
        }

        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("!!"+constructor);
        }
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("&&"+declaredConstructor);
        }

        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println("Develop Constructors"+declaredConstructor);
    }
}

User class

public class User{
    private String name;
    private int id;
    public int age;

    public User() {
    }

    public User(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

Output Results

com.ctgu.reflect.User
User
**public int com.ctgu.reflect.User.age
@@private java.lang.String com.ctgu.reflect.User.name
@@private int com.ctgu.reflect.User.id
@@public int com.ctgu.reflect.User.age
private java.lang.String com.ctgu.reflect.User.name
##public java.lang.String com.ctgu.reflect.User.toString()
##public java.lang.String com.ctgu.reflect.User.getName()
##public int com.ctgu.reflect.User.getId()
##public void com.ctgu.reflect.User.setName(java.lang.String)
##public void com.ctgu.reflect.User.setAge(int)
##public void com.ctgu.reflect.User.setId(int)
##public int com.ctgu.reflect.User.getAge()
##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()
//Naturalpublic java.lang.String com.ctgu.reflect.User.toString()
//Naturalpublic java.lang.String com.ctgu.reflect.User.getName()
//Naturalpublic int com.ctgu.reflect.User.getId()
//Naturalpublic void com.ctgu.reflect.User.setName(java.lang.String)
//Naturalpublic void com.ctgu.reflect.User.setAge(int)
//Naturalpublic void com.ctgu.reflect.User.setId(int)
//Naturalpublic int com.ctgu.reflect.User.getAge()
!!public com.ctgu.reflect.User()
!!public com.ctgu.reflect.User(java.lang.String,int,int)
&&public com.ctgu.reflect.User()
&&public com.ctgu.reflect.User(java.lang.String,int,int)
//Develop Constructorspublic com.ctgu.reflect.User(java.lang.String,int,int)

Confused about the difference between getFields and getDeclaredFields, I found out
getFields(): Gets all the public fields of a class, including those in the parent class.
getDeclaredFields(): Gets all the declared fields of a class, that is, public, private, and proteced, but not the declared fields of the parent class.
This also solves the puzzle why I initially set all three properties of User to private and getFields to print empty results.
Similarly:
getDeclaredMethod: Method to get all declarations of the current class, including public, protected, and private ly modified methods.It is important to note that these methods must be declared in the current class, that inheritance from the parent class does not count, and that methods that implement interfaces are included because they are declared.
getMethod: A method that gets all the public s of the current class and the parent class.The parent class here refers to all the parent classes in the inheritance hierarchy.For example, if A inherits B and B inherits C, then both B and C belong to the parent class of A.
···········································································································
getDeclaredConstructors() returns all types of constructors, including public and non-public
getConstructors() returns only public.

Two original articles have been published. Approved 0. Visited 70
Private letter follow

Posted by dawson1323 on Tue, 25 Feb 2020 18:28:19 -0800