Close

Java Reflection - Method.setAccessible() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public void setAccessible(boolean flag)

Set the accessible flag for this method to the provided boolean value. This method is defined in the super class AccessibleObject. The semantics of this method has been changed significantly in Java 9.

From AccessibleObject#setAccessible():

Set the accessible flag for this reflected object to the indicated boolean value. A value of true indicates that the reflected object should suppress checks for Java language access control when it is used. A value of false indicates that the reflected object should enforce checks for Java language access control when it is used, with the variation noted in the class description.



This method may be used by a caller in class C to enable access to a member of declaring class D if any of the following hold:

  • C and D are in the same module.
  • The member is public and D is public in a package that the module containing D exports to at least the module containing C .
  • The member is protected static , D is public in a package that the module containing D exports to at least the module containing C , and C is a subclass of D .
  • D is in a package that the module containing D opens to at least the module containing C . All packages in unnamed and open modules are open to all modules and so this method always succeeds when D is in an unnamed or open module.

This method cannot be used to enable access to private members, members with default (package) access, protected instance members, or protected constructors when the declaring class is in a different module to the caller and the package containing the declaring class is not open to the caller's module.

See also: Java 9 Modules - Reflective Access via open and opens clauses.



public static void setAccessible(AccessibleObject[] array,
                                 boolean flag)

Above static method is defined in AccessibleObject class.

Convenience method to set the accessible flag for an array of reflected objects.


Examples


package com.logicbig.example.method;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SetAccessibleExample {
private static class Test {
private static void doSomething() {
System.out.println("working");
}
}

public static void main(String... args) throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
Method m = Test.class.getDeclaredMethod("doSomething");
m.setAccessible(true);
m.invoke(null);
}
}

Output

working




Without setting this flag:

package com.logicbig.example.method;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SetAccessibleExample2 {

private static class Test {
private static void doSomething() {
System.out.println("working");
}
}

public static void main(String... args) throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
Method m = SetAccessibleExample2.Test.class.getDeclaredMethod("doSomething");
//m.setAccessible(true);
m.invoke(null);
}
}

Output

Caused by: java.lang.IllegalAccessException: class com.logicbig.example.method.SetAccessibleExample2 cannot access a member of class com.logicbig.example.method.SetAccessibleExample2$Test with modifiers "private static"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:360)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:589)
at java.base/java.lang.reflect.Method.invoke(Method.java:556)
at com.logicbig.example.method.SetAccessibleExample2.main(SetAccessibleExample2.java:24)
... 6 more




package com.logicbig.example.method;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

public class SetAccessibleExample3 {

private static class Test {
private void doSomething() {
}

private void process() {
}
}

public static void main(String... args) throws NoSuchMethodException {
Method m1 = Test.class.getDeclaredMethod("doSomething");
Method m2 = Test.class.getDeclaredMethod("process");
Test test = new Test();
System.out.println(m1.canAccess(test));
System.out.println(m2.canAccess(test));

AccessibleObject.setAccessible(new AccessibleObject[]{m1, m2}, true);
System.out.println(m1.canAccess(test));
System.out.println(m2.canAccess(test));

}
}

Output

false
false
true
true




See Also