diff --git a/java/java-analysis-impl/src/com/intellij/refactoring/util/VariableData.java b/java/java-analysis-impl/src/com/intellij/refactoring/util/VariableData.java index 4a21c555b80c..0ea2b912f731 100644 --- a/java/java-analysis-impl/src/com/intellij/refactoring/util/VariableData.java +++ b/java/java-analysis-impl/src/com/intellij/refactoring/util/VariableData.java @@ -4,6 +4,7 @@ package com.intellij.refactoring.util; import com.intellij.psi.*; import com.intellij.psi.impl.source.PsiImmediateClassType; import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.util.PsiTypesUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,7 +14,7 @@ public class VariableData extends AbstractVariableData { public VariableData(@NotNull PsiVariable var) { variable = var; - type = var.getType(); + type = correctType(var.getType()); } public VariableData(@Nullable PsiVariable var, PsiType type) { @@ -22,13 +23,20 @@ public class VariableData extends AbstractVariableData { if (LambdaUtil.notInferredType(type)) { type = PsiType.getJavaLangObject(var.getManager(), GlobalSearchScope.allScope(var.getProject())); } - this.type = SmartTypePointerManager.getInstance(var.getProject()).createSmartTypePointer(type).getType(); + this.type = correctType(SmartTypePointerManager.getInstance(var.getProject()).createSmartTypePointer(type).getType()); } else { - this.type = type; + this.type = correctType(type); } } + private static PsiType correctType(PsiType varType) { + if (varType instanceof PsiDisjunctionType) { + return PsiTypesUtil.getLowestUpperBoundClassType((PsiDisjunctionType)varType); + } + return varType; + } + @NotNull public VariableData substitute(@Nullable PsiVariable var) { if (var == null) { diff --git a/java/java-tests/testData/refactoring/extractMethod/DisjunctionType.java b/java/java-tests/testData/refactoring/extractMethod/DisjunctionType.java new file mode 100644 index 000000000000..ff09847a4aa6 --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/DisjunctionType.java @@ -0,0 +1,12 @@ +public class DisjunctionType { + static void test() { + try { + foo(); + } + catch (NoSuchFieldException | NoSuchMethodException ex) { + + System.out.println(ex.getCause()); + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/extractMethod/DisjunctionType_after.java b/java/java-tests/testData/refactoring/extractMethod/DisjunctionType_after.java new file mode 100644 index 000000000000..440d0247c32d --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/DisjunctionType_after.java @@ -0,0 +1,16 @@ +public class DisjunctionType { + static void test() { + try { + foo(); + } + catch (NoSuchFieldException | NoSuchMethodException ex) { + + newMethod(ex); + + } + } + + private static void newMethod(ReflectiveOperationException ex) { + System.out.println(ex.getCause()); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodTest.java index 14fd9709c9c9..3a21760a9090 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodTest.java @@ -1382,6 +1382,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase { public void testFromStaticMethodInInterface() throws Exception { doTest(); } + + public void testDisjunctionType() throws Exception { + doTest(); + } private void doTestDisabledParam() throws PrepareFailedException { final CommonCodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);