pull up: postpone type params replacement: substituted types can contain type params from base class with names that exist in current class (IDEA-87543)

This commit is contained in:
Anna Kozlova
2012-06-19 14:04:30 +04:00
parent bcc15a98f0
commit 4b5ba694dc
4 changed files with 29 additions and 2 deletions
@@ -891,6 +891,7 @@ public class RefactoringUtil {
final Iterable<PsiTypeParameter> parametersIterable,
final PsiSubstitutor substitutor,
final PsiElementFactory factory) {
final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<PsiElement, PsiElement>();
for (PsiTypeParameter parameter : parametersIterable) {
PsiType substitutedType = substitutor.substitute(parameter);
if (substitutedType == null) {
@@ -900,12 +901,17 @@ public class RefactoringUtil {
final PsiElement element = reference.getElement();
final PsiElement parent = element.getParent();
if (parent instanceof PsiTypeElement) {
parent.replace(factory.createTypeElement(substitutedType));
replacement.put(parent, factory.createTypeElement(substitutedType));
} else if (element instanceof PsiJavaCodeReferenceElement && substitutedType instanceof PsiClassType) {
element.replace(factory.createReferenceElementByType((PsiClassType)substitutedType));
replacement.put(element, factory.createReferenceElementByType((PsiClassType)substitutedType));
}
}
}
for (PsiElement element : replacement.keySet()) {
if (element.isValid()) {
element.replace(replacement.get(element));
}
}
}
@Nullable
@@ -0,0 +1,7 @@
interface Bar<T> { }
interface Base<T> { }
class Foo<T,U> implements Base<U> {
void fo<caret>o(Bar<U> bar) { }
}
@@ -0,0 +1,10 @@
interface Bar<T> { }
interface Base<T> {
void foo(Bar<T> bar);
}
class Foo<T,U> implements Base<U> {
@Override
public void foo(Bar<U> bar) { }
}
@@ -115,6 +115,10 @@ public class PullUpTest extends LightRefactoringTestCase {
doTest(false, new RefactoringTestUtil.MemberDescriptor("I", PsiClass.class));
}
public void testTypeParamsConflictingNames() throws Exception {
doTest(false, new RefactoringTestUtil.MemberDescriptor("foo", PsiMethod.class));
}
private void doTest(RefactoringTestUtil.MemberDescriptor... membersToFind) throws Exception {
doTest(true, membersToFind);
}