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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏算法修养

HOJ 1438 The Tower of Babylon(线性DP)

The Tower of Babylon My Tags Cancel - Seperate tags with commas. Source...

29011
来自专栏ml

HDUOJ----剪花布条

剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java...

36212
来自专栏ml

HDU----(3294)Girls' research(manacher)

Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/3...

2835
来自专栏码匠的流水账

聊聊sentinel的ModifyRulesCommandHandler

本文主要研究一下sentinel的ModifyRulesCommandHandler

1261
来自专栏个人分享

Socket与Http方式解析发送xml消息封装中间件jar包

  最近项目代码中太多重复的编写Document,不同的接口需要不同的模板,于是重写提取公共部分打成jar包,方便各个系统统一使用~

1593
来自专栏码匠的流水账

聊聊storm的reportError

storm-2.0.0/storm-client/src/jvm/org/apache/storm/task/IErrorReporter.java

2033
来自专栏开发与安全

90% of python in 90 minutes

注:本文整理自 http://www.slideshare.net/MattHarrison4/learn-90 -----------------------...

2230
来自专栏小樱的经验随笔

HDU 4256 The Famous Clock

The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3...

2766
来自专栏算法修养

CodeForces 668B Little Artem and Dance

B. Little Artem and Dance time limit per test 2 second memory limit per test...

3465
来自专栏码匠的流水账

聊聊storm的reportError

storm-2.0.0/storm-client/src/jvm/org/apache/storm/task/IErrorReporter.java

1513

扫码关注云+社区

领取腾讯云代金券