change var type args from call

This commit is contained in:
anna
2010-06-11 13:36:05 +04:00
parent aa30bba068
commit 22a3ff424b
13 changed files with 253 additions and 0 deletions
@@ -495,6 +495,7 @@ public class HighlightMethodUtil {
if (methodCandidates.length == 0) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new StaticImportMethodFix(methodCall), null);
}
VariableTypeFromCallFix.registerQuickFixActions(methodCall, list, highlightInfo);
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new ReplaceAddAllArrayToCollectionFix(methodCall), null);
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new SurroundWithArrayFix(methodCall), null);
@@ -0,0 +1,114 @@
/*
* Copyright 2000-2010 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.
*/
/*
* User: anna
* Date: 10-Jun-2010
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.typeMigration.TypeMigrationLabeler;
import com.intellij.refactoring.typeMigration.TypeMigrationProcessor;
import com.intellij.refactoring.typeMigration.TypeMigrationRules;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public class VariableTypeFromCallFix implements IntentionAction {
private final PsiType myExpressionType;
private final PsiVariable myVar;
public VariableTypeFromCallFix(PsiClassType type, PsiVariable var) {
myExpressionType = type;
myVar = var;
}
@NotNull
public String getText() {
return QuickFixBundle.message("fix.variable.type.text",
myVar.getName(),
myExpressionType.getCanonicalText());
}
@NotNull
public String getFamilyName() {
return QuickFixBundle.message("fix.variable.type.family");
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return true;
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final TypeMigrationRules rules = new TypeMigrationRules(TypeMigrationLabeler.getElementType(myVar));
rules.setMigrationRootType(myExpressionType);
rules.setBoundScope(myVar.getUseScope());
final TypeMigrationProcessor processor = new TypeMigrationProcessor(project, myVar, rules);
processor.setPreviewUsages(!ApplicationManager.getApplication().isUnitTestMode());
processor.run();
}
public boolean startInWriteAction() {
return true;
}
public static void registerQuickFixActions(PsiMethodCallExpression methodCall, PsiExpressionList list, HighlightInfo highlightInfo) {
final JavaResolveResult result = methodCall.getMethodExpression().advancedResolve(false);
PsiMethod method = (PsiMethod) result.getElement();
final PsiSubstitutor substitutor = result.getSubstitutor();
PsiExpression[] expressions = list.getExpressions();
if (method == null || method.getParameterList().getParametersCount() != expressions.length) return;
for (int i = 0; i < expressions.length; i++) {
final PsiExpression expression = expressions[i];
PsiType expressionType = expression.getType();
if (expressionType instanceof PsiPrimitiveType) {
expressionType = ((PsiPrimitiveType)expressionType).getBoxedType(expression);
}
if (expressionType == null) continue;
final PsiParameter parameter = method.getParameterList().getParameters()[i];
final PsiType formalParamType = parameter.getType();
final PsiType parameterType = substitutor.substitute(formalParamType);
if (parameterType.isAssignableFrom(expressionType)) continue;
final PsiExpression qualifierExpression = methodCall.getMethodExpression().getQualifierExpression();
if (!(qualifierExpression instanceof PsiReferenceExpression)) {
continue;
}
final PsiElement resolved = ((PsiReferenceExpression)qualifierExpression).resolve();
if (resolved instanceof PsiVariable) {
final PsiClass varClass = PsiUtil.resolveClassInType(((PsiVariable)resolved).getType());
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(expression.getProject()).getResolveHelper();
if (varClass != null) {
final PsiSubstitutor psiSubstitutor = resolveHelper.inferTypeArguments(varClass.getTypeParameters(),
new PsiParameter[]{parameter},
new PsiExpression[]{expression}, PsiSubstitutor.EMPTY, resolved, false);
final PsiClassType appropriateVarType = JavaPsiFacade.getElementFactory(expression.getProject()).createType(varClass, psiSubstitutor);
QuickFixAction.registerQuickFixAction(highlightInfo, new VariableTypeFromCallFix(appropriateVarType, (PsiVariable) resolved));
}
}
}
}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'List<java.lang.Integer>'" "true"
public class Test {
void foo() {
List<Integer> list = new List<Integer>();
list.add(new Integer(0));
}
}
class List<T> {
void add(T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'Lost<java.lang.Integer>'" "true"
public class Test {
void foo() {
Lost<Integer> list = new Lost<Integer>();
list.addd(new Lost<Integer>(), new Integer(9));
}
}
class Lost<T> {
void addd(Lost<T> lt, T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'List<java.lang.Integer>'" "true"
public class Test {
void foo() {
List<Integer> list = new List<Integer>();
list.add(42);
}
}
class List<T> {
void add(T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'Lost<java.lang.Integer>'" "true"
public class Test {
void foo() {
Lost<Integer> list = new Lost<Integer>();
list.add(new Lost<Integer>());
}
}
class Lost<T> {
void add(Lost<T> t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'Lost<java.lang.Integer>'" "true"
public class Test {
void foo() {
Lost<Integer> list = new Lost<Integer>();
list.add(new Lost<Integer>(), new Integer(42));
}
}
class Lost<T> {
void add(Lost<T> lt, T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'List<java.lang.Integer>'" "true"
public class Test {
void foo() {
List<String> list = new List<String>();
list.add(new In<caret>teger(0));
}
}
class List<T> {
void add(T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'Lost<java.lang.Integer>'" "true"
public class Test {
void foo() {
Lost<String> list = new Lost<String>();
list.addd(new Lost<Integer>(), new Int<caret>eger(9));
}
}
class Lost<T> {
void addd(Lost<T> lt, T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'List<java.lang.Integer>'" "true"
public class Test {
void foo() {
List<String> list = new List<String>();
list.add(4<caret>2);
}
}
class List<T> {
void add(T t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'Lost<java.lang.Integer>'" "true"
public class Test {
void foo() {
Lost<String> list = new Lost<String>();
list.add(new Lost<Integ<caret>er>());
}
}
class Lost<T> {
void add(Lost<T> t){}
}
@@ -0,0 +1,11 @@
// "Change 'list' type to 'Lost<java.lang.Integer>'" "true"
public class Test {
void foo() {
Lost<String> list = new Lost<String>();
list.add(new Lost<Integ<caret>er>(), new Integer(42));
}
}
class Lost<T> {
void add(Lost<T> lt, T t){}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2010 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.
*/
package com.intellij.codeInsight.daemon.quickFix;
public class VariableTypeFromCallTest extends LightQuickFixTestCase {
public void test() throws Exception { doAllTests(); }
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall";
}
}