java.lang.reflect.InaccessibleObjectException: Unable to make ...

This exception occurs in a wide variety of scenarios when running an application on Java 9. Certain libraries and frameworks (Spring, Hibernate, JAXB) are particularly prone to it.

比如说,下面这段代码:

package com.guigarage.shell;

import java.util.List;

import com.alibaba.fastjson.JSON;

import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;

public class JShellTest {

    private static JShell shell;

    public static synchronized void eval(String command) {
        shell = JShell.builder().build();
        List<SnippetEvent> events = shell.eval(command);
        System.out.println(events);
        System.out.println(JSON.toJSONString(events));

    }

    public static void main(String[] args) {
        eval("System.out.println(\"Hello,World!\")");
    }

}


运行到JSON.toJSONString 就会报错了:

Hello,World!
[SnippetEvent(snippet=Snippet:StatementKey#1-System.out.println("Hello,World!"),previousStatus=NONEXISTENT,status=VALID,isSignatureChange=true,causeSnippetnullvalue=)]
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final boolean jdk.jshell.SnippetEvent.isSignatureChange accessible: module jdk.jshell does not "opens jdk.jshell" to unnamed module @2421cc4
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
    at com.alibaba.fastjson.util.TypeUtils.setAccessible(TypeUtils.java:1798)
    at com.alibaba.fastjson.util.FieldInfo.<init>(FieldInfo.java:165)
    at com.alibaba.fastjson.util.TypeUtils.computeGetters(TypeUtils.java:1498)
    at com.alibaba.fastjson.util.TypeUtils.buildBeanInfo(TypeUtils.java:1196)
    at com.alibaba.fastjson.serializer.SerializeConfig.createJavaBeanSerializer(SerializeConfig.java:109)
    at com.alibaba.fastjson.serializer.SerializeConfig.getObjectWriter(SerializeConfig.java:693)
    at com.alibaba.fastjson.serializer.SerializeConfig.getObjectWriter(SerializeConfig.java:405)
    at com.alibaba.fastjson.serializer.JSONSerializer.getObjectWriter(JSONSerializer.java:359)
    at com.alibaba.fastjson.serializer.ListSerializer.write(ListSerializer.java:129)
    at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:278)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:652)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:594)
    at com.alibaba.fastjson.JSON.toJSONString(JSON.java:559)
    at com.guigarage.shell.JShellTest.eval(JShellTest.java:18)
    at com.guigarage.shell.JShellTest.main(JShellTest.java:23)


The exception is caused by the Java Platform Module System that was introduced in Java 9, particularly its implementation of strong encapsulation. It only allows access under certain conditions, the most prominent ones are:

the type has to be public
the owning package has to be exported
The same limitations are true for reflection, which the code causing the exception tried to use. More precisely the exception is caused by a call to setAccessible.

更多参见:https://stackoverflow.com/questions/41265266/how-to-solve-inaccessibleobjectexception-unable-to-make-member-accessible-m

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐