diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateInnerClassFromNewFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateInnerClassFromNewFix.java index 8f22b678efe4..09e8b4eb8f4c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateInnerClassFromNewFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateInnerClassFromNewFix.java @@ -17,8 +17,10 @@ package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.daemon.QuickFixBundle; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; /** @@ -50,11 +52,33 @@ public class CreateInnerClassFromNewFix extends CreateClassFromNewFix { final PsiModifierList modifierList = created.getModifierList(); LOG.assertTrue(modifierList != null); modifierList.setModifierProperty(PsiModifier.PRIVATE, true); - if (PsiUtil.getEnclosingStaticElement(newExpression, targetClass) != null) { + if (PsiUtil.getEnclosingStaticElement(newExpression, targetClass) != null || isInThisOrSuperCall(newExpression)) { modifierList.setModifierProperty(PsiModifier.STATIC, true); } created = (PsiClass)targetClass.add(created); setupClassFromNewExpression(created, newExpression); } + + private static boolean isInThisOrSuperCall(PsiNewExpression newExpression) { + boolean inFirstConstructorLine = false; + final PsiExpressionStatement expressionStatement = PsiTreeUtil.getParentOfType(newExpression, PsiExpressionStatement.class); + if (expressionStatement != null) { + final PsiExpression expression = expressionStatement.getExpression(); + if (expression instanceof PsiMethodCallExpression) { + final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)expression).getMethodExpression(); + final PsiElement resolve = methodExpression.resolve(); + if (resolve instanceof PsiMethod && ((PsiMethod)resolve).isConstructor()) { + final PsiElement referenceNameElement = methodExpression.getReferenceNameElement(); + if (referenceNameElement != null) { + if (Comparing.strEqual(referenceNameElement.getText(), PsiKeyword.THIS) || + Comparing.strEqual(referenceNameElement.getText(), PsiKeyword.SUPER)) { + inFirstConstructorLine = true; + } + } + } + } + } + return inFirstConstructorLine; + } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/afterInSuperNonConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/afterInSuperNonConstructor.java new file mode 100644 index 000000000000..68cf2310de2a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/afterInSuperNonConstructor.java @@ -0,0 +1,12 @@ +// "Create Inner Class 'Generic'" "true" +class Base { + void foo(){} +} +class Test extends Base { + Test() { + super.foo(new Generic ()); + } + + private class Generic { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/afterInThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/afterInThis.java new file mode 100644 index 000000000000..801e9f69ef4f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/afterInThis.java @@ -0,0 +1,11 @@ +// "Create Inner Class 'Generic'" "true" +class Test { + Test() { + this (new Generic ()); + } + + Test(String s){} + + private static class Generic { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeInSuperNonConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeInSuperNonConstructor.java new file mode 100644 index 000000000000..34a9ff3afee3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeInSuperNonConstructor.java @@ -0,0 +1,9 @@ +// "Create Inner Class 'Generic'" "true" +class Base { + void foo(){} +} +class Test extends Base { + Test() { + super.foo(new Generic ()); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeInThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeInThis.java new file mode 100644 index 000000000000..859150ca3937 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeInThis.java @@ -0,0 +1,8 @@ +// "Create Inner Class 'Generic'" "true" +class Test { + Test() { + this (new Generic ()); + } + + Test(String s){} +} \ No newline at end of file