Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 348061
Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java (+2 lines)
Lines 45-50 Link Here
45
	public static String QuickAssistProcessor_convert_anonym_to_nested;
45
	public static String QuickAssistProcessor_convert_anonym_to_nested;
46
	public static String QuickAssistProcessor_convert_local_to_field_description;
46
	public static String QuickAssistProcessor_convert_local_to_field_description;
47
	public static String QuickAssistProcessor_convert_to_message_format;
47
	public static String QuickAssistProcessor_convert_to_message_format;
48
	public static String QuickAssistProcessor_convert_to_multiple_singletype_catch_blocks;
49
	public static String QuickAssistProcessor_convert_to_single_multicatch_block;
48
	public static String QuickAssistProcessor_convert_to_string_buffer_description;
50
	public static String QuickAssistProcessor_convert_to_string_buffer_description;
49
	public static String QuickAssistProcessor_extract_to_constant_description;
51
	public static String QuickAssistProcessor_extract_to_constant_description;
50
	public static String QuickAssistProcessor_inline_local_description;
52
	public static String QuickAssistProcessor_inline_local_description;
(-)ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties (+2 lines)
Lines 327-332 Link Here
327
QuickAssistProcessor_typetoarrayInitializer_description=Add type to initializer
327
QuickAssistProcessor_typetoarrayInitializer_description=Add type to initializer
328
QuickAssistProcessor_convert_local_to_field_description=Convert local variable to field
328
QuickAssistProcessor_convert_local_to_field_description=Convert local variable to field
329
QuickAssistProcessor_convert_to_message_format=Use 'MessageFormat' for string concatenation
329
QuickAssistProcessor_convert_to_message_format=Use 'MessageFormat' for string concatenation
330
QuickAssistProcessor_convert_to_multiple_singletype_catch_blocks=Convert to multiple single-type catch blocks
331
QuickAssistProcessor_convert_to_single_multicatch_block=Convert to a single multi-catch block
330
QuickAssistProcessor_convert_to_string_buffer_description=Use ''{0}'' for string concatenation
332
QuickAssistProcessor_convert_to_string_buffer_description=Use ''{0}'' for string concatenation
331
QuickAssistProcessor_createmethodinsuper_description=Create ''{1}()'' in super type ''{0}''
333
QuickAssistProcessor_createmethodinsuper_description=Create ''{1}()'' in super type ''{0}''
332
334
(-)ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java (+126 lines)
Lines 5-10 Link Here
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * This is an implementation of an early-draft specification developed under the Java
9
 * Community Process (JCP) and is made available for testing and evaluation purposes
10
 * only. The code is not compatible with any specification of the JCP.
11
 *
8
 * Contributors:
12
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
13
 *     IBM Corporation - initial API and implementation
10
 *     Sebastian Davids <sdavids@gmx.de> - Bug 37432 getInvertEqualsProposal
14
 *     Sebastian Davids <sdavids@gmx.de> - Bug 37432 getInvertEqualsProposal
Lines 40-50 Link Here
40
import org.eclipse.ltk.core.refactoring.TextFileChange;
44
import org.eclipse.ltk.core.refactoring.TextFileChange;
41
45
42
import org.eclipse.jdt.core.Flags;
46
import org.eclipse.jdt.core.Flags;
47
import org.eclipse.jdt.core.IBuffer;
43
import org.eclipse.jdt.core.ICompilationUnit;
48
import org.eclipse.jdt.core.ICompilationUnit;
44
import org.eclipse.jdt.core.IJavaElement;
49
import org.eclipse.jdt.core.IJavaElement;
45
import org.eclipse.jdt.core.IJavaModelMarker;
50
import org.eclipse.jdt.core.IJavaModelMarker;
46
import org.eclipse.jdt.core.IType;
51
import org.eclipse.jdt.core.IType;
47
import org.eclipse.jdt.core.JavaCore;
52
import org.eclipse.jdt.core.JavaCore;
53
import org.eclipse.jdt.core.JavaModelException;
48
import org.eclipse.jdt.core.NamingConventions;
54
import org.eclipse.jdt.core.NamingConventions;
49
import org.eclipse.jdt.core.compiler.IProblem;
55
import org.eclipse.jdt.core.compiler.IProblem;
50
import org.eclipse.jdt.core.dom.AST;
56
import org.eclipse.jdt.core.dom.AST;
Lines 94-99 Link Here
94
import org.eclipse.jdt.core.dom.TryStatement;
100
import org.eclipse.jdt.core.dom.TryStatement;
95
import org.eclipse.jdt.core.dom.Type;
101
import org.eclipse.jdt.core.dom.Type;
96
import org.eclipse.jdt.core.dom.TypeDeclaration;
102
import org.eclipse.jdt.core.dom.TypeDeclaration;
103
import org.eclipse.jdt.core.dom.UnionType;
97
import org.eclipse.jdt.core.dom.VariableDeclaration;
104
import org.eclipse.jdt.core.dom.VariableDeclaration;
98
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
105
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
99
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
106
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
Lines 182-187 Link Here
182
		ASTNode coveringNode= context.getCoveringNode();
189
		ASTNode coveringNode= context.getCoveringNode();
183
		if (coveringNode != null) {
190
		if (coveringNode != null) {
184
			return getCatchClauseToThrowsProposals(context, coveringNode, null)
191
			return getCatchClauseToThrowsProposals(context, coveringNode, null)
192
				|| getConvertToMultiCatchProposals(context, coveringNode, null)
193
				|| getUnrollMultiCatchProposals(context, coveringNode, null)
185
				|| getRenameLocalProposals(context, coveringNode, null, null)
194
				|| getRenameLocalProposals(context, coveringNode, null, null)
186
				|| getRenameRefactoringProposal(context, coveringNode, null, null)
195
				|| getRenameRefactoringProposal(context, coveringNode, null, null)
187
				|| getAssignToVariableProposals(context, coveringNode, null, null)
196
				|| getAssignToVariableProposals(context, coveringNode, null, null)
Lines 225-230 Link Here
225
			if (noErrorsAtLocation) {
234
			if (noErrorsAtLocation) {
226
				boolean problemsAtLocation= locations.length != 0;
235
				boolean problemsAtLocation= locations.length != 0;
227
				getCatchClauseToThrowsProposals(context, coveringNode, resultingCollections);
236
				getCatchClauseToThrowsProposals(context, coveringNode, resultingCollections);
237
				getConvertToMultiCatchProposals(context, coveringNode, resultingCollections);
238
				getUnrollMultiCatchProposals(context, coveringNode, resultingCollections);
228
				getUnWrapProposals(context, coveringNode, resultingCollections);
239
				getUnWrapProposals(context, coveringNode, resultingCollections);
229
				getJoinVariableProposals(context, coveringNode, resultingCollections);
240
				getJoinVariableProposals(context, coveringNode, resultingCollections);
230
				getSplitVariableProposals(context, coveringNode, resultingCollections);
241
				getSplitVariableProposals(context, coveringNode, resultingCollections);
Lines 1253-1258 Link Here
1253
		return true;
1264
		return true;
1254
	}
1265
	}
1255
1266
1267
	private static boolean getConvertToMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
1268
		if (!JavaModelUtil.is70OrHigher(context.getCompilationUnit().getJavaProject()))
1269
			return false;
1270
1271
		if (!(covering instanceof CatchClause))
1272
			return false;
1273
		TryStatement tryStatement= (TryStatement) covering.getParent();
1274
		List<CatchClause> catchClauses= tryStatement.catchClauses();
1275
		if (catchClauses.size() <= 1)
1276
			return false;
1277
1278
		String commonSource= null;
1279
		try {
1280
			IBuffer buffer= context.getCompilationUnit().getBuffer();
1281
			for (Iterator<CatchClause> iterator= catchClauses.iterator(); iterator.hasNext();) {
1282
				CatchClause catchClause= iterator.next();
1283
				Block body= catchClause.getBody();
1284
				String source= buffer.getText(body.getStartPosition(), body.getLength());
1285
				if (commonSource == null) {
1286
					commonSource= source;
1287
				} else {
1288
					if (!commonSource.equals(source))
1289
						return false;
1290
				}
1291
			}
1292
		} catch (JavaModelException e) {
1293
			return false;
1294
		}
1295
1296
		if (resultingCollections == null)
1297
			return true;
1298
1299
		AST ast= covering.getAST();
1300
		ASTRewrite rewrite= ASTRewrite.create(ast);
1301
		CatchClause firstCatchClause= catchClauses.get(0);
1302
1303
		UnionType newUnionType= ast.newUnionType();
1304
		List<Type> types= newUnionType.types();
1305
		for (Iterator<CatchClause> iterator= catchClauses.iterator(); iterator.hasNext();) {
1306
			CatchClause catchClause= iterator.next();
1307
			Type type= catchClause.getException().getType();
1308
			if (type instanceof UnionType) {
1309
				List<Type> types2= ((UnionType) type).types();
1310
				for (Iterator<Type> iterator2= types2.iterator(); iterator2.hasNext();) {
1311
					types.add((Type) rewrite.createCopyTarget(iterator2.next()));
1312
				}
1313
			} else {
1314
				types.add((Type) rewrite.createCopyTarget(type));
1315
			}
1316
		}
1317
1318
		SingleVariableDeclaration newExceptionDeclaration= ast.newSingleVariableDeclaration();
1319
		newExceptionDeclaration.setType(newUnionType);
1320
		newExceptionDeclaration.setName((SimpleName) rewrite.createCopyTarget(firstCatchClause.getException().getName()));
1321
		rewrite.replace(firstCatchClause.getException(), newExceptionDeclaration, null);
1322
1323
		for (int i= 1; i < catchClauses.size(); i++) {
1324
			rewrite.remove(catchClauses.get(i), null);
1325
		}
1326
1327
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
1328
		String label= CorrectionMessages.QuickAssistProcessor_convert_to_single_multicatch_block;
1329
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 2, image);
1330
		resultingCollections.add(proposal);
1331
		return true;
1332
	}
1333
1334
	private static boolean getUnrollMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
1335
		if (!JavaModelUtil.is70OrHigher(context.getCompilationUnit().getJavaProject()))
1336
			return false;
1337
1338
		if (!(covering instanceof CatchClause))
1339
			return false;
1340
1341
		CatchClause catchClause= (CatchClause) covering;
1342
		SingleVariableDeclaration singleVariableDeclaration= catchClause.getException();
1343
		Type type= singleVariableDeclaration.getType();
1344
		if (!(type instanceof UnionType))
1345
			return false;
1346
1347
		if (resultingCollections == null)
1348
			return true;
1349
1350
		AST ast= covering.getAST();
1351
		ASTRewrite rewrite= ASTRewrite.create(ast);
1352
1353
		TryStatement tryStatement= (TryStatement) covering.getParent();
1354
		ListRewrite listRewrite= rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
1355
1356
		UnionType unionType= (UnionType) type;
1357
		List<Type> types= unionType.types();
1358
		for (int i= types.size() - 1; i >= 0; i--) {
1359
			Type type2= types.get(i);
1360
			CatchClause newCatchClause= ast.newCatchClause();
1361
1362
			SingleVariableDeclaration newSingleVariableDeclaration= ast.newSingleVariableDeclaration();
1363
			newSingleVariableDeclaration.setType((Type) rewrite.createCopyTarget(type2));
1364
			newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(singleVariableDeclaration.getName()));
1365
			newCatchClause.setException(newSingleVariableDeclaration);
1366
1367
			//newCatchClause#setBody() destroys the formatting, hence copy statement by statement
1368
			List<Statement> statements= catchClause.getBody().statements();
1369
			for (Iterator<Statement> iterator2= statements.iterator(); iterator2.hasNext();) {
1370
				newCatchClause.getBody().statements().add(rewrite.createCopyTarget(iterator2.next()));
1371
			}
1372
			listRewrite.insertAfter(newCatchClause, catchClause, null);
1373
		}
1374
		rewrite.remove(catchClause, null);
1375
1376
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
1377
		String label= CorrectionMessages.QuickAssistProcessor_convert_to_multiple_singletype_catch_blocks;
1378
		ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, 2, image);
1379
		resultingCollections.add(proposal);
1380
		return true;
1381
	}
1256
1382
1257
	private static boolean getRenameLocalProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
1383
	private static boolean getRenameLocalProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
1258
		if (!(node instanceof SimpleName)) {
1384
		if (!(node instanceof SimpleName)) {
(-)ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest17.java (+497 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * This is an implementation of an early-draft specification developed under the Java
9
 * Community Process (JCP) and is made available for testing and evaluation purposes
10
 * only. The code is not compatible with any specification of the JCP.
11
 *
12
 * Contributors:
13
 *     IBM Corporation - initial API and implementation
14
 *******************************************************************************/
15
package org.eclipse.jdt.ui.tests.quickfix;
16
17
import java.util.Hashtable;
18
import java.util.List;
19
20
import junit.framework.Test;
21
import junit.framework.TestSuite;
22
23
import org.eclipse.jdt.testplugin.JavaProjectHelper;
24
import org.eclipse.jdt.testplugin.TestOptions;
25
26
import org.eclipse.core.runtime.Preferences;
27
28
import org.eclipse.jface.preference.IPreferenceStore;
29
30
import org.eclipse.jdt.core.ICompilationUnit;
31
import org.eclipse.jdt.core.IJavaProject;
32
import org.eclipse.jdt.core.IPackageFragment;
33
import org.eclipse.jdt.core.IPackageFragmentRoot;
34
import org.eclipse.jdt.core.JavaCore;
35
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
36
37
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
38
import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType;
39
40
import org.eclipse.jdt.ui.PreferenceConstants;
41
import org.eclipse.jdt.ui.tests.core.Java17ProjectTestSetup;
42
import org.eclipse.jdt.ui.tests.core.ProjectTestSetup;
43
44
import org.eclipse.jdt.internal.ui.JavaPlugin;
45
import org.eclipse.jdt.internal.ui.text.correction.AssistContext;
46
47
public class AssistQuickFixTest17 extends QuickFixTest {
48
49
	private static final String CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK= "Convert to a single multi-catch block";
50
	private static final String CONVERT_TO_MULTIPLE_CATCH_BLOCKS= "Convert to multiple catch blocks";
51
52
	private static final Class THIS= AssistQuickFixTest17.class;
53
54
	private IJavaProject fJProject1;
55
56
	private IPackageFragmentRoot fSourceFolder;
57
58
	public AssistQuickFixTest17(String name) {
59
		super(name);
60
	}
61
62
	public static Test suite() {
63
		return setUpTest(new TestSuite(THIS));
64
	}
65
66
	public static Test setUpTest(Test test) {
67
		return new Java17ProjectTestSetup(test);
68
	}
69
70
	protected void setUp() throws Exception {
71
		Hashtable options= TestOptions.getDefaultOptions();
72
		options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
73
		options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
74
75
		JavaCore.setOptions(options);
76
77
		IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
78
		store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS, false);
79
		store.setValue(PreferenceConstants.CODEGEN_KEYWORD_THIS, false);
80
81
		StubUtility.setCodeTemplate(CodeTemplateContextType.METHODSTUB_ID, "//TODO\n${body_statement}", null);
82
83
		Preferences corePrefs= JavaPlugin.getJavaCorePluginPreferences();
84
		corePrefs.setValue(JavaCore.CODEASSIST_FIELD_PREFIXES, "");
85
		corePrefs.setValue(JavaCore.CODEASSIST_STATIC_FIELD_PREFIXES, "");
86
		corePrefs.setValue(JavaCore.CODEASSIST_FIELD_SUFFIXES, "");
87
		corePrefs.setValue(JavaCore.CODEASSIST_STATIC_FIELD_SUFFIXES, "");
88
89
		fJProject1= ProjectTestSetup.getProject();
90
91
		fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
92
	}
93
94
	protected void tearDown() throws Exception {
95
		JavaProjectHelper.clear(fJProject1, Java17ProjectTestSetup.getDefaultClasspath());
96
	}
97
98
	public void testConvertToMultiCatch1() throws Exception {
99
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
100
		StringBuffer buf= new StringBuffer();
101
		buf.append("package test1;\n");
102
		buf.append("public class E {\n");
103
		buf.append("    void foo() {\n");
104
		buf.append("        try {\n");
105
		buf.append("            System.out.println(\"foo\");\n");
106
		buf.append("        } catch (IllegalArgumentException ex) {\n");
107
		buf.append("            ex.printStackTrace();\n");
108
		buf.append("        } catch (NullPointerException ex) {\n");
109
		buf.append("            ex.printStackTrace();\n");
110
		buf.append("        }\n");
111
		buf.append("    }\n");
112
		buf.append("}\n");
113
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
114
115
		int offset= buf.toString().indexOf("catch");
116
		AssistContext context= getCorrectionContext(cu, offset, 0);
117
		assertNoErrors(context);
118
		List proposals= collectAssists(context, false);
119
120
		assertCorrectLabels(proposals);
121
122
		buf= new StringBuffer();
123
		buf.append("package test1;\n");
124
		buf.append("public class E {\n");
125
		buf.append("    void foo() {\n");
126
		buf.append("        try {\n");
127
		buf.append("            System.out.println(\"foo\");\n");
128
		buf.append("        } catch (IllegalArgumentException | NullPointerException ex) {\n");
129
		buf.append("            ex.printStackTrace();\n");
130
		buf.append("        }\n");
131
		buf.append("    }\n");
132
		buf.append("}\n");
133
		String expected1= buf.toString();
134
135
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
136
	}
137
138
	public void testConvertToMultiCatch2() throws Exception {
139
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
140
		StringBuffer buf= new StringBuffer();
141
		buf.append("package test1;\n");
142
		buf.append("public class E {\n");
143
		buf.append("    void foo() {\n");
144
		buf.append("        try {\n");
145
		buf.append("            System.out.println(\"foo\");\n");
146
		buf.append("        } catch (IllegalArgumentException e) {\n");
147
		buf.append("            e.printStackTrace();\n");
148
		buf.append("        } catch (NullPointerException e) {\n");
149
		buf.append("            e.printStackTrace();\n");
150
		buf.append("        }\n");
151
		buf.append("    }\n");
152
		buf.append("}\n");
153
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
154
155
		int offset= buf.toString().indexOf("catch");
156
		AssistContext context= getCorrectionContext(cu, offset, 0);
157
		assertNoErrors(context);
158
		List proposals= collectAssists(context, false);
159
160
		assertCorrectLabels(proposals);
161
162
		buf= new StringBuffer();
163
		buf.append("package test1;\n");
164
		buf.append("public class E {\n");
165
		buf.append("    void foo() {\n");
166
		buf.append("        try {\n");
167
		buf.append("            System.out.println(\"foo\");\n");
168
		buf.append("        } catch (IllegalArgumentException | NullPointerException e) {\n");
169
		buf.append("            e.printStackTrace();\n");
170
		buf.append("        }\n");
171
		buf.append("    }\n");
172
		buf.append("}\n");
173
		String expected1= buf.toString();
174
175
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
176
177
	}
178
179
	public void testConvertToMultiCatch3() throws Exception {
180
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
181
		StringBuffer buf= new StringBuffer();
182
		buf.append("package test1;\n");
183
		buf.append("public class E {\n");
184
		buf.append("    void foo() {\n");
185
		buf.append("        try {\n");
186
		buf.append("            System.out.println(\"foo\");\n");
187
		buf.append("        } catch (IllegalArgumentException | NullPointerException e) {\n");
188
		buf.append("            e.printStackTrace();\n");
189
		buf.append("        } catch (RuntimeException e) {\n");
190
		buf.append("            e.printStackTrace();\n");
191
		buf.append("        }\n");
192
		buf.append("    }\n");
193
		buf.append("}\n");
194
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
195
196
		int offset= buf.toString().indexOf("catch");
197
		AssistContext context= getCorrectionContext(cu, offset, 0);
198
		assertNoErrors(context);
199
		List proposals= collectAssists(context, false);
200
201
		assertCorrectLabels(proposals);
202
203
		buf= new StringBuffer();
204
		buf.append("package test1;\n");
205
		buf.append("public class E {\n");
206
		buf.append("    void foo() {\n");
207
		buf.append("        try {\n");
208
		buf.append("            System.out.println(\"foo\");\n");
209
		buf.append("        } catch (IllegalArgumentException | NullPointerException | RuntimeException e) {\n");
210
		buf.append("            e.printStackTrace();\n");
211
		buf.append("        }\n");
212
		buf.append("    }\n");
213
		buf.append("}\n");
214
		String expected1= buf.toString();
215
216
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
217
	}
218
219
	public void testConvertToMultiCatch4() throws Exception {
220
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
221
		StringBuffer buf= new StringBuffer();
222
		buf.append("package test1;\n");
223
		buf.append("public class E {\n");
224
		buf.append("    void foo() {\n");
225
		buf.append("        try {\n");
226
		buf.append("            System.out.println(\"foo\");\n");
227
		buf.append("        } catch (IllegalArgumentException e) {\n");
228
		buf.append("            e.printStackTrace();\n");
229
		buf.append("        } catch (NullPointerException e) {\n");
230
		buf.append("            \n");
231
		buf.append("        }\n");
232
		buf.append("    }\n");
233
		buf.append("}\n");
234
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
235
236
		int offset= buf.toString().indexOf("catch");
237
		AssistContext context= getCorrectionContext(cu, offset, 0);
238
		assertNoErrors(context);
239
		List proposals= collectAssists(context, false);
240
241
		assertProposalDoesNotExist(proposals, CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK);
242
	}
243
244
	public void testConvertToMultiCatch5() throws Exception {
245
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
246
		StringBuffer buf= new StringBuffer();
247
		buf.append("package test1;\n");
248
		buf.append("public class E {\n");
249
		buf.append("    void foo() {\n");
250
		buf.append("        try {\n");
251
		buf.append("            System.out.println(\"foo\");\n");
252
		buf.append("        } catch (IllegalArgumentException e) {\n");
253
		buf.append("            e.printStackTrace();\n");
254
		buf.append("        }\n");
255
		buf.append("    }\n");
256
		buf.append("}\n");
257
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
258
259
		int offset= buf.toString().indexOf("catch");
260
		AssistContext context= getCorrectionContext(cu, offset, 0);
261
		assertNoErrors(context);
262
		List proposals= collectAssists(context, false);
263
264
		assertProposalDoesNotExist(proposals, CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK);
265
	}
266
267
	public void testConvertToMultiCatch6() throws Exception {
268
		//Quick assist should not be offered in 1.5 mode
269
		JavaProjectHelper.set15CompilerOptions(fJProject1);
270
		try {
271
			IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
272
			StringBuffer buf= new StringBuffer();
273
			buf.append("package test1;\n");
274
			buf.append("public class E {\n");
275
			buf.append("    void foo() {\n");
276
			buf.append("        try {\n");
277
			buf.append("            System.out.println(\"foo\");\n");
278
			buf.append("        } catch (IllegalArgumentException e) {\n");
279
			buf.append("            e.printStackTrace();\n");
280
			buf.append("        } catch (NullPointerException e) {\n");
281
			buf.append("            e.printStackTrace();\n");
282
			buf.append("        }\n");
283
			buf.append("    }\n");
284
			buf.append("}\n");
285
			ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
286
287
			int offset= buf.toString().indexOf("catch");
288
			AssistContext context= getCorrectionContext(cu, offset, 0);
289
			assertNoErrors(context);
290
			List proposals= collectAssists(context, false);
291
292
			assertProposalDoesNotExist(proposals, CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK);
293
		} finally {
294
			JavaProjectHelper.set17CompilerOptions(fJProject1);
295
		}
296
	}
297
298
	public void testUnrollMultiCatch1() throws Exception {
299
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
300
		StringBuffer buf= new StringBuffer();
301
		buf.append("package test1;\n");
302
		buf.append("public class E {\n");
303
		buf.append("    void foo() {\n");
304
		buf.append("        try {\n");
305
		buf.append("            System.out.println(\"foo\");\n");
306
		buf.append("        } catch (IllegalArgumentException | NullPointerException ex) {\n");
307
		buf.append("            ex.printStackTrace();\n");
308
		buf.append("        }\n");
309
		buf.append("    }\n");
310
		buf.append("}\n");
311
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
312
313
		int offset= buf.toString().indexOf("catch");
314
		AssistContext context= getCorrectionContext(cu, offset, 0);
315
		assertNoErrors(context);
316
		List proposals= collectAssists(context, false);
317
318
		assertCorrectLabels(proposals);
319
320
		buf= new StringBuffer();
321
		buf.append("package test1;\n");
322
		buf.append("public class E {\n");
323
		buf.append("    void foo() {\n");
324
		buf.append("        try {\n");
325
		buf.append("            System.out.println(\"foo\");\n");
326
		buf.append("        } catch (IllegalArgumentException ex) {\n");
327
		buf.append("            ex.printStackTrace();\n");
328
		buf.append("        } catch (NullPointerException ex) {\n");
329
		buf.append("            ex.printStackTrace();\n");
330
		buf.append("        }\n");
331
		buf.append("    }\n");
332
		buf.append("}\n");
333
		String expected1= buf.toString();
334
335
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
336
	}
337
338
	public void testUnrollMultiCatch2() throws Exception {
339
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
340
		StringBuffer buf= new StringBuffer();
341
		buf.append("package test1;\n");
342
		buf.append("public class E {\n");
343
		buf.append("    void foo() {\n");
344
		buf.append("        try {\n");
345
		buf.append("            System.out.println(\"foo\");\n");
346
		buf.append("        } catch (IllegalArgumentException | NullPointerException e) {\n");
347
		buf.append("            e.printStackTrace();\n");
348
		buf.append("        } catch (RuntimeException e) {\n");
349
		buf.append("            e.printStackTrace();\n");
350
		buf.append("        }\n");
351
		buf.append("    }\n");
352
		buf.append("}\n");
353
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
354
355
		int offset= buf.toString().indexOf("catch");
356
		AssistContext context= getCorrectionContext(cu, offset, 0);
357
		assertNoErrors(context);
358
		List proposals= collectAssists(context, false);
359
360
		assertCorrectLabels(proposals);
361
362
		buf= new StringBuffer();
363
		buf.append("package test1;\n");
364
		buf.append("public class E {\n");
365
		buf.append("    void foo() {\n");
366
		buf.append("        try {\n");
367
		buf.append("            System.out.println(\"foo\");\n");
368
		buf.append("        } catch (IllegalArgumentException e) {\n");
369
		buf.append("            e.printStackTrace();\n");
370
		buf.append("        } catch (NullPointerException e) {\n");
371
		buf.append("            e.printStackTrace();\n");
372
		buf.append("        } catch (RuntimeException e) {\n");
373
		buf.append("            e.printStackTrace();\n");
374
		buf.append("        }\n");
375
		buf.append("    }\n");
376
		buf.append("}\n");
377
		String expected1= buf.toString();
378
379
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
380
	}
381
382
	public void testUnrollMultiCatch3() throws Exception {
383
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
384
		StringBuffer buf= new StringBuffer();
385
		buf.append("package test1;\n");
386
		buf.append("public class E {\n");
387
		buf.append("    void foo() {\n");
388
		buf.append("        try {\n");
389
		buf.append("            System.out.println(\"foo\");\n");
390
		buf.append("        } catch (IllegalArgumentException e) {\n");
391
		buf.append("            e.printStackTrace();\n");
392
		buf.append("        } catch (NullPointerException | ClassCastException e) {\n");
393
		buf.append("            e.printStackTrace();\n");
394
		buf.append("        }\n");
395
		buf.append("    }\n");
396
		buf.append("}\n");
397
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
398
399
		int offset= buf.toString().indexOf("catch (NullPointerException");
400
		AssistContext context= getCorrectionContext(cu, offset, 0);
401
		assertNoErrors(context);
402
		List proposals= collectAssists(context, false);
403
404
		assertCorrectLabels(proposals);
405
406
		buf= new StringBuffer();
407
		buf.append("package test1;\n");
408
		buf.append("public class E {\n");
409
		buf.append("    void foo() {\n");
410
		buf.append("        try {\n");
411
		buf.append("            System.out.println(\"foo\");\n");
412
		buf.append("        } catch (IllegalArgumentException e) {\n");
413
		buf.append("            e.printStackTrace();\n");
414
		buf.append("        } catch (NullPointerException e) {\n");
415
		buf.append("            e.printStackTrace();\n");
416
		buf.append("        } catch (ClassCastException e) {\n");
417
		buf.append("            e.printStackTrace();\n");
418
		buf.append("        }\n");
419
		buf.append("    }\n");
420
		buf.append("}\n");
421
		String expected1= buf.toString();
422
423
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
424
	}
425
426
	public void testUnrollMultiCatch4() throws Exception {
427
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
428
		StringBuffer buf= new StringBuffer();
429
		buf.append("package test1;\n");
430
		buf.append("public class E {\n");
431
		buf.append("    void foo() {\n");
432
		buf.append("        try {\n");
433
		buf.append("            System.out.println(\"foo\");\n");
434
		buf.append("        } catch (IllegalArgumentException e) {\n");
435
		buf.append("            e.printStackTrace();\n");
436
		buf.append("        } catch (NullPointerException | ClassCastException e) {\n");
437
		buf.append("            e.printStackTrace();\n");
438
		buf.append("        } catch (ArrayIndexOutOfBoundsException e) {\n");
439
		buf.append("            e.printStackTrace();\n");
440
		buf.append("        }\n");
441
		buf.append("    }\n");
442
		buf.append("}\n");
443
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
444
445
		int offset= buf.toString().indexOf("catch (NullPointerException");
446
		AssistContext context= getCorrectionContext(cu, offset, 0);
447
		assertNoErrors(context);
448
		List proposals= collectAssists(context, false);
449
450
		assertCorrectLabels(proposals);
451
452
		buf= new StringBuffer();
453
		buf.append("package test1;\n");
454
		buf.append("public class E {\n");
455
		buf.append("    void foo() {\n");
456
		buf.append("        try {\n");
457
		buf.append("            System.out.println(\"foo\");\n");
458
		buf.append("        } catch (IllegalArgumentException e) {\n");
459
		buf.append("            e.printStackTrace();\n");
460
		buf.append("        } catch (NullPointerException e) {\n");
461
		buf.append("            e.printStackTrace();\n");
462
		buf.append("        } catch (ClassCastException e) {\n");
463
		buf.append("            e.printStackTrace();\n");
464
		buf.append("        } catch (ArrayIndexOutOfBoundsException e) {\n");
465
		buf.append("            e.printStackTrace();\n");
466
		buf.append("        }\n");
467
		buf.append("    }\n");
468
		buf.append("}\n");
469
		String expected1= buf.toString();
470
471
		assertExpectedExistInProposals(proposals, new String[] { expected1 });
472
	}
473
474
	public void testUnrollMultiCatch5() throws Exception {
475
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
476
		StringBuffer buf= new StringBuffer();
477
		buf.append("package test1;\n");
478
		buf.append("public class E {\n");
479
		buf.append("    void foo() {\n");
480
		buf.append("        try {\n");
481
		buf.append("            System.out.println(\"foo\");\n");
482
		buf.append("        } catch (NullPointerException ex) {\n");
483
		buf.append("            ex.printStackTrace();\n");
484
		buf.append("        }\n");
485
		buf.append("    }\n");
486
		buf.append("}\n");
487
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
488
489
		int offset= buf.toString().indexOf("catch");
490
		AssistContext context= getCorrectionContext(cu, offset, 0);
491
		assertNoErrors(context);
492
		List proposals= collectAssists(context, false);
493
494
		assertProposalDoesNotExist(proposals, CONVERT_TO_MULTIPLE_CATCH_BLOCKS);
495
	}
496
497
}

Return to bug 348061