Under what situations will Java's field.setAccessible(true) fail?
java, reflection
Solution
`setAccessible` is documented to throw a `SecurityException`. Note that the documentation gives cases where `SecurityException` will be thrown even if there is no `SecurityManager` present. Of course it may also fail due to an asynchronous exception: `Thread.stop`, NIO buffer related exception or a JVM error.
The real problem with this code (other than that it uses reflection) is that there is a field that can be set to be partially initialised. This causes a race condition (you have a mutable static, therefore you need to worry about threads (hint, avoid mutable statics!)). Another thread may call `getInt` on the same `Field` before `setAccessible` is called. And as the original questioner appears to have found out, it isn't exception safe either. It would be much safer and clearer to set up the field in a static initialiser.
Problem
I have a situation where a user's code is throwing an `IllegalAccessException` on a field accessed by reflection. Just before accessing the field, `setAccessible(true)` is called. So, it would seem to me that this method is silently failing. Under what situations would this happen? Could this have something to do with a security manager? Here is the code snippet that is causing the exception: ``` private static Field levelField; public int getLevel() { try { if (levelField == null) { levelField = MessageInfo.class.getDeclaredField("level"); levelField.setAccessible(true); } return levelField.getInt(this); // <-- IllegalAccessException thrown here } catch (Exception e) { handleException(e); } return ICompilationUnit.NO_AST; } ```