Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 350308 - [1.7][quick fix] Smarter 'Combine catch blocks'
Summary: [1.7][quick fix] Smarter 'Combine catch blocks'
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Deepak Azad CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-24 13:37 EDT by Markus Keller CLA
Modified: 2019-04-13 01:17 EDT (History)
1 user (show)

See Also:


Attachments
testcase (1.05 KB, application/octet-stream)
2011-10-17 05:42 EDT, Ayushman Jain CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2011-06-24 13:37:40 EDT
BETA_JAVA7

The 'Convert to a single multi-catch block' quick fix should be a bit smarter.

From the selected catch clause it should go forward and backward to collect all other catch clauses that have the same block and form a continuous sequence that includes the selected clause.

E.g. here, it should allow me to merge all but the first catch clause:

package multicatch;

import java.lang.reflect.InvocationTargetException;

public class Reflect {
	public static void main(String[] args) {
		try {
			String.class.getConstructor().newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
Comment 1 Deepak Azad CLA 2011-06-24 13:50:13 EDT
(In reply to comment #0)
> The 'Convert to a single multi-catch block' quick fix should be a bit smarter.
> 
> From the selected catch clause it should go forward and backward to collect all
> other catch clauses that have the same block and form a continuous sequence
> that includes the selected clause.
I expected someone to ask for this :) 

Another possible improvement - Currently the 'sameness' is computed by doing a string comparison of the source of catch blocks (the same is also used by join if statements with || quick assist), as a result when the 2 catch blocks differ only in the exception variable name the quick assist is not offered.

e.g.
        try {
            String.class.getConstructor().newInstance();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalArgumentException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
Comment 2 Markus Keller CLA 2011-06-24 14:05:24 EDT
(In reply to comment #1)
Yeah, I saw that, but that's probably a more complex implementation than what we want to risk for 3.7.1. The SnippetFinder implements something like this for Extract Method.

But another problem we should also fix is this:

		try {
			FileReader fileReader = new FileReader("");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

The spec says: "It is a compile-time error if a disjunctive type contains two alternatives Di, Dj where Di is a subtype of Dj."

=> We should just remove a type if it is a subtype of another one. If we end up with just a simple one-type catch block, that's also OK.
Comment 3 Ayushman Jain CLA 2011-10-17 05:42:36 EDT
Created attachment 205302 [details]
testcase

There's another annoying problem with this quick assist. It only shows up when the content of mutiple catch blocks are exactly same(apart from whitespace/tab differences).

In the attached class, foo and foo2 are same, yet I see the "combine catch blocks" assist on foo2 and not on foo. Only after scratching my head for a few mins did I realise that there may be a whitespace difference. Indeed, in foo, the two catch blocks were differentiated by tabs and spaces. :(
Comment 4 Eclipse Genie CLA 2019-04-13 01:17:57 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.