数组转换 Java 8 与 Java 9 [英] array cast Java 8 vs Java 9

查看:27
本文介绍了数组转换 Java 8 与 Java 9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么这段代码适用于 java 8 而不适用于 java 9

Does anyone have insights on why this code works on java 8 but not on java 9

String[] strings = (String[]) Arrays.asList("foo", "bar").toArray();
for (String string : strings) {
    System.out.println(string);
}

我知道我们可以在执行 toArray 而不是强制转换时指定类型.但是我在调​​试我们的一个依赖项 (hive-metastore-2.1.1 HiveMetaStoreClient 第 274 行).所以我没有更改代码的自由,我们正在运行 java 9.有没有办法解决这个问题?这是 Java 9 的问题(因为它看起来像是一个重大更改)还是只是在 hive 存储库中提交了一个错误.

I understand we can specify the type while doing toArray instead of casting it. But I found this issue while debugging one of our dependency (hive-metastore-2.1.1 HiveMetaStoreClient line 274). So I don't have the liberty to change the code and we are running java 9. Is there a way to get around this? Is this a problem with java 9(since it seems like a breaking change) or just file a bug in the hive repo.

推荐答案

看起来可能是由于更改 (coll) Arrays.asList(x).toArray().getClass() 应该是 Object[].class

Seems like it might be due to the change (coll) Arrays.asList(x).toArray().getClass() should be Object[].class

看起来他们修复了 toArray 可能返回 Object 以外的类型的错误.

Looks like they fixed a bug that toArray could return types other than Object.

引用发行说明

这可能会导致预期旧行为失败并返回 ClassCastException 的代码...如果发生此问题,请重写代码以使用单参数形式 toArray(T[]),并提供所需数组类型的实例.这也将消除对演员的需要.

This may cause code that was expecting the old behavior to fail with a ClassCastException...If this problem occurs, rewrite the code to use the one-arg form toArray(T[]), and provide an instance of the desired array type. This will also eliminate the need for a cast.

因此,您似乎需要在 Hive 存储库中提交错误才能更新代码以在此更改后工作.

So it seems like you'll need to file a bug in the Hive repo to update the code to work after this change.

看起来他们实际上在未来的提交中添加了一个配置值,如果设置为某个值实际上会避免导致问题的代码路径. https://github.com/apache/hive/commit/07492e0d2f1942c1794a3190610e10207c850cf7#diff-ca39aa4869cc58909a31c761cd7a27ccR257

Looks like they actually added a configuration value in a future commit which if set with a certain value would actually avoid the code path causing the issue. https://github.com/apache/hive/commit/07492e0d2f1942c1794a3190610e10207c850cf7#diff-ca39aa4869cc58909a31c761cd7a27ccR257

也许您可以升级到具有此功能的版本并使用此配置来避免该问题.只要您不关心需要该代码路径的功能.似乎导致问题的代码是随机选择要使用的 URI,而不是仅从列表中选择第一个.

Maybe you can upgrade to a version that has this and use this configuration to avoid the problem. So long as you don't care about the functionality that requires that code path. Seems like the code causing the problem is selecting which URI to use randomly instead of just picking the first one out of a list.

这篇关于数组转换 Java 8 与 Java 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆