简体   繁体   English

关于“二进制兼容性”的问题

[英]Issue about “binary compatibility”

When I read Java Language Specification (JLS8) > Binary Compatibility , one of a set changes that doesn't break binary compatibility is: 当我阅读Java语言规范(JLS8)> 二进制兼容性时 ,不会破坏二进制兼容性的一组更改之一是:

Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur or failed by going into an infinite loop or causing a deadlock 更改方法或构造函数以返回其先前曾抛出异常的输入的值,这些异常通常不应该发生或因进入无限循环或导致死锁而失败

I don't understand this idea. 我不明白这个主意。

Please help clarify and give an example to demonstrate it. 请帮助澄清并举例说明。

Not very sure about it but what I can infer from it is this. 不是很确定,但是我可以从中推断出这一点。
Suppose you had some method which would accept some integer and return some integer as a result. 假设您有一些方法可以接受某个整数并返回一个整数。

int myMethod(int arg);

Now what JLS speaks is 现在JLS讲的是

here is a list of some important binary compatible changes that the Java programming language supports: 这是Java编程语言支持的一些重要的二进制兼容更改的列表:

..... .....
Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur or failed by going into an infinite loop or causing a deadlock. 更改方法或构造函数以在其先前抛出异常的输入上返回值,这些异常通常不应该发生或因进入无限循环或导致死锁而失败。

Suppose for some invalid input (like your method was only designed to work on positive integers and you passed other than that), the method either throws some exception (either as a part of validation or otherwise) or it enters results into some undefined behavior like infinite loop etc. 假设有一些无效的输入(例如您的方法仅设计用于正整数,并且您通过了其他操作),该方法要么抛出某些异常(作为验证的一部分,否则)将结果输入一些未定义的行为中,例如无限循环等

So for these kinds of input (invalid input) if you want to return a value indicating the same then it does not break the binary compatibility is what the JLS is saying I think. 因此,对于这些类型的输入(无效输入),如果您想返回一个指示相同值的值,那么它不会破坏二进制兼容性,这是JLS所说的。 Eg. 例如。 you want to return -1 or something like that to indicate the passage of obsolete arguments (rather than throwing or having an undefined behavior). 您想返回-1或类似的值来指示过时参数的传递(而不是抛出或具有未定义的行为)。 It's implementation specific and depends how you want to handle such inputs. 它是特定于实现的,并且取决于您如何处理此类输入。

Changing methods or constructors to return values on inputs for which they previously either threw exceptions that normally should not occur 更改方法或构造函数以在其之前抛出通常不应该发生的异常的输入上返回值

Existing code: 现有代码:

public int square(int x) {
    if (x % 2 == 0) {
        return x * x;
    } else {
        throw new IllegalArgumentException("Cannot square odd numbers");
    }
}

Example of a compatible change that satisfies above rule: 满足上述规则的兼容更改的示例:

public int square(int x) {
    return x * x;
}

Example of an incompatible change: 不兼容更改的示例:

public int square(int x) {
    if (x % 2 == 1) {
        return x * x;
    } else {
        throw new IllegalArgumentException("Cannot square even numbers");
    }
}

or failed by going into an infinite loop or causing a deadlock 或因陷入无限循环或导致死锁而失败

Before: 之前:

public int square(int x) {
    if (x % 2 == 0) {
        return x * x;
    } else {
        while (true)
            ;
    }
}

Example of a compatible change that satisfies above rule: 满足上述规则的兼容更改的示例:

public int square(int x) {
    if (x % 2 == 0) {
        return x * x;
    } else {
        // Even this is binary compatible (although bad form as the method
        // name is no longer self-explanatory.)
        return x * x * x;
    }
}

I think you get the picture. 我想你明白了。

The practical meaning of the statements is: 这些语句的实际含义是:

  • You can add functionality that makes your method do something it couldn't do before successfully 您可以添加使您的方法成功完成之前无法完成的功能
  • But you cannot change existing behaviour for already-valid inputs while remaining binary compatible. 但是,在保持二进制兼容的同时,您不能更改已经有效的输入的现有行为。

It's a fancy way of saying something that make a lot of common sense. 这是一种表达很多常识的奇特方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM