前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java.lang.reflect.InaccessibleObjectException: Unable to make

java.lang.reflect.InaccessibleObjectException: Unable to make

作者头像
一个会写诗的程序员
发布2018-08-17 14:34:45
2K0
发布2018-08-17 14:34:45
举报

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.

比如说,下面这段代码:

代码语言:javascript
复制
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 就会报错了:

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档