Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

fixes walking over internal JDK11 fields #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}


}