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 388724 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/surround/SurroundWithTryCatchRefactoring.java (-6 / +62 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Samrat Dhillon samrat.dhillon@gmail.com - Bug 388724 -  [surround with try/catch][quick fix] Multi-Catch QuickFix creates compiler error
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.corext.refactoring.surround;
12
package org.eclipse.jdt.internal.corext.refactoring.surround;
12
13
Lines 235-241 Link Here
235
		TryStatement tryStatement= getAST().newTryStatement();
236
		TryStatement tryStatement= getAST().newTryStatement();
236
		ITypeBinding[] exceptions= fAnalyzer.getExceptions();
237
		ITypeBinding[] exceptions= fAnalyzer.getExceptions();
237
		ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fAnalyzer.getEnclosingBodyDeclaration(), fImportRewrite);
238
		ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fAnalyzer.getEnclosingBodyDeclaration(), fImportRewrite);
238
239
		ITypeBinding[] superExceptions = getSuperExceptionSet(exceptions);
240
		
239
		if (!fIsMultiCatch) {
241
		if (!fIsMultiCatch) {
240
			for (int i= 0; i < exceptions.length; i++) {
242
			for (int i= 0; i < exceptions.length; i++) {
241
				ITypeBinding exception= exceptions[i];
243
				ITypeBinding exception= exceptions[i];
Lines 265-275 Link Here
265
267
266
			UnionType unionType= getAST().newUnionType();
268
			UnionType unionType= getAST().newUnionType();
267
			List<Type> types= unionType.types();
269
			List<Type> types= unionType.types();
268
			for (int i= 0; i < exceptions.length; i++) {
270
			int i=0;
269
				ITypeBinding exception= exceptions[i];
271
			for (ITypeBinding exception:superExceptions) {
270
				Type type= fImportRewrite.addImport(exception, getAST(), context);
272
				if(exception!=null){
271
				types.add(type);
273
					Type type= fImportRewrite.addImport(exception, getAST(), context);
272
				fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(type), i == 0);
274
					types.add(type);
275
					fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(type), i == 0);
276
					i++;
277
				}
273
			}
278
			}
274
279
275
			decl.setType(unionType);
280
			decl.setType(unionType);
Lines 373-378 Link Here
373
			statements.insertLast(toMove, null);
378
			statements.insertLast(toMove, null);
374
		}
379
		}
375
	}
380
	}
381
	/*
382
	 * Get the subset of exceptions from a list, that are not subclass of any other other class in the same list
383
	 */
384
	private ITypeBinding[] getSuperExceptionSet(ITypeBinding[] exceptions) {
385
		//initialize the array to size of given exception array. That is the maximum size possible.
386
		ITypeBinding[] superExceptions = new ITypeBinding[exceptions.length];
387
		int currentIndex=0;
388
		for(int i =0;i<exceptions.length;i++){
389
			currentIndex=addToSuperException(superExceptions, exceptions[i], currentIndex);
390
		}
391
		return superExceptions;
392
	}
393
	private int addToSuperException(ITypeBinding[] superExceptions,ITypeBinding exception, int currentIndex) {
394
		boolean superTypeAlreadyPresent=false;
395
		boolean addingSuperType=false;
396
		for(int i=0;i<superExceptions.length;i++){
397
			//check if the super class of the exception that we want to add in the array, is already present. If super class is present dont add anything.
398
			if(superExceptions[i]!=null && exception.isSubTypeCompatible(superExceptions[i])){
399
				superTypeAlreadyPresent=true;
400
				break;
401
			}
402
			//if exception we are adding now is the super class of the exception already added, then replace the existing exception with super class
403
			if(superExceptions[i]!=null && superExceptions[i].isSubTypeCompatible(exception)){
404
				addingSuperType=true;
405
				superExceptions[i]=exception;
406
				//normalize will remove duplicate if we added the same class twice
407
				normalize(superExceptions,exception);
408
			}
409
		}
410
		//if super class was not present or if we were not adding super type, then add the new exception
411
		if(!superTypeAlreadyPresent && !addingSuperType){
412
			superExceptions[currentIndex] = exception;
413
			currentIndex++;
414
		}
415
		return currentIndex;
416
	}
417
	//Remove duplicates from the list of exceptions
418
	private void normalize(ITypeBinding[] superExceptions,ITypeBinding exception) {
419
		boolean firstInstanceFound=false;
420
		for(int i=0;i<superExceptions.length;i++){
421
			if(superExceptions[i]!=null){
422
				if(superExceptions[i].getQualifiedName().equals(exception.getQualifiedName())){
423
					if(!firstInstanceFound){
424
						firstInstanceFound=true;
425
					}else{
426
						superExceptions[i]=null;
427
					}
428
				}
429
			}
430
		}
431
	}
376
432
377
	private List<ASTNode> getSpecialVariableDeclarationStatements() {
433
	private List<ASTNode> getSpecialVariableDeclarationStatements() {
378
		List<ASTNode> result= new ArrayList<ASTNode>(3);
434
		List<ASTNode> result= new ArrayList<ASTNode>(3);

Return to bug 388724