gpt4 book ai didi

java - 在不破坏向后兼容性的情况下将返回类型从 Integer 更改为 int

转载 作者:行者123 更新时间:2023-12-02 15:18:17 25 4
gpt4 key购买 nike

我正在对某个 Java 7 API 进行质量检查,该 API 作为开源公开使用。我的Clirr报告检查向后 API 兼容性。我收到以下错误:

[ERROR] 7006: com.example.Foo: Return type of method 'public java.lang.Integer nameToUnicode(java.lang.String)' has been changed to int
[ERROR] 7006: com.example.Bar: Return type of method 'public byte getAnchorDelta()' has been changed to short

所以这意味着我的开发人员将 Foo 的返回类型从 Integer 更改为 int,并将 Bar 的返回类型更改为 Integer 已从 byte 更改为 short。方法签名保持不变,因此重载不是一种选择。

  1. 这真的会破坏向后兼容性吗?还是 Clirr 给我误报?
  2. 如何打破向后兼容性?如果不引入新的方法名称,这是否可能?

我有一些关于返回类型更改的其他错误(主要是一些 voidfloat),但它们在 protected方法,所以我将忽略它们。

最佳答案

它显然破坏了向后兼容性,因为您的 API 用户可能有这样的代码:

if (nameToUnicode("...").equals(4)) { ... } // no such method
byte delta = getAnchorDelta(); // typecast required

从现在开始不会编译。

考虑到新版本的 getAnchorData() 返回更多的值,你不能在不破坏向后兼容性的情况下执行转换,因为客户端的旧代码可能不适合接受所有可能的值。

还有关于你最后的声明:

I have got a couple of other errors about return type changes (mostly some voids to float), but they are in protected methods, so I am going to ignore them.

如果这些方法在 publicfinal 类中,那么这样做也会破坏向后兼容性。

关于java - 在不破坏向后兼容性的情况下将返回类型从 Integer 更改为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com