IDEA-244988: extract method: dont suggest static modifier inside local or inner classes

GitOrigin-RevId: 2c496066816e484a45c71cb65e25e217c394e261
This commit is contained in:
Alexandr Suhinin
2020-07-02 13:00:35 +00:00
committed by intellij-monorepo-bot
parent f83b9b0410
commit 3ab1e8e852
8 changed files with 70 additions and 5 deletions
@@ -209,7 +209,8 @@ object ExtractMethodPipeline {
}
fun withForcedStatic(analyzer: CodeFragmentAnalyzer, extractOptions: ExtractOptions): ExtractOptions? {
val targetClass = PsiTreeUtil.getParentOfType(ExtractMethodHelper.getValidParentOf(extractOptions.elements.first()), PsiClass::class.java)!!
val targetClass = PsiTreeUtil.getParentOfType(extractOptions.anchor, PsiClass::class.java)!!
if (PsiUtil.isLocalOrAnonymousClass(targetClass) || PsiUtil.isInnerClass(targetClass)) return null
val fieldUsages = analyzer.findLocalFieldUsages(targetClass, extractOptions.elements)
if (fieldUsages.any { it.isWrite }) return null
val fieldInputParameters =
@@ -126,7 +126,8 @@ class MethodExtractor {
val candidates = ExtractMethodPipeline.findTargetCandidates(analyzer, options)
val defaultTargetClass = candidates.firstOrNull { it !is PsiAnonymousClass } ?: candidates.first()
options = ExtractMethodPipeline.withTargetClass(analyzer, options, defaultTargetClass) ?: throw ExtractException("Fail", elements.first())
options = ExtractMethodPipeline.withTargetClass(analyzer, options, targetClass ?: defaultTargetClass)
?: throw ExtractException("Fail", elements.first())
options = options.copy(methodName = "newMethod")
if (isConstructor != options.isConstructor){
options = ExtractMethodPipeline.asConstructor(analyzer, options) ?: throw ExtractException("Fail", elements.first())
@@ -142,9 +143,6 @@ class MethodExtractor {
if (returnType != null) {
options = options.copy(dataOutput = options.dataOutput.withType(returnType))
}
if (targetClass != null) {
options = ExtractMethodPipeline.withTargetClass(analyzer, options, targetClass) ?: options
}
if (disabledParameters.isNotEmpty()) {
options = options.copy(
disabledParameters = options.inputParameters.filterIndexed { index, _ -> index in disabledParameters },
@@ -0,0 +1,7 @@
class Outer {
class Inner {
{
<selection>int i = 0;</selection>
}
}
}
@@ -0,0 +1,7 @@
class Outer {
static class Nested {
{
<selection>int i = 0;</selection>
}
}
}
@@ -0,0 +1,11 @@
class Outer {
static class Nested {
{
newMethod();
}
private static void newMethod() {
int i = 0;
}
}
}
@@ -0,0 +1,7 @@
class Outer {
class Inner {
{
<selection>int i = 0;</selection>
}
}
}
@@ -0,0 +1,11 @@
class Outer {
class Inner {
{
newMethod();
}
}
private static void newMethod() {
int i = 0;
}
}
@@ -1414,6 +1414,29 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testNoStaticForInnerClass() {
try {
configureByFile(BASE_PATH + getTestName(false) + ".java");
performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, true, null, null, null);
fail("Static modifier is forbidden inside inner classes");
} catch (PrepareFailedException e){
}
}
public void testStaticForNestedClass() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, true, null, null, null);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testStaticForOuterClass() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
final int caret = getEditor().getSelectionModel().getLeadSelectionOffset();
final PsiClass outerClass = PsiTreeUtil.getParentOfType(getFile().findElementAt(caret), PsiClass.class).getContainingClass();
performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, true, null, outerClass, null);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testDontMakeParametersFinalDueToUsagesInsideAnonymous() throws Exception {
doTest();
}