Skip to content

Commit

Permalink
Super-gross hack to catch Java 9's InaccessibleObjectException.
Browse files Browse the repository at this point in the history
This new exception is raised by Jigsaw when you attempt to make
accessible the inaccessible elements of a module's classes.
Unfortunately, since the exception doesn't exist on Java 8,
there's no way to catch it (in code that needs to build on Java 8)
except for this hack.

This allows `jruby -e 1` to run properly on Java 9.

This fixes part of #4111.
headius committed Aug 24, 2016
1 parent 658ce6f commit 84b12e7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/src/main/java/org/jruby/java/invokers/RubyToJavaInvoker.java
Original file line number Diff line number Diff line change
@@ -282,14 +282,30 @@ static <T extends AccessibleObject> T setAccessible(T accessible) {
if ( ! Ruby.isSecurityRestricted() ) {
try { accessible.setAccessible(true); }
catch (SecurityException e) {}
catch (RuntimeException re) {
rethrowIfNotInaccessibleObject(re);
}
}
return accessible;
}

private static void rethrowIfNotInaccessibleObject(RuntimeException re) {
// Mega gross, but how else are we supposed to catch this and support Java 8?
if (re.getClass().getName().equals("java.lang.reflect.InaccessibleObjectException")) {
// ok, leave it inaccessible
} else {
// throw all other RuntimeException
throw re;
}
}

static <T extends AccessibleObject> T[] setAccessible(T[] accessibles) {
if ( ! Ruby.isSecurityRestricted() ) {
try { AccessibleObject.setAccessible(accessibles, true); }
catch (SecurityException e) {}
catch (RuntimeException re) {
rethrowIfNotInaccessibleObject(re);
}
}
return accessibles;
}

0 comments on commit 84b12e7

Please sign in to comment.