From 00292e469634b1a286f5fac261ce220d8e3dcf2c Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 22 Mar 2011 19:32:43 +0100 Subject: [PATCH] Introduce variable tests fixed for multi-catch type --- .../IntroduceVariableBase.java | 14 ++++------- .../MultiCatchTyped.after.java | 2 +- .../refactoring/IntroduceVariableTest.java | 6 ++--- .../MockIntroduceVariableHandler.java | 24 +++++++++++++++---- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index d2e09cdd5cea..e75a44e48d8b 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -13,14 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/** - * Created by IntelliJ IDEA. - * User: dsl - * Date: Nov 15, 2002 - * Time: 5:21:33 PM - * To change this template use Options | File Templates. - */ package com.intellij.refactoring.introduceVariable; import com.intellij.codeInsight.CodeInsightUtil; @@ -72,6 +64,10 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +/** + * @author dsl + * Date: Nov 15, 2002 + */ public abstract class IntroduceVariableBase extends IntroduceHandlerBase implements RefactoringActionHandler { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.introduceVariable.IntroduceVariableBase"); @NonNls private static final String PREFER_STATEMENTS_OPTION = "introduce.variable.prefer.statements"; @@ -848,6 +844,4 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme conflicts.putValue(occurence, RefactoringBundle.message("introducing.variable.may.break.code.logic")); } } - - } diff --git a/java/java-tests/testData/refactoring/introduceVariable/MultiCatchTyped.after.java b/java/java-tests/testData/refactoring/introduceVariable/MultiCatchTyped.after.java index aed77f0ac0b7..f4f6d90fcb93 100644 --- a/java/java-tests/testData/refactoring/introduceVariable/MultiCatchTyped.after.java +++ b/java/java-tests/testData/refactoring/introduceVariable/MultiCatchTyped.after.java @@ -6,7 +6,7 @@ class C { void m() { try { } catch (E1 | E2 ex) { - final B b = ex; + final Exception b = ex; } } } diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java index 6bbc35d63484..df712f6facdb 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceVariableTest.java @@ -222,7 +222,7 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase { } public void testSiblingInnerClassType() throws Exception { - doTest(new MockIntroduceVariableHandler("vari", true, false, false, "A.B"){ + doTest(new MockIntroduceVariableHandler("vari", true, false, false, "A.B") { @Override public IntroduceVariableSettings getSettings(Project project, Editor editor, PsiExpression expr, PsiExpression[] occurrences, @@ -249,11 +249,11 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase { } public void testMultiCatchSimple() throws Exception { - doTest(new MockIntroduceVariableHandler("e", true, true, false, "C.E1 | C.E2")); + doTest(new MockIntroduceVariableHandler("e", true, true, false, "java.lang.Exception", true)); } public void testMultiCatchTyped() throws Exception { - doTest(new MockIntroduceVariableHandler("b", true, true, false, "C.E1 | C.E2")); + doTest(new MockIntroduceVariableHandler("b", true, true, false, "java.lang.Exception", true)); } private void doTest(IntroduceVariableBase testMe) throws Exception { diff --git a/java/java-tests/testSrc/com/intellij/refactoring/MockIntroduceVariableHandler.java b/java/java-tests/testSrc/com/intellij/refactoring/MockIntroduceVariableHandler.java index 620934fa1c41..61406067563b 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/MockIntroduceVariableHandler.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/MockIntroduceVariableHandler.java @@ -21,19 +21,27 @@ class MockIntroduceVariableHandler extends IntroduceVariableBase { private final boolean myDeclareFinal; private final boolean myReplaceLValues; private final String myExpectedTypeCanonicalName; + private final boolean myLookForType; public MockIntroduceVariableHandler(@NonNls final String name, final boolean replaceAll, - final boolean declareFinal, final boolean replaceLValues, - @NonNls final String expectedTypeCanonicalName) { + final boolean declareFinal, final boolean replaceLValues, + @NonNls final String expectedTypeCanonicalName) { + + this(name, replaceAll, declareFinal, replaceLValues, expectedTypeCanonicalName, false); + } + + public MockIntroduceVariableHandler(@NonNls final String name, final boolean replaceAll, + final boolean declareFinal, final boolean replaceLValues, + @NonNls final String expectedTypeCanonicalName, boolean lookForType) { myName = name; myReplaceAll = replaceAll; myDeclareFinal = declareFinal; myReplaceLValues = replaceLValues; myExpectedTypeCanonicalName = expectedTypeCanonicalName; + myLookForType = lookForType; } - @Override public IntroduceVariableSettings getSettings(Project project, Editor editor, PsiExpression expr, final PsiExpression[] occurrences, @@ -42,7 +50,8 @@ class MockIntroduceVariableHandler extends IntroduceVariableBase { boolean anyAssignmentLHS, InputValidator validator, final OccurrencesChooser.ReplaceChoice replaceChoice) { - final PsiType type = typeSelectorManager.getDefaultType(); + final PsiType type = myLookForType ? findType(typeSelectorManager.getTypesForAll(), typeSelectorManager.getDefaultType()) + : typeSelectorManager.getDefaultType(); Assert.assertTrue(type.getCanonicalText(), type.equalsToText(myExpectedTypeCanonicalName)); IntroduceVariableSettings introduceVariableSettings = new IntroduceVariableSettings() { @Override @@ -93,4 +102,11 @@ class MockIntroduceVariableHandler extends IntroduceVariableBase { protected boolean reportConflicts(final MultiMap conflicts, final Project project, IntroduceVariableSettings dialog) { return false; } + + private PsiType findType(final PsiType[] candidates, PsiType defaultType) { + for (PsiType candidate : candidates) { + if (candidate.equalsToText(myExpectedTypeCanonicalName)) return candidate; + } + return defaultType; + } }