Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason behind setAccessible method of AccessibleObject class have a boolean parameter?

I am very new in Reflection and I have a doubt like:

public void setAccessible(boolean flag) throws SecurityException

This method has a boolen parameter flag, which indicates the new accessibility of any fields or methods.
For an example if we are try to access a private method of a class from outside the class then we fetch the method using getDeclaredMethod and set the accessibility as true, so it can be invoked, like: method.setAccessible(true);
Now in which scenario we should use method.setAccessible(false); , for an example it can be used when there is a public method and we set the accessibility as false. But what is the need of that? Is my understanding clear?
If there is no use of method.setAccessible(false) then we can change the method signature like:

public void setAccessible() throws SecurityException
like image 799
Shreyos Adikari Avatar asked Apr 26 '13 07:04

Shreyos Adikari


People also ask

What is setAccessible in Java?

setAccessible(boolean flag) method sets the accessible flag for this object to the indicated boolean value. A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates that the reflected object should enforce Java language access checks.

What is field setAccessible?

Java Field setAccessible() Method The setAccessible () method of Field class sets the accessible flag for this reflected object to the indicated boolean value. A true value indicates that the reflected object should suppress checks for Java language access control when it is used.

What is Accessible object?

Accessible objects also provide methods that clients call to cause the object to perform some action. Accessible objects that have simple elements associated with them are also called parents, or containers. Accessible objects are implemented using Microsoft Active Accessibility's COM-based IAccessible interface.

What is Java reflection?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.


2 Answers

Probably you would never do setAccessible(false) in your entire life. This is because setAccessible doesn't the change the visiblity of the a member permanently. When you to something like method.setAccessible(true) you are allowed to make subsequent calls on this method instance even if the method in the original source is private.

For example consider this:

A.java
*******
public class A
{
   private void fun(){
     ....
   }
}

B.java
***********
public class B{

   public void someMeth(){
       Class clz = A.class; 
       String funMethod = "fun";

       Method method = clz.getDeclaredMethod(funMethod);
       method.setAccessible(true);

       method.invoke(); //You can do this, perfectly legal;

       /** but you cannot do this(below), because fun method's visibilty has been 
           turned on public only for the method instance obtained above **/

       new A().fun(); //wrong, compilation error

       /**now you may want to re-switch the visibility to of fun() on method
          instance to private so you can use the below line**/

      method.setAccessible(false);

      /** but doing so doesn't make much effect **/

  }

}

like image 143
mickeymoon Avatar answered Nov 08 '22 23:11

mickeymoon


Scenario: you removed protection from a private field with Field.setAccessible(true), read it and returned the field into original state with Field.setAccessible(false).

like image 36
Evgeniy Dorofeev Avatar answered Nov 08 '22 23:11

Evgeniy Dorofeev