Does SecurityManager null check give better performance when doing doPrivs?

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) {
  // Do the operation normally, since Java 2 Security is not enabled
  return clazz.getClassLoader();
} 
else {
  // Use a doPriv, since Java 2 Security is enabled
  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.

+4

Source: https://habr.com/ru/post/1652104/


All Articles