Δευτέρα 3 Ιανουαρίου 2011

How to check who calls your method!

If you want to lock some method so that it will be public but only some specific methods or classes or packages will be allowed to access, then you can modify the following method in order to verify whether the caller should be allowed to execute the method or you should rise an exception instead!


public static boolean isAllowedToCall(){
String callerClassName= new Throwable().getStackTrace()[2].getClassName();
return (callerClassName.startsWith("com.gorbas")||callerClassName.contains("binding"));
}

In my case I allow only packages starting with com.gorbas and classes that contain the word "binding" in their full name.

LinkWithin

Blog Widget by LinkWithin

Mobile edition