Bug 28869 - Parse error with final local vars without immediate assignment
Summary: Parse error with final local vars without immediate assignment
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.1 M5   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 31070 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-12-25 06:09 EST by Arnd Beissner CLA
Modified: 2003-02-07 12:21 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Arnd Beissner CLA 2002-12-25 06:09:27 EST
Consider this prototype code:

void foo()
{
  final PreparedStatement statement;

  try
  {
    statement = xxx.prepareStatement(...)
  }
  catch (..)
  {
    // handle errors
  }

  return statement;
}

For this kind of thing I often use a final local variable - mostly to enable the
compiler to find my errors (like assigning more than once). However, with
Eclipse I get parse errors (cursor on the line that declares the local var) when
 using refactoring functions, such as "Reorganize imports".

The Eclipse version used is Eclipse 2.1 M4.
Comment 1 Dirk Baeumer CLA 2003-01-07 10:22:05 EST
Test it with the following example under M4

public class A {
	public int foo() {
		final int value;
		try
		{
		  value= init();
		}
		catch(Exception e)
		{
		  // handle errors
		}

		return value;
	}
	public int init() {
		return 10;
	}
}

Activating Organize imports claims that the CU has parse errors. 

The refactoring actions claim that the variable value may not have been 
initialized which is the correct behaviour.

Martin, can you comment on the organize import case.
Comment 2 Martin Aeschlimann CLA 2003-01-07 12:13:13 EST
Fixed it for 'organize imports'. Only syntax errors should stop the action.
> 20030107

Add Philippe as CC: The bug report says other compilers tolerate the code when 
the variable is set to final, eclipse doesn't. Who's right?
Comment 3 Arnd Beissner CLA 2003-01-07 13:06:31 EST
Thanks for caring, guys.

To simplify the situation (the rules for definitive assignment really *ARE* 
complicated with try/catch/finally), here's a more obvious example.

Eclipse also complains with:

public void foo()
{
  final int bar;

  bar = 1;
}

There's no conditionality involved. My impression is that you always get parse 
errors when a local final variable isn't immediately assigned. However, this is 
really ok - it's an explicit g. Actually, I found this feature of Java while 
reading the Java language reference. For reference, take the example in the 
Java Language Specification, Second Edition (English), page 409, example 
method "unflow".
Comment 4 Philipe Mulet CLA 2003-01-08 04:22:17 EST
There is no parse error at all in the original code sample, only an issue with 
definite assignment (which doesn't prevent parsing). Suspecting some 
inconsistency in the DOM AST.
Comment 5 Philipe Mulet CLA 2003-01-08 05:30:38 EST
Moving to JDT/Core for further investigation on the DOM AST conversion.
It seems fooled by the extra modifier bit: AccBlankFinal (which it shouldn't 
consider).
Comment 6 Philipe Mulet CLA 2003-01-08 05:36:50 EST
AccBlankFinal got introduced recently, and DOM AST didn't get updated 
accordingly. 

Fixed in latest.
Comment 7 Olivier Thomann CLA 2003-01-08 09:09:10 EST
Regression test added.
Comment 8 Olivier Thomann CLA 2003-02-06 11:09:05 EST
*** Bug 31070 has been marked as a duplicate of this bug. ***
Comment 9 David Audel CLA 2003-02-07 12:21:45 EST
Verified.