简体   繁体   English

将返回类型从Integer更改为int,而不会破坏向后兼容性

[英]Change return type from Integer to int without breaking backwards compatibility

I'm doing QA on a certain Java 7 API, that is in public usage as Open Source. 我正在使用某个Java 7 API进行QA,这在公共场合用作开源。 My Clirr report checks for backwards API compatibility. My Clirr报告检查向后API兼容性。 I got the following errors: 我收到以下错误:

[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

So this means that my developers changed the return type of Foo from Integer to int , and the return type of Bar was changed from byte to short . 所以这意味着我的开发人员将Foo的返回类型从Integer更改为int ,并且Bar的返回类型从byte更改为short The method signatures stayed the same, so overloading is not an option. 方法签名保持不变,因此不能选择重载。

  1. Does this really break backwards compatibility? 这真的打破了向后兼容性吗? Or is Clirr giving me a false positive? 或者Clirr给我一个误报?
  2. How can I unbreak backwards compatibility? 我怎样才能破坏向后兼容性? Is this even possible without introducing new method names? 如果不引入新的方法名称,这是否可行?

I have got a couple of other errors about return type changes (mostly some void s to float ), but they are in protected methods, so I am going to ignore them. 我有一些关于返回类型更改的其他错误(大多数是void float ),但是它们在protected方法中,所以我将忽略它们。

It clearly breaks backwards compatibility as your API's user may have had a code like: 它显然打破了向后兼容性,因为您的API用户可能拥有如下代码:

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

Which won't compile from now on. 从现在开始不会编译。

Considering that the new version of getAnchorData() returns a wider amount of values, you can't perform the transition without breaking backwards compatibility, as client's old code may not be suitable to accept all the possible values. 考虑到新版本的getAnchorData()返回更多的值,您无法在不破坏向后兼容性的情况下执行转换,因为客户端的旧代码可能不适合接受所有可能的值。

Also regarding your last statement: 关于你的最后陈述:

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. 我有一些关于返回类型更改的其他错误(大多数是浮动的空洞),但它们是受保护的方法,所以我将忽略它们。

If these methods are in public non- final classes, then you break backwards compatibility by doing so as well. 如果这些方法属于publicfinal类,那么您也可以通过这样做来打破向后兼容性。

Backward compatibility is definitely broken, as the expression stack is typed, on the result unboxing is done. 向后兼容性肯定会被破坏,因为表达式堆栈是打字的,结果是取消装箱完成。

The second question is: whether a recompilation against the new API would suffice and deliver no errors. 第二个问题是:针对新API的重新编译是否足够并且不会发生错误。

Integer to int, though better style might now propagate style warnings on Integer usage in client software. 整数到int,虽然更好的样式现在可能会传播客户端软件中Integer用法的样式警告 Even when inferring types as in java 8 it should go well, except when immediately comparing with null: 即使在java 8中推断类型时也应该顺利,除非立即与null进行比较:

if (nameToUnicode("ĉarma") == null)

And that is a very rare case. 这是一个非常罕见的案例。

With byte/short hands-on is needed. 需要字节/短手动操作。 Also one might expect short values exceeding the byte range, or sign overflow for short values between 128 and 255. Only if the logic is unchanged, the values still in -128 .. 127 all is functionally well. 还可以预期短值超出字节范围,或者对于128到255之间的短值进行符号溢出。仅当逻辑未更改时,仍然在-128 ... 127中的值在功能上都很好。

Though not compilable, sources must be adapted. 虽然不可编辑,但必须调整来源。


Providing backward compatibility: 提供向后兼容性:

For existing binary jars: keep the old functions, @Deprecated and javadoc comments for new usage. 对于现有的二进制jar:保留旧函数, @Deprecated和javadoc注释以用于新用法。 Throwing IllegalStateException when out-of-range for short to byte. 当超出范围超出字节时抛出IllegalStateException。

Apart from what user3707125 wrote another problem might be if someone extends your class and overrides your method. 除了user3707125写的另外一个问题可能是有人扩展你的类并覆盖你的方法。 Consider following example: 考虑以下示例:

public class BaseClass {
    public Integer getInt() {
        return 1;
    }
}

public class MyClass extends BaseClass {
    @Override
    public Integer getInt() {
        return null;
    }
}

If you changed return type in BaseClass to int then MyClass will have a compilation error. 如果将BaseClass中的返回类型更改为int,则MyClass将出现编译错误。 So this breaks backward compatability. 所以这打破了向后的兼容性。 The same is when you change byte to short. 将字节更改为短时也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在Java中将常量从int更改为byte而不会破坏向后兼容性吗? - Can I change constant from int to byte in Java without breaking backward compatibility? 在不破坏二进制向后兼容性的情况下,Java 中的扩展方法可能吗? - Extension Methods in Java possible without breaking Binary Backwards Compatibility? 如何在不破坏当前代码的情况下修复错误“此方法必须返回 int 类型的结果”? - How can I fix the error "This method must return a result of type int" without breaking the current code? 在Java中将Integer转换为int作为堆栈的返回类型 - Convert Integer to int in Java as a return type of stack 为 Jooq 3.16 中的几何类型提供向后兼容性 - Provide backwards compatibility for Geometry type in Jooq 3.16 覆盖和返回类型兼容性 - Overriding and return type compatibility 我可以将返回类型更改为严格子类型并保留二进制兼容性吗? - Can I change a return type to be a strict subtype and retain binary compatibility? 类型不匹配:无法从整数转换为整数 - Type mismatch: cannot convert from Integer to int 从基本类型int更改为类Integer - Changing from primitive type int to class Integer 更改属性 DynamoDB 的数据类型时如何确保向后兼容性 - How to ensure backwards compatibility when changing data type of attribute DynamoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM