diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java index 1972758db60e..05d4ab349249 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java @@ -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); diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java new file mode 100644 index 000000000000..bce942363796 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFromCallFix.java @@ -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)); + } + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/after1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/after1.java new file mode 100644 index 000000000000..b3a7ffeff88c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/after1.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'List'" "true" +public class Test { + void foo() { + List list = new List(); + list.add(new Integer(0)); + } +} + +class List { + void add(T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterAnotherParamTypeChange.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterAnotherParamTypeChange.java new file mode 100644 index 000000000000..23ec904260b4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterAnotherParamTypeChange.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'Lost'" "true" +public class Test { + void foo() { + Lost list = new Lost(); + list.addd(new Lost(), new Integer(9)); + } +} + +class Lost { + void addd(Lost lt, T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterBoxing.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterBoxing.java new file mode 100644 index 000000000000..c1f8fb6f4a0f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterBoxing.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'List'" "true" +public class Test { + void foo() { + List list = new List(); + list.add(42); + } +} + +class List { + void add(T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterCompound.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterCompound.java new file mode 100644 index 000000000000..8713ac5b24d8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterCompound.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'Lost'" "true" +public class Test { + void foo() { + Lost list = new Lost(); + list.add(new Lost()); + } +} + +class Lost { + void add(Lost t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterMixed.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterMixed.java new file mode 100644 index 000000000000..b9fd96255dea --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/afterMixed.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'Lost'" "true" +public class Test { + void foo() { + Lost list = new Lost(); + list.add(new Lost(), new Integer(42)); + } +} + +class Lost { + void add(Lost lt, T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/before1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/before1.java new file mode 100644 index 000000000000..c14b8344e1e5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/before1.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'List'" "true" +public class Test { + void foo() { + List list = new List(); + list.add(new Integer(0)); + } +} + +class List { + void add(T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeAnotherParamTypeChange.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeAnotherParamTypeChange.java new file mode 100644 index 000000000000..61e868401f9d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeAnotherParamTypeChange.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'Lost'" "true" +public class Test { + void foo() { + Lost list = new Lost(); + list.addd(new Lost(), new Integer(9)); + } +} + +class Lost { + void addd(Lost lt, T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeBoxing.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeBoxing.java new file mode 100644 index 000000000000..1d02b919f259 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeBoxing.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'List'" "true" +public class Test { + void foo() { + List list = new List(); + list.add(42); + } +} + +class List { + void add(T t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeCompound.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeCompound.java new file mode 100644 index 000000000000..aa2eb0c34b5b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeCompound.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'Lost'" "true" +public class Test { + void foo() { + Lost list = new Lost(); + list.add(new Loster>()); + } +} + +class Lost { + void add(Lost t){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeMixed.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeMixed.java new file mode 100644 index 000000000000..b0d6caba242e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/varTypeFromCall/beforeMixed.java @@ -0,0 +1,11 @@ +// "Change 'list' type to 'Lost'" "true" +public class Test { + void foo() { + Lost list = new Lost(); + list.add(new Loster>(), new Integer(42)); + } +} + +class Lost { + void add(Lost lt, T t){} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/VariableTypeFromCallTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/VariableTypeFromCallTest.java new file mode 100644 index 000000000000..2bb0d56a0f09 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/VariableTypeFromCallTest.java @@ -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"; + } + +} +