I have repeatedly seen the following pattern when performing operations that require special privileges (for example, Class.getClassLoader())
final Class<?> clazz = MyClass.class;
if(System.getSecurityManager() == null) {
return clazz.getClassLoader();
}
else {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return clazz.getClassLoader();
}
});
}
I assume that the advantage is that Java 2 protection is not enabled, the code avoids building an anonymous inner class and avoids calling the additional AccessController.doPrivilaged () method.
So my question is:
Is an extra null check potentially possible to avoid an anonymous object PrivilagedActionand an extra doPriv call on the stack?
, , 1-, , , , .
, JVM , JVM, , / doPriv.