diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java index c733b4898a42..c10045ed938e 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodHandler.java @@ -152,8 +152,7 @@ public class ExtractMethodHandler implements RefactoringActionHandler, ContextAw return expressions.toArray(PsiElement.EMPTY_ARRAY); } - public static boolean shouldUseNewImpl(@NotNull Project project, PsiFile file, PsiElement @NotNull [] elements){ - if (! Registry.is("java.refactoring.extractMethod.newImplementation")) return false; + public static boolean canUseNewImpl(@NotNull Project project, PsiFile file, PsiElement @NotNull [] elements){ final ExtractMethodProcessor processor = getProcessor(project, elements, file, false); if (processor == null) return true; try { @@ -169,7 +168,7 @@ public class ExtractMethodHandler implements RefactoringActionHandler, ContextAw } public static void invokeOnElements(@NotNull Project project, final Editor editor, PsiFile file, PsiElement @NotNull [] elements) { - if (shouldUseNewImpl(project, file, elements)) { + if (Registry.is("java.refactoring.extractMethod.newImplementation") && canUseNewImpl(project, file, elements)) { new MethodExtractor().doExtract(editor, getRefactoringName(), HelpID.EXTRACT_METHOD); return; } diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java index 4ff3945be855..760bd24c7f62 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java @@ -800,7 +800,6 @@ public class ExtractMethodProcessor implements MatchProvider { myNullability = initNullability(); } - @TestOnly public void testPrepare() { myInputVariables.setFoldingAvailable(myInputVariables.isFoldingSelectedByDefault()); myMethodName = myInitialMethodName; diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java index 8ea314c65f4b..21ae4a18eb1a 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.java.refactoring; import com.intellij.JavaTestUtil; @@ -22,6 +22,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.extractMethod.ExtractMethodHandler; import com.intellij.refactoring.extractMethod.ExtractMethodProcessor; import com.intellij.refactoring.extractMethod.PrepareFailedException; +import com.intellij.refactoring.extractMethod.newImpl.MethodExtractor; import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; import com.intellij.refactoring.util.duplicates.Match; import com.intellij.testFramework.LightJavaCodeInsightTestCase; @@ -709,11 +710,11 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase { } public void testMethod2InterfaceFromStatic() throws Exception { - doTest(); + doTestWithLanguageLevel(LanguageLevel.JDK_1_8); } public void testMethod2InterfaceFromConstant() throws Exception { - doTest(); + doTestWithLanguageLevel(LanguageLevel.JDK_1_8); } public void testParamDetection() throws Exception { @@ -1405,7 +1406,7 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase { } public void testFromStaticMethodInInterface() throws Exception { - doTest(); + doTestWithLanguageLevel(LanguageLevel.JDK_1_8); } public void testDisjunctionType() throws Exception { @@ -1463,7 +1464,7 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase { private void doPrepareErrorTest(final String expectedMessage) throws Exception { String expectedError = null; try { - doExitPointsTest(false); + doErrorTest(); } catch(PrepareFailedException ex) { expectedError = ex.getMessage(); @@ -1471,11 +1472,22 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase { assertEquals(expectedMessage, expectedError); } + private void doErrorTest() throws Exception { + String fileName = getTestName(false) + ".java"; + configureByFile(BASE_PATH + fileName); + performAction(false,false); + } + private void doExitPointsTest(boolean shouldSucceed) throws Exception { String fileName = getTestName(false) + ".java"; configureByFile(BASE_PATH + fileName); - boolean success = performAction(false, false); - assertEquals(shouldSucceed, success); + boolean succeed = false; + try { + doErrorTest(); + succeed = true; + } catch (PrepareFailedException e) { + } + assertEquals(shouldSucceed, succeed); } private void doTest() throws Exception { @@ -1583,6 +1595,11 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase { processor.setShowErrorDialogs(false); processor.setChainedConstructor(extractChainedConstructor); + if (ExtractMethodHandler.canUseNewImpl(project, file, elements)) { + return new MethodExtractor().doTestExtract(true, editor, extractChainedConstructor, makeStatic, returnType, + newNameOfFirstParam, targetClass, methodVisibility, disabledParams); + } + if (!processor.prepare()) { return false; }