diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java index 2cecd333f5ad..a0a1f435c404 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java @@ -30,6 +30,7 @@ import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author mike @@ -97,10 +98,11 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { } } - public static void setupSuperCall(PsiClass targetClass, PsiMethod constructor, TemplateBuilderImpl templateBuilder) + @Nullable + public static PsiMethod setupSuperCall(PsiClass targetClass, PsiMethod constructor, TemplateBuilderImpl templateBuilder) throws IncorrectOperationException { PsiElementFactory elementFactory = JavaPsiFacade.getInstance(targetClass.getProject()).getElementFactory(); - + PsiMethod supConstructor = null; PsiClass superClass = targetClass.getSuperClass(); if (superClass != null && !"java.lang.Object".equals(superClass.getQualifiedName()) && !"java.lang.Enum".equals(superClass.getQualifiedName())) { @@ -110,7 +112,10 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { for (PsiMethod superConstructor : constructors) { if (superConstructor.getParameterList().getParametersCount() == 0) { hasDefaultConstructor = true; + supConstructor = null; break; + } else { + supConstructor = superConstructor; } } @@ -126,6 +131,7 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { } templateBuilder.setEndVariableAfter(constructor.getBody().getLBrace()); + return supConstructor; } private static void setupGenericParameters(PsiNewExpression expr, PsiClass targetClass) throws IncorrectOperationException { diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java index 2161f38298f2..1b49e207d570 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateConstructorFromCallFix.java @@ -17,6 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.CodeInsightUtilBase; import com.intellij.codeInsight.daemon.QuickFixBundle; +import com.intellij.codeInsight.generation.OverrideImplementUtil; import com.intellij.codeInsight.template.Template; import com.intellij.codeInsight.template.TemplateBuilderImpl; import com.intellij.codeInsight.template.TemplateEditingAdapter; @@ -42,7 +43,7 @@ public class CreateConstructorFromCallFix extends CreateFromUsageBaseFix { myConstructorCall = constructorCall; } - protected void invokeImpl(PsiClass targetClass) { + protected void invokeImpl(final PsiClass targetClass) { final Project project = myConstructorCall.getProject(); PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); @@ -53,11 +54,10 @@ public class CreateConstructorFromCallFix extends CreateFromUsageBaseFix { TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(constructor); CreateFromUsageUtils.setupMethodParameters(constructor, templateBuilder, myConstructorCall.getArgumentList(), getTargetSubstitutor(myConstructorCall)); - CreateClassFromNewFix.setupSuperCall(targetClass, constructor, templateBuilder); + final PsiMethod superConstructor = CreateClassFromNewFix.setupSuperCall(targetClass, constructor, templateBuilder); constructor = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(constructor); Template template = templateBuilder.buildTemplate(); - targetClass = PsiTreeUtil.getParentOfType(constructor, PsiClass.class); if (targetClass == null) return; final Editor editor = positionCursor(project, targetClass.getContainingFile(), targetClass); final TextRange textRange = constructor.getTextRange(); @@ -72,7 +72,11 @@ public class CreateConstructorFromCallFix extends CreateFromUsageBaseFix { PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); final int offset = editor.getCaretModel().getOffset(); PsiMethod constructor = PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiMethod.class, false); - CreateFromUsageUtils.setupMethodBody(constructor); + if (superConstructor == null) { + CreateFromUsageUtils.setupMethodBody(constructor); + } else { + OverrideImplementUtil.setupMethodBody(constructor, superConstructor, targetClass); + } CreateFromUsageUtils.setupEditor(constructor, editor); } catch (IncorrectOperationException e) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterDefaultSuperConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterDefaultSuperConstructor.java new file mode 100644 index 000000000000..b7ad0c94e199 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterDefaultSuperConstructor.java @@ -0,0 +1,16 @@ +// "Create Constructor" "true" +class Test extends A{ + + public Test(String a) { + //To change body of created methods use File | Settings | File Templates. + } + + public void t() { + new Test("a"){}; + } +} + +class A { + A(String s){} + A(){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterImplicitSuperConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterImplicitSuperConstructor.java new file mode 100644 index 000000000000..0293ed5758bd --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterImplicitSuperConstructor.java @@ -0,0 +1,14 @@ +// "Create Constructor" "true" +class Test extends A{ + + public Test(String a) { + //To change body of created methods use File | Settings | File Templates. + } + + public void t() { + new Test("a"){}; + } +} + +class A { +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterInsertSuper.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterInsertSuper.java new file mode 100644 index 000000000000..52dca4122c8a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/afterInsertSuper.java @@ -0,0 +1,15 @@ +// "Create Constructor" "true" +class Test extends A{ + + public Test(String a) { + super(a); //To change body of overridden methods use File | Settings | File Templates. + } + + public void t() { + new Test("a"){}; + } +} + +class A { + A(String s){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeDefaultSuperConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeDefaultSuperConstructor.java new file mode 100644 index 000000000000..634d2f1a95b5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeDefaultSuperConstructor.java @@ -0,0 +1,12 @@ +// "Create Constructor" "true" +class Test extends A{ + + public void t() { + new Test("a"){}; + } +} + +class A { + A(String s){} + A(){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeImplicitSuperConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeImplicitSuperConstructor.java new file mode 100644 index 000000000000..29e3172c95e0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeImplicitSuperConstructor.java @@ -0,0 +1,10 @@ +// "Create Constructor" "true" +class Test extends A{ + + public void t() { + new Test("a"){}; + } +} + +class A { +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeInsertSuper.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeInsertSuper.java new file mode 100644 index 000000000000..a137d38b6c64 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createConstructorFromCall/beforeInsertSuper.java @@ -0,0 +1,11 @@ +// "Create Constructor" "true" +class Test extends A{ + + public void t() { + new Test("a"){}; + } +} + +class A { + A(String s){} +} \ No newline at end of file