Could a final variable be reassigned in catch, even if assignment is last operation in try?

前端 未结 12 1130
粉色の甜心
粉色の甜心 2020-12-04 18:49

I am quite convinced that here

final int i;
try { i = calculateIndex(); }
catch (Exception e) { i = 1; }

i cannot possibly have

相关标签:
12条回答
  • 2020-12-04 19:44

    EDITING RESPONSE BASED UPON QUESTIONS FROM OP

    This is really in response to the comment:

    All you have done is written up a clear-cut example of a straw man argument: you are vicariously introducing the tacit assumption that there must always be one and only one default value, valid for all call sites

    I believe that we are approaching the entire question from opposite ends. It seems that you are looking at it from the bottom up - literally from the bytecode and going up to the Java. If this is not true, you are looking at it from the "code" compliance to the spec.

    Approaching this from the opposite direction, from the "design" down, I see problems. I think it was M. Fowler who collected various "bad smells" into the book: "Refactoring: Improving the Design of Existing Code". Here (and probably many, many other places) the "Extract Method" refactoring is described.

    Thus, if I imagine a made-up version of your code without the 'calculateIndex' method, I might have something like this:

    public void someMethod() {
        final int i;
        try {
            int intermediateVal = 35;
            intermediateVal += 56;
            i = intermediateVal*3;
        } catch (Exception e) {
            // would like to be able to set i = 1 here;
        }
    }
    

    Now, the above COULD have been refactored as originally posted with a 'calculateIndex' method. However, if the 'Extract Method' Refactoring defined by Fowler is completely applied, then one gets this [note: dropping the 'e' is intentional to differentiate from your method.]

    public void someMethod() {
        final int i =  calculateIndx();
    }
    
    private int calculateIndx() {
        try {
            int intermediateVal = 35;
            intermediateVal += 56;
            return intermediateVal*3;
        } catch (Exception e) {
            return 1;  // or other default values or other way of setting
        }
    }
    

    So from the 'design' perspective the problem is the code you have. Your 'calculateIndex' method does NOT calculate the index. It only does sometimes. The rest of the time, the exception handler does the calculation.

    Furthermore, this refactoring is far more accommodating to changes. For instance, if you have to change what I assumed was the default value of '1' to a '2', no big deal. However, as pointed out by the OP reply quoted, one cannot assume that there is only one default value. If the logic to set this grows to be only slightly complex it could still easily reside in the encapsulated exception handler. However, at some point, it too may need to be refactored into it's own method. Both cases still allow the encapsulated method to perform it's function and truly calculate the index.

    In summary, when I get here and look at what I believe is the correct code, then there is no compiler issue for discussion. (I am most certain you will not agree: that is fine, I just want to be clearer about my viewpoint.) As for compiler warnings that come up for incorrect code, those help me realize in the first place that something is wrong. In this case, that refactoring is needed.

    0 讨论(0)
  • 2020-12-04 19:44

    I faced EXACTLY the same problem Mario, and read this very interresting discussion. I just solved my issue by that:

    private final int i;
    
    public Byte(String hex) {
        int calc;
        try {
            calc = Integer.parseInt(hex, 16);
        } catch (NumberFormatException e) {
            calc = 0;
        }
        finally {
          i = calc;
        }
    }
    

    @Joeg, I must admit that I liked a lot your post about design, especially that sentence: calculateIndx() calculates sometimes the index, but could we say the same about parseInt() ? Isn't that also the role of calculateIndex() to throw and thus not calculate the index when it is not possible, and then making it returning a wrong value (1 is arbitrary in your refactoring) is imho bad.

    @Marko, I didn't understand your reply to Joeg about the AFTER line 4 and BEFORE line 5... I'm not strong enough yet in java world (25y of c++ but only 1 in java...), but I thing this case is one where the compiler is right : i could be initialized twice in Joeg's case.

    [All what I'm saying is a very very humble opinion]

    0 讨论(0)
  • 2020-12-04 19:46

    I think there is one situation where this model act as life saver. Consider the code given below:

    final Integer i;
    try
    {
        i = new Integer(10);----->(1)
    }catch(Exception ex)
    {
        i = new Integer(20);
    }
    

    Now Consider the line (1). Most of the JIT compilers creates object in following sequence(psuedo code):

    mem = allocate();   //Allocate memory 
    ctorInteger(instance);//Invoke constructor for Singleton passing instance.
    i = mem;        //Make instance i non-null
    

    But, some JIT compilers does out of order writes. And above steps is reordered as follows:

    mem = allocate();   //Allocate memory 
    i = mem;        //Make instance i non-null
    ctorInteger(instance);  //Invoke constructor for Singleton passing instance.
    

    Now suppose, the JIT performs out of order writes while creating the object in line (1). And suppose an exception is thrown while executing the constructor. In that case, the catch block will have i which is not null . If JVM doesn't follow this modal then in this case final variable is allowed to be assigned twice!!!

    0 讨论(0)
  • 2020-12-04 19:49

    JLS hunting:

    It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

    Quoth chapter 16:

    V is definitely unassigned before a catch block iff all of the following conditions hold:

    V is definitely unassigned after the try block.
    V is definitely unassigned before every return statement that belongs to the try block.
    V is definitely unassigned after e in every statement of the form throw e that belongs to the try block.
    V is definitely unassigned after every assert statement that occurs in the try block.
    V is definitely unassigned before every break statement that belongs to the try block and whose break target contains (or is) the try statement.
    V is definitely unassigned before every continue statement that belongs to the try block and whose continue target contains the try statement.

    Bold is mine. After the try block it is unclear whether i is assigned.

    Furthermore in the example

    final int i;
    try {
        i = foo();
        bar();
    }
    catch(Exception e) { // e might come from bar
        i = 1;
    }
    

    The bold text is the only condition preventing the actual erroneous assignment i=1 from being illegal. So this is sufficient to prove that a finer condition of "definitely unassigned" is necessary to allow the code in your original post.

    If the spec were revised to replace this condition with

    V is definitely unassigned after the try block, if the catch block catches an unchecked exception.
    V is definitely unassigned before the last statement capable of throwing an exception of a type caught by the catch block, if the catch block catches an unchecked exception.

    Then I believe your code would be legal. (To the best of my ad-hoc analysis.)

    I submitted a JSR for this, which I expect to be ignored but I was curious to see how these are handled. Technically fax number is a required field, I hope it won't do too much damage if I entered +1-000-000-000 there.

    0 讨论(0)
  • 2020-12-04 19:51

    This is a summary of the strongest arguments in favor of the thesis that the current rules for definite assignment cannot be relaxed without breaking consistency (A), followed by my counterarguments (B):

    • A: on the bytecode level the write to the variable is not the last instruction within the try-block: for example, the last instruction will typically be a goto jump over the exception handling code;

    • B: but if the rules state that i is definitely unassigned within the catch-block, its value may not be observed. An unobservable value is as good as no value;

    • A: even if the compiler declares i as definitely unassigned, a debug tool could still see the value;

    • B: in fact, a debug tool could always access an uninitialized local variable, which will on a typical implementation have any arbitrary value. There is no essential difference between an uninitialized variable and a variable whose initialization completed abruptly after the actual write having occurred. Regardless of the special case under consideration here, the tool must always use additional metadata to know for each local variable the range of instructions where that variable is definitely assigned and only allow its value to be observed while execution finds itself within the range.

    Final Conclusion:

    The specification could consistently receive more fine-grained rules which would allow my posted example to compile.

    0 讨论(0)
  • 2020-12-04 19:52

    But i may be assigned twice

        int i;
        try {
            i = calculateIndex();  // suppose func returns true
            System.out.println("i=" + i);
            throw new IOException();
        } catch (IOException e) {
            i = 1;
            System.out.println("i=" + i);
        }
    

    output

    i=0
    i=1
    

    and it means it cannot be final

    0 讨论(0)
提交回复
热议问题