Error querying database. Cause: java.lang.reflect.InaccessibleObjectException:

最近开发过程中,居然碰到了一个Arrays.asList的错,怎么个场景呢?传参一个用固定符号拼接的字符串,需要转成集合然后再myBatis里in判断。然后就报错了。

一、代码层面

service层面:

java 复制代码
shortDetailUrlList = Arrays.asList(params.getShortDetailUrl().split("-"));

mybatis层面:

xml 复制代码
<if test="shortDetailUrlList != null and shortDetailUrlList.size() > 0">
            and tcp.short_url in
            <foreach item="shortDetailUrl" collection="shortDetailUrlList" open="(" separator="," close=")"
                     index="index">
                #{shortDetailUrl}
            </foreach>
        </if>

二、错误日志

bash 复制代码
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.reflect.InaccessibleObjectException: Unable to make public int java.util.Arrays$ArrayList.size() accessible: module java.base does not "opens java.util" to unnamed module @65e579dc
### Cause: java.lang.reflect.InaccessibleObjectException: Unable to make public int java.util.Arrays$ArrayList.size() accessible: module java.base does not "opens java.util" to unnamed module @65e579dc
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
	at jdk.proxy2/jdk.proxy2.$Proxy103.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
	at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
	at jdk.proxy2/jdk.proxy2.$Proxy152.getConstructInfoByReceiveId(Unknown Source)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:577)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at jdk.proxy2/jdk.proxy2.$Proxy153.getConstructInfoByReceiveId(Unknown Source)
	at com.meta.image.admin.service.impl.ReportBaseServiceImpl.getReportConstructInfo(ReportBaseServiceImpl.java:389)
	at com.meta.image.admin.controller.ReportBaseV2Controller.getReportConstructInfo(ReportBaseV2Controller.java:75)
	at com.meta.image.admin.controller.ReportBaseV2Controller$$FastClassBySpringCGLIB$$b659f1a1.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)

三、日志分析

根据错误日志可以晓得:数据库报错了,然后还是反射的错。错误的根源就是:

bash 复制代码
Unable to make public int java.util.Arrays$ArrayList.size() accessible

那这错误的根源到底是什么呢?那还得从Array.asList分析入手啊。。。

四、Array.asList的坑

1、使用场景

一般我们字符串切割,很容易想到额就是它了,固定符号分割,获取一个集合列表。

【注意】:

复制代码
	- 该方法适用于对象型数据的数组(String、Integer...)
	- 该方法不建议使用于基本数据类型的数组(byte,short,int,long,float,double,boolean)
	- 该方法将数组与List列表链接起来:当更新其一个时,另一个自动更新
	- 不支持add()、remove()、clear()等方法

2、为何说是一个坑?

① :此方法得到的List的长度是不可改变的(看源码就会一目了然)

当你向这个List添加或删除一个元素时(例如 list.add("d");)程序就会抛出异尝(java.lang.UnsupportedOperationException)。

java 复制代码
public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

    /**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private final E[] a;

        ArrayList(E[] array) {
            a = Objects.requireNonNull(array);
        }
    

看源码你就会发现,每次调用asList,都是new一个新的,来操作增删。

②:虽然也是集合,但是确实内部类,和我们所用的java.util.arrayList是不一样的


回到第三点,为何会报反射异常,因为内部类,mybatis去获取size的时候,是获取不到的。解决办法也有很多种。

相关推荐
whitelbwwww7 小时前
CUDA C++实战--中级
开发语言·前端·c++
MSTcheng.7 小时前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
FungLeo8 小时前
FengCMS · PHP+MySQL 升级实录 · 内容写入一致性:锁等待超时与跨模型子树删除竞态
开发语言·mysql·php·工程实践·fengcms
Yeats_Liao8 小时前
物联网平台MongoDB实战|(一)为什么选择MongoDB
数据库·物联网·mongodb
鹤落晴春9 小时前
MySQL日志梳理
运维·数据库·mysql
jaysee-sjc9 小时前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
谙弆悕博士9 小时前
多智能体架构详解:市场拍卖式架构(Market-based / 竞价协商)
开发语言·架构·去中心化·多智能体·智能体·拍卖架构
ShineWinsu9 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
五彩小白10 小时前
Redis的过期删除和内存淘汰机制
数据库·redis·缓存
承渊政道10 小时前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表