22
33import java .lang .reflect .Field ;
44import java .lang .reflect .Modifier ;
5+ import java .security .AccessController ;
6+ import java .security .PrivilegedAction ;
57
68/**
79 * Used to replace a "Finder" that is located as a static field (typically on an Model entity bean).
1113 */
1214public class WithStaticFinder <T > {
1315
14- Class <T > beanType ;
16+ final Class <T > beanType ;
1517
1618 String fieldName ;
1719
@@ -41,7 +43,7 @@ public WithStaticFinder(Class<T> beanType, String fieldName) {
4143 * <p/>
4244 * Note that the test double instance is not set until <code>useTestDouble()</code> is called.
4345 */
44- public WithStaticFinder as (Object testDouble ) throws FinderFieldNotFoundException {
46+ public WithStaticFinder < T > as (Object testDouble ) throws FinderFieldNotFoundException {
4547
4648 try {
4749 this .testDouble = testDouble ;
@@ -50,7 +52,16 @@ public WithStaticFinder as(Object testDouble) throws FinderFieldNotFoundExceptio
5052 this .field .setAccessible (true );
5153 try {
5254 Field modifiersField = Field .class .getDeclaredField ("modifiers" );
53- modifiersField .setAccessible (true );
55+
56+ /**
57+ * If the project using this library has a SecurityManager set up, permission may be denied.
58+ * Therefor, running this as a privileged action.
59+ */
60+ AccessController .doPrivileged ((PrivilegedAction <Object >) () -> {
61+ modifiersField .setAccessible (true );
62+ return null ;
63+ });
64+
5465 modifiersField .setInt (field , field .getModifiers () & ~Modifier .FINAL );
5566 } catch (NoSuchFieldException e ) {
5667 throw new RuntimeException (e );
0 commit comments