The problem of java reflection calling private method using setAccessible (true)

as mentioned, a Person class is defined to have a private method

public Person {
    private void test();//private
}

use reflection to call
start with the problematic method

Constructor con= Person.class.getConstructor();//
Object object = con.newInstance();//
//
Person.class.getDeclareMethod("test").setAccessible(true);
Person.class.getDeclareMethod("test").invoke(object);//
/*Person.class.getDeclareMethod("test").isAccessible()false*/

but you can use the following way to write

Method = Person.class.getDeclareMethod("test");
method.setAccessible(true);
method.invoke(object);//
/*method.isAccessible()true
Person.class.getDeclareMethod("test").isAccessible()false
*/

is this a problem with the Person.class.getDeclareMethod ("test") method, which also occurs when reflection calls constructors? they all have an @ CallerSensitive annotation. Is that the reason? Hope for an answer.

Mar.08,2022

each acquisition method does not get the same Method object
setAccessable only acts on the resulting method object, nor is it a global
, so the first method will report an error

.

in addition, the properties of setAccessable are not included in equals and hashCode of Method

.
        Constructor con = People.class.getConstructor();//
        Object object = con.newInstance();//
        Method test1 = People.class.getDeclaredMethod("test");
        test1.setAccessible(true);

        Method test2 = People.class.getDeclaredMethod("test");
        
        System.out.println(test1.hashCode());//hashcode
        System.out.println(test2.hashCode());//hashcode

        test1.invoke(object);//

the explanation I can think of is that the references to test1 and test2 are not the same, but equals and hashcode are the same, which confuses me. But the explanation should still come to me, but that way, they can be quoted differently

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5defd-4db07.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5defd-4db07.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?