generate constructor matching super on extend/implement (IDEA-59463)

This commit is contained in:
anna
2010-10-07 12:20:01 +04:00
parent 8023ceee66
commit fb48f50a1b
2 changed files with 50 additions and 10 deletions
@@ -44,7 +44,7 @@ import java.util.List;
* @author ven
*/
public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
private final Logger LOG = Logger.getInstance("com.intellij.codeInsight.daemon.impl.quickfix.CreateConstructorMatchingSuperFix");
private static final Logger LOG = Logger.getInstance("com.intellij.codeInsight.daemon.impl.quickfix.CreateConstructorMatchingSuperFix");
private final PsiClass myClass;
@@ -74,6 +74,15 @@ public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
if (PsiUtil.isAccessible(baseConstr, myClass, myClass)) baseConstructors.add(new PsiMethodMember(baseConstr, substitutor));
}
chooseConstructor2Delegate(project, editor, substitutor, baseConstructors, baseConstrs, myClass);
}
public static void chooseConstructor2Delegate(final Project project,
final Editor editor,
PsiSubstitutor substitutor,
List<PsiMethodMember> baseConstructors,
PsiMethod[] baseConstrs,
final PsiClass targetClass) {
PsiMethodMember[] constructors = baseConstructors.toArray(new PsiMethodMember[baseConstructors.size()]);
if (constructors.length == 0) {
constructors = new PsiMethodMember[baseConstrs.length];
@@ -99,7 +108,7 @@ public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
new Runnable() {
public void run() {
try {
PsiElementFactory factory = JavaPsiFacade.getInstance(myClass.getProject()).getElementFactory();
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
CodeStyleManager reformatter = CodeStyleManager.getInstance(project);
PsiMethod derived = null;
for (PsiMethodMember candidate : constructors1) {
@@ -113,7 +122,7 @@ public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
}
}
derived.getNameIdentifier().replace(myClass.getNameIdentifier());
derived.getNameIdentifier().replace(targetClass.getNameIdentifier());
@NonNls StringBuffer buffer = new StringBuffer();
buffer.append("void foo () {\nsuper(");
@@ -124,11 +133,11 @@ public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
if (j < params.length - 1) buffer.append(",");
}
buffer.append(");\n}");
PsiMethod stub = factory.createMethodFromText(buffer.toString(), myClass);
PsiMethod stub = factory.createMethodFromText(buffer.toString(), targetClass);
derived.getBody().replace(stub.getBody());
derived = (PsiMethod)reformatter.reformat(derived);
derived = (PsiMethod)myClass.add(derived);
derived = (PsiMethod)targetClass.add(derived);
}
if (derived != null) {
editor.getCaretModel().moveToOffset(derived.getTextRange().getStartOffset());
@@ -139,7 +148,7 @@ public class CreateConstructorMatchingSuperFix extends BaseIntentionAction {
LOG.error(e);
}
UndoUtil.markPsiFileForUndo(myClass.getContainingFile());
UndoUtil.markPsiFileForUndo(targetClass.getContainingFile());
}
}
);
@@ -26,10 +26,14 @@ package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightNamesUtil;
import com.intellij.codeInsight.daemon.impl.quickfix.CreateClassKind;
import com.intellij.codeInsight.daemon.impl.quickfix.CreateConstructorMatchingSuperFix;
import com.intellij.codeInsight.generation.OverrideImplementUtil;
import com.intellij.codeInsight.generation.PsiMethodMember;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.ide.util.MemberChooser;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
@@ -41,11 +45,16 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class CreateSubclassAction extends PsiElementBaseIntentionAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.intention.impl.ImplementAbstractClassAction");
private String myText = CodeInsightBundle.message("intention.implement.abstract.class.default.text");
@@ -176,11 +185,33 @@ public class CreateSubclassAction extends PsiElementBaseIntentionAction {
}
});
if (targetClass[0] == null) return;
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final Editor editor1 = CodeInsightUtil.positionCursor(project, targetClass[0].getContainingFile(), targetClass[0].getLBrace());
if (editor1 == null) return;
OverrideImplementUtil.chooseAndImplementMethods(project, editor1, targetClass[0]);
final Editor editor = CodeInsightUtil.positionCursor(project, targetClass[0].getContainingFile(), targetClass[0].getLBrace());
if (editor == null) return;
boolean hasNonTrivialConstructor = false;
final PsiMethod[] constructors = psiClass.getConstructors();
for (PsiMethod constructor : constructors) {
if (constructor.getParameterList().getParametersCount() > 0) {
hasNonTrivialConstructor = true;
break;
}
}
if (hasNonTrivialConstructor) {
final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(psiClass, targetClass[0], PsiSubstitutor.EMPTY);
final List<PsiMethodMember> baseConstructors = new ArrayList<PsiMethodMember>();
for (PsiMethod baseConstr : constructors) {
if (PsiUtil.isAccessible(baseConstr, targetClass[0], targetClass[0])) {
baseConstructors.add(new PsiMethodMember(baseConstr, substitutor));
}
}
CreateConstructorMatchingSuperFix.chooseConstructor2Delegate(project, editor,
substitutor,
baseConstructors, constructors, targetClass[0]);
}
OverrideImplementUtil.chooseAndImplementMethods(project, editor, targetClass[0]);
}
}
});