This method returns
true
if the
caller can access this method.
Examples
package com.logicbig.example.method;
import java.lang.reflect.Method;
public class CanAccessExample { private static class Test { private void doSomething() { } }
public static void main(String... args) throws NoSuchMethodException { Method m = Test.class.getDeclaredMethod("doSomething"); Test test = new Test(); System.out.println(m.canAccess(test)); boolean b = m.trySetAccessible(); System.out.println(b); System.out.println(m.canAccess(test)); } }
Output
true true true
package com.logicbig.example.method;
import java.lang.reflect.Method;
public class CanAccessExample2 { public static class Test { public void doSomething() { } }
public static void main(String... args) throws NoSuchMethodException { Method m = Test.class.getDeclaredMethod("doSomething"); boolean b = m.canAccess(new Test()); System.out.println(b);