Skip to content

Commit

Permalink
fixes walking over internal JDK11 fields
Browse files Browse the repository at this point in the history
This is a backport of a fix developed in:

Original Author: @mprusakov
ehcache/sizeof#61
ehcache/sizeof#54

Also adds unit test for that issue.
  • Loading branch information
Mateusz Szygenda committed Feb 1, 2023
1 parent 0b23e58 commit 43826db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ private static Collection<Field> getAllFields(Class<?> refClass) {
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) {
LOG.warn("The JVM is preventing Ehcache from accessing the subgraph beneath '{}'" +
" - cache sizes may be underestimated as a result", field, e);
continue;
}

fields.add(field);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand Down Expand Up @@ -63,6 +65,7 @@ public void increment(String value) {
assertThat(map.isEmpty(), is(true));

assertThat(walker.walk(MAX_SIZEOF_DEPTH, false, new SomeInnerClass()), is(14L));

assertThat(map.remove("java.util.concurrent.locks.ReentrantReadWriteLock$Sync$ThreadLocalHoldCounter"), is(1L));

assertThat(map.remove(SomeInnerClass.class.getName()), is(1L));
Expand Down Expand Up @@ -91,6 +94,30 @@ public class SomeInnerClass {
private final Object four = new ReentrantReadWriteLock();
private final Object[] anArray = new Object[]{new Object(), new Object(), new Object(), one, two, two, three, four, value};
private final int[] anIntArray = new int[] {1, 2, 1300 };
}

@Test
public void testWalksAGraphWithInacessibleFieldsWithoutThrowingException() {
class ClassWithNestedInacessibleFields {
/**
* Certain fields of JDK11 classes are internal and throw new kind of exception upon reflection access
*
* java.lang.reflect.InaccessibleObjectException: Unable to make field final jdk.internal.loader.URLClassPath jdk.internal.loader.ClassLoaders$AppClassLoader.ucp accessible: module java.base does not "opens jdk.internal.loader"
*/
public final URLClassLoader objectWithInaccessibleFields = new URLClassLoader(new URL[]{});
}

ObjectGraphWalker walker = new ObjectGraphWalker(
new ObjectGraphWalker.Visitor() {
@Override
public long visit(Object object) {
return 0;
}
}, new PassThroughFilter()
);

walker.walk(MAX_SIZEOF_DEPTH, false, new ClassWithNestedInacessibleFields());
}


}

0 comments on commit 43826db

Please sign in to comment.