温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IDEA Debug问题怎么解决

发布时间:2023-01-09 09:26:05 来源:亿速云 阅读:123 作者:iii 栏目:开发技术

这篇文章主要讲解了“IDEA Debug问题怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“IDEA Debug问题怎么解决”吧!

问题引入

最近看了 eclipse 开源的集合 Eclipse Collections,觉得它的 api 相比 JDK 集合 api 简洁,想在实际项目中使用,如下。

JDK api

 // users is List<String> 
 users.stream.map(user -> user.getName()).collect(Collectors.toList());

Eclipse Collections api

 //users is MutableList
 users.collect(user -> user.getName);

项目实际开发中使用集合最多的地方还是来自数据库查询,如下。

JDK api

List<User> findByCity(String city);

我想改成

MutableList<User> findByCity(String city);

然而报错了

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.ArrayList<?>] to type [org.eclipse.collections.api.list.MutableList<?>] for value '[]'; nested exception is java.lang.IllegalArgumentException: Unsupported Collection interface: org.eclipse.collections.api.list.MutableList
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)

太长不看直接结论是改成下列代码。

FastList<User> findByCity(String city);

Debug

对代码简单分析

报错的地方都是 Spring 的包,证明我们使用的 Spring Data JPA 访问数据库,事实上也是。

查看类名称,方法名称。 有 convert.ConversionFailedException/convert.support.ConversionUtils.invokeConverter/convert.support.GenericConversionService.convert等等,关键词 convert,我应该联想到这段代码的功能是把什么类型 convert 到什么类型。

再分析报错的那一行我们会更清晰一点。

  • result 是转换的结果。

  • converter是转换器,结合上面的结论,这个类肯定是真正执行转换的类,我们要的核心代码肯定在这里,如果你直接去看的话,它肯定是一个接口,面向接口编程。

  • sourceType 源类型,结合上述分析肯定是原始类型。

  • targetType 目标类型,同上不赘述。

打断点

IDEA 可以直接点击报错 class 定位到源文件,这里我们先点击 ConversionFailedException ,再点击 ConversionUtils.java:47,发现都是报错的异常,对我们没有帮助。最后我们点击 GenericConversionService.java:192,终于看到一行代码了。

Object result = 
    ConversionUtils.invokeConverter(converter, source, sourceType, targetType);

断点分析

执行过程会停留在断点处,我们可以查看上下文变量类的实例。这里我们以 converter 为例。按照数字步骤点击,如下。

IDEA Debug问题怎么解决

可能的 converter 如下:

1. java.lang.String -> java.lang.Enum
2. NO_OP
3. java.lang.Boolean -> java.lang.String
// 等等。。。。。

由于是底层方法,被调用的次数很多,在这个断点停留的次数也很多。很多次不是我们想要的 converter

条件断点

顾名思义 IDEA 会通过我们添加的条件来判断这个断点是否需要被处理。

我们想要的 converter 是什么呢?回到代码分析阶段,我们想要的 convertersourceType &rarr; targetTypetargetType 类型是什么呢?回到我们自己写的代码。

MutableList<User> findByAdress(String address);

可以看到我们需要 targetTypeMutableList class。

下面添加条件断点:

IDEA Debug问题怎么解决

完整的条件如下:

MutableList.class.isAssignableFrom(targetType.getType());

添加成功的标志如下。

IDEA Debug问题怎么解决

单步调试

Debug 模式启动程序,可以看到 IDEA 停留在我们的条件断点上,并且targetType 的类型正是 MutableList

IDEA Debug问题怎么解决

单步调试代码,来到 org.springframework.core.CollectionFactory#createCollection 方法。

部分代码如下:

//省略的代码
// 判断集合类型是不是 ArrayList 或者 List,显然这里不是
else if (ArrayList.class == collectionType || List.class == collectionType) {
  return new ArrayList<>(capacity);
}
//省略的代码
else {
//如果是集合类型的接口 或者 不是集合类型抛出异常
  if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) {
    throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
  }
  try {
  //如果是集合类型的类,直接通过反射实例化。
    return (Collection<E>) ReflectionUtils.accessibleConstructor(collectionType).newInstance();
  }
}

重回代码分析

我们的 targetType 的类型正是 MutableList,而 MutableList 是接口,走读代码可以发现最终会执行下面的代码,最终导致抛出异常。

if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) {
    throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName());
  }

翻看控制台找到了下面的异常信息,这也侧面反映我们之前找的报错位置不是很精确。我们寻找异常时应该选择最原始的异常信息。

Caused by: java.lang.IllegalArgumentException: Unsupported Collection type: org.eclipse.collections.api.list.MutableList
	at org.springframework.core.CollectionFactory.createCollection(CollectionFactory.java:205)
	at org.springframework.core.convert.support.CollectionToCollectionConverter.convert(CollectionToCollectionConverter.java:81)

继续分析源码可以发现,如果我们定义的类型不是接口,JPA 就会通过反射创建集合,即如下代码:

return (Collection<E>) ReflectionUtils.accessibleConstructor(collectionType).newInstance();

所以我们只需要将 MutableList 换成它的实现类即可,比如 FastList。最终代码如下:

FastList<User> findByCity(String city);

感谢各位的阅读,以上就是“IDEA Debug问题怎么解决”的内容了,经过本文的学习后,相信大家对IDEA Debug问题怎么解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI