do not generate duplicated constructors, warn when nothing was added (IDEA-67659)

This commit is contained in:
anna
2011-06-08 14:43:46 +04:00
parent 711350a21d
commit 3354dfff66
5 changed files with 37 additions and 3 deletions

View File

@@ -161,9 +161,22 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
}
constructors.add(new PsiGenerationInfo(generateConstructorPrototype(aClass, baseConstructor, myCopyJavadoc, fields)));
}
return constructors;
return filterOutAlreadyInsertedConstructors(aClass, constructors);
}
return Collections.<GenerationInfo>singletonList(new PsiGenerationInfo(generateConstructorPrototype(aClass, null, false, fields)));
final List<GenerationInfo> constructors =
Collections.<GenerationInfo>singletonList(new PsiGenerationInfo(generateConstructorPrototype(aClass, null, false, fields)));
return filterOutAlreadyInsertedConstructors(aClass, constructors);
}
private static List<? extends GenerationInfo> filterOutAlreadyInsertedConstructors(PsiClass aClass, List<? extends GenerationInfo> constructors) {
boolean alreadyExist = true;
for (GenerationInfo constructor : constructors) {
alreadyExist &= aClass.findMethodBySignature((PsiMethod)constructor.getPsiMember(), false) != null;
}
if (alreadyExist) {
return Collections.emptyList();
}
return constructors;
}
public static PsiMethod generateConstructorPrototype(PsiClass aClass, PsiMethod baseConstructor, boolean copyJavaDoc, PsiField[] fields) throws IncorrectOperationException {
@@ -172,6 +185,7 @@ public class GenerateConstructorHandler extends GenerateMembersHandlerBase {
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
PsiMethod constructor = factory.createConstructor();
constructor.setName(aClass.getName());
@Modifier String modifier = getConstructorModifier(aClass);
if (modifier != null) {
PsiUtil.setModifierProperty(constructor, modifier, true);

View File

@@ -16,6 +16,7 @@
package com.intellij.codeInsight.generation;
import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateEditingAdapter;
import com.intellij.codeInsight.template.TemplateManager;
@@ -114,10 +115,16 @@ public abstract class GenerateMembersHandlerBase implements CodeInsightActionHan
LOG.error(e);
return;
}
if (newMembers.isEmpty()) return;
editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(line, col));
if (newMembers.isEmpty()) {
if (!ApplicationManager.getApplication().isUnitTestMode()) {
HintManager.getInstance().showErrorHint(editor, "Nothing found to insert");
}
return;
}
final ArrayList<TemplateGenerationInfo> templates = new ArrayList<TemplateGenerationInfo>();
for (GenerationInfo member : newMembers) {
if (member instanceof TemplateGenerationInfo) {

View File

@@ -0,0 +1,6 @@
public class Test {
<caret>
public Test() {
}
}

View File

@@ -0,0 +1,6 @@
public class Test {
<caret>
public Test() {
}
}

View File

@@ -17,6 +17,7 @@ public class GenerateConstructorTest extends LightCodeInsightTestCase {
public void testBoundComments() throws Exception { doTest(); }
public void testSameNamedFields() throws Exception { doTest(); }
public void testEnumWithAbstractMethod() throws Exception { doTest(); }
public void testNoMoreConstructorsCanBeGenerated() throws Exception { doTest(); }
public void testImmediatelyAfterRBrace() throws Exception { // IDEADEV-28811
final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings();