When will the SecurityManager not allow this?
The javadoc says:
First, if there is a security manager, its checkPermission method is called with a ReflectPermission("suppressAccessChecks")
permission.
A SecurityException
is raised if flag is true but accessibility of this object may not be changed (for example, if this element object is a Constructor
object for the class Class
).
As to your other question
How portable is it to include this in a library? Will it fail when imported into certain contexts?
It is portable across JVM implementations because Field
is defined in the core library with these semantics. It is not portable across instances because different JVM instances may have differently configured security policies.