java.lang.reflect.InaccessibleObjectException: Unable to make

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值