Skip to content

java 11 compatibility: handle a new exception if a field is inaccessible#59

Closed
sda399 wants to merge 5 commits intoehcache:masterfrom
sda399:master
Closed

java 11 compatibility: handle a new exception if a field is inaccessible#59
sda399 wants to merge 5 commits intoehcache:masterfrom
sda399:master

Conversation

@sda399
Copy link

@sda399 sda399 commented Sep 20, 2019

  • setAccessible function now throws a new exception
  • catch the new exception class that appeared in java9+ only

- setAccessible function now throws a new exception
- catch the new exception class that appeared in java9+ only
@sda399
Copy link
Author

sda399 commented Sep 20, 2019

Hi all,
this is a simple patch to fix a java11 compat I run into.

java.lang.reflect.InaccessibleObjectException: Unable to make field jdk.internal.ref.PhantomCleanable jdk.internal.ref.PhantomCleanable.prev accessible: module java.base does not "opens jdk.internal.ref" to unnamed module @3c153a1
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:340)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280)
	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
	at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
	at org.ehcache.sizeof.ObjectGraphWalker.getAllFields(ObjectGraphWalker.java:275)
	at org.ehcache.sizeof.ObjectGraphWalker.getFilteredFields(ObjectGraphWalker.java:234)
	at org.ehcache.sizeof.ObjectGraphWalker.walk(ObjectGraphWalker.java:176)
	at org.ehcache.sizeof.SizeOf.deepSizeOf(SizeOf.java:75)

Copy link
Contributor

@henri-tremblay henri-tremblay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== null to fix.

@mprusakov
Copy link
Contributor

@chrisdennis would it be possible to merge this please? The library doesn't really work well with java 11 without this change and as far as i can tell all the issues have been fixed. I'd be more than happy to assist if any more changes are required

} catch (RuntimeException e) {
// new class (jdk9+ only) can be thrown
if ("java.lang.reflect.InaccessibleObjectException".equals(e.getClass().getName())) {
LOG.error("JPMS blocks field access. This prevents Ehcache from accessing "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be just a warning. Because it might occur a lot and there's nothing we can do about it.

LOG.error("Security settings prevent Ehcache from accessing the subgraph beneath '{}'" +
" - cache sizes may be underestimated as a result", field, e);
continue;
} catch (RuntimeException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this is swallowing thrown RuntimeException instances without logging anything and then adding an inaccessible field to the collection. I think it's going to be simpler (and cleaner) to just:

} catch (RuntimeException e) {
  LOG.warn("The JVM is preventing Ehcache from accessing the subgraph beneath '{}'" +
           " - cache sizes may be underestimated as a result", field, e);
}

@mprusakov
Copy link
Contributor

Unfortunately i do not have access to the original for had to re-fork and re-raise the pr, available here #61

@chrisdennis
Copy link
Member

Replaced with #61

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants