IDEA-74202 "Change Signature" refactoring should handle Generics

This commit is contained in:
Maxim.Medvedev
2011-10-28 09:34:39 +04:00
parent 2e7a08c68c
commit fb4be8cb82
5 changed files with 84 additions and 74 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -148,7 +148,7 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc
final GrCodeBlock codeBlock = GroovyPsiElementFactory.getInstance(method.getProject()).createMethodBodyFromText(buffer.toString());
newMethod.setBlock(codeBlock);
newMethod.getModifierList().setModifierProperty(GrModifier.ABSTRACT, false);
newMethod.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false);
CodeStyleManager.getInstance(method.getProject()).reformat(newMethod);
return processPrimaryMethodInner(grInfo, method, null);
@@ -175,7 +175,7 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc
buffer.append(");");
}
private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, PsiMethod baseMethod) {
private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
if (changeInfo.isNameChanged()) {
String newName = baseMethod == null ? changeInfo.getNewName() :
RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
@@ -205,13 +205,9 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc
}
else {
PsiType type = newReturnType.getType(context, method.getManager());
final PsiType oldReturnType = method.getReturnType();
if (!TypesUtil
.isAssignableByMethodCallConversion(type, oldReturnType, context.getManager(), context.getResolveScope())) { //todo ask for replace covariant type
method.setReturnType(substitutor.substitute(type));
if (oldReturnTypeElement==null) {
modifierList.setModifierProperty(GrModifier.DEF, false);;
}
method.setReturnType(substitutor.substitute(type));
if (oldReturnTypeElement == null) {
modifierList.setModifierProperty(GrModifier.DEF, false);
}
}
}
@@ -371,8 +367,8 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc
.createConstructorFromText(name, ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", null);
GrModifierList list = constructor.getModifierList();
if (psiClass.hasModifierProperty(GrModifier.PRIVATE)) list.setModifierProperty(GrModifier.PRIVATE, true);
if (psiClass.hasModifierProperty(GrModifier.PROTECTED)) list.setModifierProperty(GrModifier.PROTECTED, true);
if (psiClass.hasModifierProperty(PsiModifier.PRIVATE)) list.setModifierProperty(PsiModifier.PRIVATE, true);
if (psiClass.hasModifierProperty(PsiModifier.PROTECTED)) list.setModifierProperty(PsiModifier.PROTECTED, true);
if (!list.hasExplicitVisibilityModifiers()) {
list.setModifierProperty(GrModifier.DEF, true);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,13 +15,11 @@
*/
package org.jetbrains.plugins.groovy.refactoring.changeSignature;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiType;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.refactoring.changeSignature.JavaThrownExceptionInfo;
import com.intellij.refactoring.changeSignature.ThrownExceptionInfo;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.util.TestUtils;
import java.io.File;
@@ -37,42 +35,33 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
}
public void testOneNewParameter() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo("p", -1, "\"5\"", null, CommonClassNames.JAVA_LANG_STRING)});
doTest(new SimpleInfo("p", -1, "\"5\"", null, CommonClassNames.JAVA_LANG_STRING));
}
public void testRemoveParameter() throws Exception {
doTest(new SimpleInfo[0]);
doTest();
}
public void testInsertParameter() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(0),
new SimpleInfo("p", -1, "5", "-3", PsiType.INT),
new SimpleInfo(1)
});
doTest(new SimpleInfo(0),
new SimpleInfo("p", -1, "5", "-3", PsiType.INT),
new SimpleInfo(1));
}
public void testInsertOptionalParameter() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(0),
new SimpleInfo(1),
new SimpleInfo("p", -1, "5", "-3", PsiType.INT)
});
doTest(new SimpleInfo(0),
new SimpleInfo(1),
new SimpleInfo("p", -1, "5", "-3", PsiType.INT));
}
public void testNamedParametersRemove() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(1),
new SimpleInfo(2)
});
doTest(new SimpleInfo(1),
new SimpleInfo(2));
}
public void testNamedParametersOrder1() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(0),
new SimpleInfo(2)
});
doTest(new SimpleInfo(0),
new SimpleInfo(2));
}
/*public void testNamedParametersOrder2() throws Exception {
@@ -92,17 +81,13 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
}*/
public void testMoveNamedParameters() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(1),
new SimpleInfo(0)
});
doTest(new SimpleInfo(1),
new SimpleInfo(0));
}
public void testMoveVarArgParameters() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(1),
new SimpleInfo(0)
});
doTest(new SimpleInfo(1),
new SimpleInfo(0));
}
public void testChangeVisibilityAndName() throws Exception {
@@ -110,28 +95,20 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
}
public void testImplicitConstructorInConstructor() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo("p", -1, "5", null, PsiType.INT)
});
doTest(new SimpleInfo("p", -1, "5", null, PsiType.INT));
}
public void testImplicitConstructorForClass() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo("p", -1, "5", null, PsiType.INT)
});
doTest(new SimpleInfo("p", -1, "5", null, PsiType.INT));
}
public void testAnonymousClassUsage() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo("p", -1, "5", null, PsiType.INT)
});
doTest(new SimpleInfo("p", -1, "5", null, PsiType.INT));
}
public void testGroovyDocReferences() throws Exception {
doTest(new SimpleInfo[]{
new SimpleInfo(0),
new SimpleInfo(2)
});
doTest(new SimpleInfo(0),
new SimpleInfo(2));
}
public void testOverriders() throws Exception {
@@ -139,7 +116,7 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
}
public void testParameterRename() throws Exception {
doTest(new SimpleInfo[]{new SimpleInfo("newP", 0)});
doTest(new SimpleInfo("newP", 0));
}
public void testAddReturnType() throws Exception {
@@ -197,39 +174,39 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
}
public void testTypeParameters() throws Exception {
doTest(new SimpleInfo[]{new SimpleInfo("list", -1, "null", null, "java.util.List<T>"), new SimpleInfo(0)});
doTest(new SimpleInfo("list", -1, "null", null, "java.util.List<T>"), new SimpleInfo(0));
}
public void testEnumConstructor() throws Exception {
doTest(new SimpleInfo[]{new SimpleInfo("a", -1, "2", null, PsiType.INT)});
doTest(new SimpleInfo("a", -1, "2", null, PsiType.INT));
}
public void testMoveArrayToTheEnd() throws Exception {
doTest(new SimpleInfo[] {new SimpleInfo(1), new SimpleInfo(0)});
doTest(new SimpleInfo(1), new SimpleInfo(0));
}
public void testReplaceVarargWithArray() throws Exception {
doTest(new SimpleInfo[]{new SimpleInfo("l", 1, null, null, "List<T>[]"), new SimpleInfo(0)});
doTest(new SimpleInfo("l", 1, null, null, "List<T>[]"), new SimpleInfo(0));
}
public void testReplaceVarargWithArray2() throws Exception {
doTest(new SimpleInfo[]{new SimpleInfo("l", 1, null, null, "Map<T, E>[]"), new SimpleInfo(0)});
doTest(new SimpleInfo("l", 1, null, null, "Map<T, E>[]"), new SimpleInfo(0));
}
public void testConstructorCall() {
doTest(new SimpleInfo[]{new SimpleInfo(0), new SimpleInfo("a", -1, "1", null, PsiType.INT)});
doTest(new SimpleInfo(0), new SimpleInfo("a", -1, "1", null, PsiType.INT));
}
public void testNoArgInCommandCall() {
doTest(new SimpleInfo[0]);
doTest();
}
public void testClosureArgs() {
doTest(new SimpleInfo[]{new SimpleInfo(0)});
doTest(new SimpleInfo(0));
}
public void testRemoveSingleClosureArgument() {
doTest(new SimpleInfo[0]);
doTest();
}
public void testNewExpr() {
@@ -243,6 +220,10 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
public void testDefaultInitializerInJava() {
doTest(new SimpleInfo("p", -1, "", "1", ""));
}
public void testChangeType() {
doTest(PsiModifier.PUBLIC, "foo", "List<String>", new SimpleInfo[0], new ThrownExceptionInfo[0], false);
}
private PsiType createType(String typeText) {
return JavaPsiFacade.getElementFactory(getProject()).createTypeByFQClassName(typeText, GlobalSearchScope.allScope(getProject()));
@@ -262,8 +243,8 @@ public class ChangeSignatureTest extends ChangeSignatureTestCase {
}
private void doTest(String newVisibility,
String newName,
String newReturnType,
@Nullable String newName,
@Nullable String newReturnType,
SimpleInfo[] parameterInfo,
ThrownExceptionInfo[] exceptionInfo,
final boolean generateDelegate) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import com.intellij.refactoring.changeSignature.ThrownExceptionInfo;
import com.intellij.refactoring.util.CanonicalTypes;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
@@ -152,11 +153,11 @@ public abstract class ChangeSignatureTestCase extends LightCodeInsightFixtureTes
this(null, oldIndex);
}
SimpleInfo(String newName, int oldIndex) {
SimpleInfo(@Nullable String newName, int oldIndex) {
this(newName, oldIndex, "", null, "");
}
SimpleInfo(String newName, int oldIndex, String defaultValue, String defaultInitializer, String type) {
SimpleInfo(String newName, int oldIndex, String defaultValue, @Nullable String defaultInitializer, String type) {
myOldIndex = oldIndex;
myNewName = newName;
myDefaultValue = defaultValue;
@@ -0,0 +1,16 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
List fo<caret>o(){return []}
@@ -0,0 +1,16 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
List<String> fo<caret>o(){return []}