Java Reflection Java Java API
Class:
java.lang.reflect.Method
Method:
public final boolean trySetAccessible()
Set the
accessible
flag for this reflected object to
true
if possible. This method sets the
accessible
flag, as if by invoking
setAccessible(true)
, and returns the possibly-updated value for the
accessible
flag. If access cannot be enabled, i.e. the checks or Java language access control cannot be suppressed, this method
returns
false
(as opposed to
setAccessible(true)
throwing
InaccessibleObjectException
when it fails).
Examples
package com.logicbig.example.method;
import java.lang.reflect.Method;
public class TrySetAccessibleExample { private static class Test { private void doSomething() { System.out.println("working"); } }
public static void main(String... args) throws NoSuchMethodException { Method m = Test.class.getDeclaredMethod("doSomething"); Test t = new Test(); System.out.println(m.canAccess(t));
boolean b = m.trySetAccessible(); System.out.println(b);
System.out.println(m.canAccess(t)); } }
Outputfalse true true
|