Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 97413 - [extract method] missing return type when code can throw exception
Summary: [extract method] missing return type when code can throw exception
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M1   Edit
Assignee: Benjamin Muskalla CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-31 06:10 EDT by Christof Marti CLA
Modified: 2009-08-04 06:42 EDT (History)
2 users (show)

See Also:


Attachments
patch + testcase (3.93 KB, patch)
2009-07-04 18:51 EDT, Benjamin Muskalla CLA
markus.kell.r: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Christof Marti CLA 2005-05-31 06:10:09 EDT
I20050527-1300

Extracting code that can return a value and ends with throwing an exception
results in a new method with no return type - and therefore compile errors. No
other errors or exceptions.

Before:
class Foo {
	int bar(int bar) {
		if (bar > 0)
			return bar - 1;
		throw new IllegalArgumentException();
	}
}

After:
class Foo {
	int bar(int bar) {
		foo(bar);
	}

	private void foo(int bar) {
		if (bar > 0)
			return bar - 1;
		throw new IllegalArgumentException();
	}
}

Swapping the return and the throw statements works.

Before:
class Foo {
	int bar(int bar) {
		if (bar > 0)
			throw new IllegalArgumentException();
		return bar - 1;
	}
}

After:
class Foo {
	int bar(int bar) {
		return foo(bar);
	}

	private int foo(int bar) {
		if (bar > 0)
			throw new IllegalArgumentException();
		return bar - 1;
	}
}
Comment 1 Benjamin Muskalla CLA 2009-07-04 18:51:25 EDT
Created attachment 140808 [details]
patch + testcase

Currently the two "branches" return a Partial return (if without else) and a throw.
As a combination of this
Comment 2 Benjamin Muskalla CLA 2009-07-04 18:53:21 EDT
Too early to submit :|

In combination (throw+partial return) always returns a value or throws a exception we always have a return value. The return kind table was adjusted accordingly.
Comment 3 Markus Keller CLA 2009-07-13 13:08:19 EDT
Thanks, released to HEAD and fixed the tests (there was just 1 test, without corresponding 'out', and the test was only for the case that already worked).
Comment 4 Raksha Vasisht CLA 2009-08-04 04:43:06 EDT
Verified for 3.6 M1 using I20090803-1800