make method return fix: make available for constructor calls; java 8 specific tests; simplify inference

This commit is contained in:
Anna Kozlova
2015-08-20 21:47:30 +02:00
parent c014fe6eb4
commit 7cf142b351
13 changed files with 57 additions and 31 deletions
@@ -41,8 +41,6 @@ import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.util.*;
import com.intellij.refactoring.util.RefactoringChangeUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MostlySingularMultiMap;
import com.intellij.util.ui.UIUtil;
import com.intellij.xml.util.XmlStringUtil;
@@ -402,7 +400,7 @@ public class HighlightMethodUtil {
.description(description).escapedToolTip(toolTip).navigationShift(navigationShift).create();
if (highlightInfo != null) {
registerMethodCallIntentions(highlightInfo, methodCall, list, resolveHelper);
registerMethodReturnFixAction(highlightInfo, candidateInfo, methodCall, resolveHelper);
registerMethodReturnFixAction(highlightInfo, candidateInfo, methodCall);
}
}
else {
@@ -440,38 +438,14 @@ public class HighlightMethodUtil {
private static void registerMethodReturnFixAction(HighlightInfo highlightInfo,
MethodCandidateInfo candidate,
PsiMethodCallExpression methodCall,
PsiResolveHelper resolveHelper) {
PsiCall methodCall) {
if (methodCall.getParent() instanceof PsiReturnStatement) {
final PsiMethod containerMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true, PsiLambdaExpression.class);
if (containerMethod != null) {
final PsiMethod method = candidate.getElement();
final PsiType[] expressionTypes = methodCall.getArgumentList().getExpressionTypes();
final PsiParameter[] parameters = method.getParameterList().getParameters();
final int max = Math.max(parameters.length, expressionTypes.length);
PsiType[] leftTypes = PsiType.createArray(max);
PsiType[] rightTypes = PsiType.createArray(max);
for (int i = 0; i < max; i++) {
if (i < parameters.length) {
leftTypes[i] = parameters[i].getType();
rightTypes[i] = i < expressionTypes.length ? expressionTypes[i] : null;
if (leftTypes[i] instanceof PsiEllipsisType &&
!(max == expressionTypes.length &&
expressionTypes[i] instanceof PsiArrayType &&
expressionTypes[i].getArrayDimensions() == leftTypes[i].getArrayDimensions())) {
leftTypes[i] = ((PsiEllipsisType)leftTypes[i]).getComponentType();
}
}
else {
leftTypes[i] = null;
rightTypes[i] = i < expressionTypes.length ? expressionTypes[i] : null;
}
}
final PsiSubstitutor substitutor = resolveHelper
.inferTypeArguments(method.getTypeParameters(), leftTypes, rightTypes,
PsiUtil.getLanguageLevel(methodCall));
PsiType methodCallTypeByArgs = substitutor.substitute(methodCall.getType());
final PsiExpression methodCallCopy =
JavaPsiFacade.getElementFactory(method.getProject()).createExpressionFromText(methodCall.getText(), methodCall);
PsiType methodCallTypeByArgs = methodCallCopy.getType();
//ensure type params are not included
methodCallTypeByArgs = JavaPsiFacade.getElementFactory(method.getProject())
.createRawSubstitutor(method).substitute(methodCallTypeByArgs);
@@ -1547,6 +1521,7 @@ public class HighlightMethodUtil {
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(infoElement).description(description).escapedToolTip(toolTip).navigationShift(+1).create();
if (info != null) {
registerFixesOnInvalidConstructorCall(constructorCall, classReference, list, aClass, constructors, results, infoElement, info);
registerMethodReturnFixAction(info, result, constructorCall);
holder.add(info);
}
}
@@ -0,0 +1,8 @@
// "Make 'bar' return 'Foo'" "true"
public class Foo {
public Foo(int i) {}
Foo bar() {
return new Foo("");
}
}
@@ -0,0 +1,8 @@
// "Make 'bar' return 'Foo'" "true"
public class Foo {
public Foo(int i) {}
String bar() {
return new Foo<caret>("");
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2000-2015 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;
import com.intellij.pom.java.LanguageLevel;
public class MethodReturn8Test extends LightQuickFixParameterizedTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/methodReturn8";
}
@Override
protected LanguageLevel getLanguageLevel() {
return LanguageLevel.JDK_1_8;
}
}