jbellis / jamm

Java Agent for Memory Measurements
Apache License 2.0
684 stars 125 forks source link

java.lang.reflect.InaccessibleObjectException #29

Closed jtaafri closed 1 year ago

jtaafri commented 6 years ago

This exception is thrown when running on Java 9. It is thrown in addFieldChildren when trying to access private fields:

    private void addFieldChildren(Object current, Deque<Object> stack, Set<Object> tracker, Object ignorableChild, MemoryMeterListener listener) {
        Class<?> cls = current.getClass();
        while (!skipClass(cls)) {
            for (Field field : cls.getDeclaredFields()) {
                if (field.getType().isPrimitive()
                        || Modifier.isStatic(field.getModifiers())
                        || field.isAnnotationPresent(Unmetered.class)) {
                    continue;
                }

                if (ignoreOuterClassReference && field.getName().matches(outerClassReference)) {
                    continue;
                }

                if (ignoreClass(field.getType())) {
                    continue;
                }

                field.setAccessible(true);

Is there any way around this in Java 9?

mebigfatguy commented 6 years ago

take a look at the –illegal-access=permit option in java 9

https://docs.oracle.com/javase/9/tools/java.htm#JSWOR624

but probably java10 means bad things

jtaafri commented 6 years ago

Yeah, it is weird. I have tried that option and still get an exception here.

On Mon, Mar 12, 2018 at 10:02 PM, Dave Brosius notifications@github.com wrote:

take a look at the –illegal-access=permit option in java 9

https://docs.oracle.com/javase/9/tools/java.htm#JSWOR624

but probably java10 means bad things

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jbellis/jamm/issues/29#issuecomment-372521468, or mute the thread https://github.com/notifications/unsubscribe-auth/AjlYkHu5WzMFpugY0QotYU5ZoUDcGZjnks5tdyjBgaJpZM4Snrd7 .

mebigfatguy commented 6 years ago

Are you using classpath or modules?

jtaafri commented 6 years ago

My project does not contain any modules.

On Wed, Mar 14, 2018 at 10:57 PM, Dave Brosius notifications@github.com wrote:

Are you using classpath or modules?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jbellis/jamm/issues/29#issuecomment-373244591, or mute the thread https://github.com/notifications/unsubscribe-auth/AjlYkBcyBObdAx1VVxxCIlwVQKTR0setks5tediegaJpZM4Snrd7 .

victornoel commented 5 years ago

See https://stackoverflow.com/a/41265267/9046139 for a solution.

Basically you need to "open" the failing modules to unnamed ones (i.e. jamm). The solution must be adapted for each module that fails.

blerer commented 1 year ago

We have changed the code to ensure that Jamm can work even without the add-opens.