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 5c9b5bf commit 677ce03
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -237,7 +237,20 @@ static JavaProxy castJavaProxy(final IRubyObject self) {
static void trySetAccessible(AccessibleObject... accesibles) {
if ( ! Ruby.isSecurityRestricted() ) {
try { AccessibleObject.setAccessible(accesibles, true); }
catch(SecurityException e) {}
catch (SecurityException e) {}
catch (RuntimeException re) {
rethrowIfNotInaccessibleObject(re);
}
}
}

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;
}
}

0 comments on commit 677ce03

Please sign in to comment.