Issue about “binary compatibility”

拟墨画扇 提交于 2019-12-11 00:16:07

问题


When I read Java Language Specification (JLS8) > Binary Compatibility, one of a set changes that doesn't break binary compatibility is:

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.


回答1:


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.




回答2:


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

here is a list of some important binary compatible changes that the Java programming language supports:

.....
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. 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). It's implementation specific and depends how you want to handle such inputs.



来源:https://stackoverflow.com/questions/45930494/issue-about-binary-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!