diff options
| author | Manju Mathew | 2013-10-17 12:12:33 +0000 |
|---|---|---|
| committer | Manju Mathew | 2013-10-17 12:12:33 +0000 |
| commit | 794af31895de019e6dee502e0d1f0ba337c03cdd (patch) | |
| tree | 76e306cd0498bab024c2621a9f9b8a5ea59c1977 | |
| parent | ace0a97be4318ccbde62328ddc0b8374bf710d7c (diff) | |
| download | eclipse.jdt.ui-794af31895de019e6dee502e0d1f0ba337c03cdd.tar.gz eclipse.jdt.ui-794af31895de019e6dee502e0d1f0ba337c03cdd.tar.xz eclipse.jdt.ui-794af31895de019e6dee502e0d1f0ba337c03cdd.zip | |
Fixed Bug 388724: [surround with try/catch][quick fix] Multi-Catch
QuickFix creates compiler error
2 files changed, 47 insertions, 8 deletions
diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/surround/SurroundWithTryCatchRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/surround/SurroundWithTryCatchRefactoring.java index 617690d336..e0fc5dd57a 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/surround/SurroundWithTryCatchRefactoring.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/surround/SurroundWithTryCatchRefactoring.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -11,6 +11,7 @@ package org.eclipse.jdt.internal.corext.refactoring.surround; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -257,6 +258,7 @@ public class SurroundWithTryCatchRefactoring extends Refactoring { fLinkedProposalModel.getPositionGroup(GROUP_EXC_NAME + i, true).addPosition(fRewriter.track(decl.getName()), false); } } else { + List<ITypeBinding> filteredExceptions= filterSubtypeExceptions(exceptions); CatchClause catchClause= getAST().newCatchClause(); SingleVariableDeclaration decl= getAST().newSingleVariableDeclaration(); String varName= StubUtility.getExceptionVariableName(fCUnit.getJavaProject()); @@ -265,11 +267,12 @@ public class SurroundWithTryCatchRefactoring extends Refactoring { UnionType unionType= getAST().newUnionType(); List<Type> types= unionType.types(); - for (int i= 0; i < exceptions.length; i++) { - ITypeBinding exception= exceptions[i]; + int i=0; + for (ITypeBinding exception : filteredExceptions) { Type type= fImportRewrite.addImport(exception, getAST(), context); types.add(type); fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(type), i == 0); + i++; } decl.setType(unionType); @@ -374,6 +377,23 @@ public class SurroundWithTryCatchRefactoring extends Refactoring { } } + private List<ITypeBinding> filterSubtypeExceptions(ITypeBinding[] exceptions) { + List<ITypeBinding> filteredExceptions= new ArrayList<ITypeBinding>(); + filteredExceptions.addAll(Arrays.asList(exceptions)); + + for (Iterator<ITypeBinding> subtypeIterator= filteredExceptions.iterator(); subtypeIterator.hasNext();) { + ITypeBinding iTypeBinding= subtypeIterator.next(); + for (Iterator<ITypeBinding> supertypeIterator= filteredExceptions.iterator(); supertypeIterator.hasNext();) { + ITypeBinding superTypeBinding= supertypeIterator.next(); + if (!iTypeBinding.equals(superTypeBinding) && iTypeBinding.isSubTypeCompatible(superTypeBinding)) { + subtypeIterator.remove(); + break; + } + } + } + return filteredExceptions; + } + private List<ASTNode> getSpecialVariableDeclarationStatements() { List<ASTNode> result= new ArrayList<ASTNode>(3); VariableDeclaration[] locals= fAnalyzer.getAffectedLocals(); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java index 964dc45f8f..cd9576c238 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java @@ -13,6 +13,7 @@ package org.eclipse.jdt.internal.ui.text.correction; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; @@ -287,7 +288,8 @@ public class LocalCorrectionsSubProcessor { List<CatchClause> catchClauses= surroundingTry.catchClauses(); if (catchClauses != null && catchClauses.size() == 1) { - String label= uncaughtExceptions.length > 1 + List<ITypeBinding> filteredExceptions= filterSubtypeExceptions(uncaughtExceptions); + String label= filteredExceptions.size() > 1 ? CorrectionMessages.LocalCorrectionsSubProcessor_addexceptionstoexistingcatch_description : CorrectionMessages.LocalCorrectionsSubProcessor_addexceptiontoexistingcatch_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION); @@ -301,8 +303,8 @@ public class LocalCorrectionsSubProcessor { if (type instanceof UnionType) { UnionType unionType= (UnionType) type; ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); - for (int i= 0; i < uncaughtExceptions.length; i++) { - ITypeBinding excBinding= uncaughtExceptions[i]; + for (int i= 0; i < filteredExceptions.size(); i++) { + ITypeBinding excBinding= filteredExceptions.get(i); Type type2= imports.addImport(excBinding, ast, importRewriteContext); listRewrite.insertLast(type2, null); @@ -315,8 +317,8 @@ public class LocalCorrectionsSubProcessor { List<Type> types= newUnionType.types(); types.add((Type) rewrite.createCopyTarget(type)); - for (int i= 0; i < uncaughtExceptions.length; i++) { - ITypeBinding excBinding= uncaughtExceptions[i]; + for (int i= 0; i < filteredExceptions.size(); i++) { + ITypeBinding excBinding= filteredExceptions.get(i); Type type2= imports.addImport(excBinding, ast, importRewriteContext); types.add(type2); @@ -433,6 +435,23 @@ public class LocalCorrectionsSubProcessor { } } + private static List<ITypeBinding> filterSubtypeExceptions(ITypeBinding[] exceptions) { + List<ITypeBinding> filteredExceptions= new ArrayList<ITypeBinding>(); + filteredExceptions.addAll(Arrays.asList(exceptions)); + + for (Iterator<ITypeBinding> subtypeIterator= filteredExceptions.iterator(); subtypeIterator.hasNext();) { + ITypeBinding iTypeBinding= subtypeIterator.next(); + for (Iterator<ITypeBinding> supertypeIterator= filteredExceptions.iterator(); supertypeIterator.hasNext();) { + ITypeBinding superTypeBinding= supertypeIterator.next(); + if (!iTypeBinding.equals(superTypeBinding) && iTypeBinding.isSubTypeCompatible(superTypeBinding)) { + subtypeIterator.remove(); + break; + } + } + } + return filteredExceptions; + } + private static void addExceptionTypeLinkProposals(LinkedCorrectionProposal proposal, ITypeBinding exc, String key) { // all super classes except Object while (exc != null && !"java.lang.Object".equals(exc.getQualifiedName())) { //$NON-NLS-1$ |
