Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static Object getUnsafeInstance() {
private static Field getOverrideField() {
try {
return AccessibleObject.class.getDeclaredField("override");
} catch (NoSuchFieldException e) {
} catch (Exception e) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package com.google.gson.internal.reflect;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;
import java.security.Permission;

import org.junit.Test;

Expand All @@ -41,6 +43,30 @@ public void testMakeAccessibleWithUnsafe() throws Exception {
}
}

@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")
private static final class ClassWithPrivateFinalFields {
private final String a;
Expand Down