mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce parameter: substitute type parameters on call site
This commit is contained in:
+17
-2
@@ -20,6 +20,7 @@ import com.intellij.lang.Language;
|
||||
import com.intellij.lang.StdLanguages;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
@@ -64,11 +65,12 @@ public class JavaIntroduceParameterMethodUsagesProcessor implements IntroducePar
|
||||
PsiExpression[] oldArgs = argList.getExpressions();
|
||||
|
||||
final PsiExpression anchor;
|
||||
if (!data.getMethodToSearchFor().isVarArgs()) {
|
||||
final PsiMethod methodToSearchFor = data.getMethodToSearchFor();
|
||||
if (!methodToSearchFor.isVarArgs()) {
|
||||
anchor = getLast(oldArgs);
|
||||
}
|
||||
else {
|
||||
final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
|
||||
final PsiParameter[] parameters = methodToSearchFor.getParameterList().getParameters();
|
||||
if (parameters.length > oldArgs.length) {
|
||||
anchor = getLast(oldArgs);
|
||||
}
|
||||
@@ -88,6 +90,7 @@ public class JavaIntroduceParameterMethodUsagesProcessor implements IntroducePar
|
||||
else {
|
||||
PsiElement initializer =
|
||||
ExpressionConverter.getExpression(data.getParameterInitializer().getExpression(), StdLanguages.JAVA, data.getProject());
|
||||
substituteTypeParametersInInitializer(initializer, callExpression, argList, methodToSearchFor);
|
||||
assert initializer instanceof PsiExpression;
|
||||
ChangeContextUtil.encodeContextInfo(initializer, true);
|
||||
PsiExpression newArg = (PsiExpression)argList.addAfter(initializer, anchor);
|
||||
@@ -106,6 +109,18 @@ public class JavaIntroduceParameterMethodUsagesProcessor implements IntroducePar
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void substituteTypeParametersInInitializer(PsiElement initializer,
|
||||
PsiCall callExpression,
|
||||
PsiExpressionList argList,
|
||||
PsiMethod method) {
|
||||
final Project project = method.getProject();
|
||||
final PsiSubstitutor psiSubstitutor = JavaPsiFacade.getInstance(project).getResolveHelper()
|
||||
.inferTypeArguments(method.getTypeParameters(), method.getParameterList().getParameters(),
|
||||
argList.getExpressions(), PsiSubstitutor.EMPTY, callExpression, false);
|
||||
RefactoringUtil.replaceMovedMemberTypeParameters(initializer, PsiUtil.typeParametersIterable(method), psiSubstitutor,
|
||||
JavaPsiFacade.getElementFactory(project));
|
||||
}
|
||||
|
||||
private static void removeParametersFromCall(@NotNull final PsiExpressionList argList, TIntArrayList parametersToRemove) {
|
||||
final PsiExpression[] exprs = argList.getExpressions();
|
||||
parametersToRemove.forEachDescending(new TIntProcedure() {
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import java.util.*;
|
||||
class Test {
|
||||
<T> void foo(T t, final ArrayList<T> anObject) {
|
||||
List<T> ls = anObject;
|
||||
}
|
||||
|
||||
void bar() {
|
||||
String s = "";
|
||||
foo(s, new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import java.util.*;
|
||||
class Test {
|
||||
<T> void foo(T t) {
|
||||
List<T> ls = <selection>new ArrayList<T>()</selection>;
|
||||
}
|
||||
|
||||
void bar() {
|
||||
String s = "";
|
||||
foo(s);
|
||||
}
|
||||
}
|
||||
@@ -276,6 +276,10 @@ public class IntroduceParameterTest extends LightCodeInsightTestCase {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
|
||||
}
|
||||
|
||||
public void testSubstituteTypeParams() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
|
||||
}
|
||||
|
||||
private void doTestThroughHandler() throws Exception {
|
||||
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
|
||||
boolean enabled = true;
|
||||
|
||||
@@ -292,7 +292,7 @@ public class GenericsUtil {
|
||||
substitutor = substitutor.put(typeParameter, toPut);
|
||||
}
|
||||
final PsiAnnotation[] applicableAnnotations = classType.getApplicableAnnotations();
|
||||
if (substitutor == PsiSubstitutor.EMPTY && !toExtend && applicableAnnotations.length == 0) return classType;
|
||||
if (substitutor == PsiSubstitutor.EMPTY && !toExtend && applicableAnnotations.length == 0 && !(aClass instanceof PsiTypeParameter)) return classType;
|
||||
PsiManager manager = aClass.getManager();
|
||||
PsiType result = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory()
|
||||
.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass), applicableAnnotations);
|
||||
|
||||
Reference in New Issue
Block a user