Merge pull request #1712 from rhernandez35/master
Fix fallback behavior of UnsafeReflectionAllocator when AccessibleObject isn't so accessible
This commit is contained in:
commit
63e747f7f4
@ -79,7 +79,7 @@ final class UnsafeReflectionAccessor extends ReflectionAccessor {
|
|||||||
private static Field getOverrideField() {
|
private static Field getOverrideField() {
|
||||||
try {
|
try {
|
||||||
return AccessibleObject.class.getDeclaredField("override");
|
return AccessibleObject.class.getDeclaredField("override");
|
||||||
} catch (NoSuchFieldException e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.gson.internal.reflect;
|
package com.google.gson.internal.reflect;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.security.Permission;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -41,6 +43,30 @@ public class UnsafeReflectionAccessorTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMakeAccessibleWithRestrictiveSecurityManager() throws Exception {
|
||||||
|
final Permission accessDeclaredMembers = new RuntimePermission("accessDeclaredMembers");
|
||||||
|
final SecurityManager original = System.getSecurityManager();
|
||||||
|
SecurityManager restrictiveManager = new SecurityManager() {
|
||||||
|
@Override
|
||||||
|
public void checkPermission(Permission perm) {
|
||||||
|
if (accessDeclaredMembers.equals(perm)) {
|
||||||
|
throw new SecurityException("nope");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
System.setSecurityManager(restrictiveManager);
|
||||||
|
|
||||||
|
try {
|
||||||
|
UnsafeReflectionAccessor accessor = new UnsafeReflectionAccessor();
|
||||||
|
Field field = ClassWithPrivateFinalFields.class.getDeclaredField("a");
|
||||||
|
assertFalse("override field should have been inaccessible", accessor.makeAccessibleWithUnsafe(field));
|
||||||
|
accessor.makeAccessible(field);
|
||||||
|
} finally {
|
||||||
|
System.setSecurityManager(original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static final class ClassWithPrivateFinalFields {
|
private static final class ClassWithPrivateFinalFields {
|
||||||
private final String a;
|
private final String a;
|
||||||
|
Loading…
Reference in New Issue
Block a user