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.