Skip to content

Commit

Permalink
Check accesibility through a class that's in our control
Browse files Browse the repository at this point in the history
jmiettinen authored and headius committed Dec 15, 2016
1 parent 96f7273 commit 3eb55f4
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 11 additions & 0 deletions core/src/main/java/org/jruby/javasupport/DummyForJavaUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jruby.javasupport;

/**
* This class exists only for access-checks to set {@linkplain JavaUtil#CAN_SET_ACCESSIBLE}
*/
public class DummyForJavaUtil {

private static final Object PRIVATE = new Object[0];
public static final Object PUBLIC = PRIVATE;

}
11 changes: 7 additions & 4 deletions core/src/main/java/org/jruby/javasupport/JavaUtil.java
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static java.lang.Character.isLetter;
import static java.lang.Character.isLowerCase;
@@ -46,7 +47,6 @@
import java.lang.reflect.ReflectPermission;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.AccessController;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
@@ -98,9 +98,12 @@ public class JavaUtil {

if (RubyInstanceConfig.CAN_SET_ACCESSIBLE) {
try {
AccessController.checkPermission(new ReflectPermission("suppressAccessChecks"));
canSetAccessible = true;
} catch (Throwable t) {
// We want to check if we can access a commonly-existing private field through reflection. If so,
// we're probably able to access some other fields too later on.
Field f = DummyForJavaUtil.class.getDeclaredField("PRIVATE");
f.setAccessible(true);
canSetAccessible = f.get(null).equals(DummyForJavaUtil.PUBLIC);
} catch (Exception t) {
// added this so if things are weird in the future we can debug without
// spinning a new binary
if (Options.JI_LOGCANSETACCESSIBLE.load()) {

0 comments on commit 3eb55f4

Please sign in to comment.