replace constructor with factory: insert class type parameters for default constructor(IDEA-104858)

This commit is contained in:
anna
2013-04-10 12:13:18 +02:00
parent 16acf1398e
commit feed82a5bd
5 changed files with 21 additions and 9 deletions
@@ -116,9 +116,9 @@ public class ReplaceConstructorWithFactoryHandler
CommonRefactoringUtil.showErrorHint(myProject, editor, message, REFACTORING_NAME, HelpID.REPLACE_CONSTRUCTOR_WITH_FACTORY);
return;
}
final int answer = Messages.showYesNoCancelDialog(myProject,
RefactoringBundle.message("would.you.like.to.replace.default.constructor.of.0.with.factory.method", aClass.getQualifiedName()),
REFACTORING_NAME, Messages.getQuestionIcon()
final int answer = Messages.showYesNoDialog(myProject,
RefactoringBundle.message("would.you.like.to.replace.default.constructor.of.0.with.factory.method", aClass.getQualifiedName()),
REFACTORING_NAME, Messages.getQuestionIcon()
);
if (answer != 0) return;
if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, aClass)) return;
@@ -250,15 +250,16 @@ public class ReplaceConstructorWithFactoryProcessor extends BaseRefactoringProce
if (myConstructor != null) {
factoryMethod.getParameterList().replace(myConstructor.getParameterList());
factoryMethod.getThrowsList().replace(myConstructor.getThrowsList());
}
Collection<String> names = new HashSet<String>();
for (PsiTypeParameter typeParameter : PsiUtil.typeParametersIterable(myConstructor)) {
if (!names.contains(typeParameter.getName())) { //Otherwise type parameter is hidden in the constructor
names.add(typeParameter.getName());
factoryMethod.getTypeParameterList().addAfter(typeParameter, null);
}
Collection<String> names = new HashSet<String>();
for (PsiTypeParameter typeParameter : PsiUtil.typeParametersIterable(myConstructor != null ? myConstructor : containingClass)) {
if (!names.contains(typeParameter.getName())) { //Otherwise type parameter is hidden in the constructor
names.add(typeParameter.getName());
factoryMethod.getTypeParameterList().addAfter(typeParameter, null);
}
}
PsiReturnStatement returnStatement =
(PsiReturnStatement)myFactory.createStatementFromText("return new A();", null);
PsiNewExpression newExpression = (PsiNewExpression)returnStatement.getReturnValue();
@@ -0,0 +1,8 @@
class Test<A,B,C> {
private Test() {
}
static <A, B, C> Test<A, B, C> newTest() {
return new Test<A, B, C>();
}
}
@@ -0,0 +1,2 @@
class Te<caret>st<A,B,C> {
}
@@ -22,6 +22,7 @@ public class ReplaceConstructorWithFactoryTest extends LightRefactoringTestCase
public void testSubclass() throws Exception { runTest("02", null); }
public void testDefaultConstructor() throws Exception { runTest("03", null); }
public void testDefaultConstructorWithTypeParams() throws Exception { runTest("TypeParams", null); }
public void testInnerClass() throws Exception { runTest("04", "OuterClass"); }