suggest wrap string expression with java.io.File when it's expected IDEA-167566

This commit is contained in:
Dmitry Batkovich
2017-02-09 12:19:11 +03:00
parent fa6fbc3b84
commit aa67d1e6e2
16 changed files with 279 additions and 1 deletions
@@ -286,4 +286,7 @@ public abstract class QuickFixFactory {
@NotNull
public abstract IntentionAction createInsertMethodCallFix(@NotNull PsiMethodCallExpression call, PsiMethod method);
@NotNull
public abstract IntentionAction createWrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression);
}
@@ -628,6 +628,7 @@ public class HighlightMethodUtil {
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info);
registerChangeParameterClassFix(methodCall, list, info);
@@ -716,6 +717,7 @@ public class HighlightMethodUtil {
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
WrapStringWithFileFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info);
registerChangeParameterClassFix(methodCall, list, info);
@@ -763,6 +765,7 @@ public class HighlightMethodUtil {
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapStringWithFileFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
registerMethodAccessLevelIntentions(methodCandidates, methodCall, list, highlightInfo);
registerChangeMethodSignatureFromUsageIntentions(methodCandidates, list, highlightInfo, fixRange);
RemoveRedundantArgumentsFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
@@ -564,6 +564,7 @@ public class HighlightUtil extends HighlightUtilBase {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapLongWithMathToIntExactFix(lType, expression));
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapWithOptionalFix(lType, expression));
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapExpressionFix(lType, expression));
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createWrapStringWithFileFix(lType, expression));
AddTypeArgumentsConditionalFix.register(highlightInfo, expression, lType);
registerCollectionToArrayFixAction(highlightInfo, rType, lType, expression);
}
@@ -46,5 +46,6 @@ public class ConstructorParametersFixer {
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(candidates, constructorCall, highlightInfo, fixRange);
WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(candidates, constructorCall, highlightInfo, fixRange);
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, constructorCall, highlightInfo, fixRange);
WrapStringWithFileFix.REGISTAR.registerCastActions(candidates, constructorCall, highlightInfo, fixRange);
}
}
@@ -0,0 +1,129 @@
/*
* Copyright 2000-2017 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.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
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.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class WrapStringWithFileFix extends LocalQuickFixAndIntentionActionOnPsiElement implements HighPriorityAction {
public final static MyMethodArgumentFixerFactory REGISTAR = new MyMethodArgumentFixerFactory();
@Nullable private final PsiType myType;
public WrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
super(expression);
myType = type;
}
@Nls
@NotNull
@Override
public String getText() {
return getFamilyName();
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return QuickFixBundle.message("wrap.with.java.io.file.text");
}
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
return myType != null &&
myType.isValid() &&
myType.equalsToText(CommonClassNames.JAVA_IO_FILE) &&
startElement.isValid() &&
startElement.getManager().isInProject(startElement) &&
isStringType(startElement);
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
startElement.replace(getModifiedExpression(startElement));
}
private static boolean isStringType(@NotNull PsiElement expression) {
if (!(expression instanceof PsiExpression)) return false;
final PsiType type = ((PsiExpression) expression).getType();
if (type == null) return false;
return type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
private static PsiElement getModifiedExpression(@NotNull PsiElement expression) {
return JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(PsiKeyword.NEW + " " + CommonClassNames.JAVA_IO_FILE + "(" + expression.getText() + ")", expression);
}
private static class MyMethodArgumentFix extends MethodArgumentFix implements HighPriorityAction {
protected MyMethodArgumentFix(@NotNull PsiExpressionList list,
int i,
@NotNull PsiType toType,
@NotNull ArgumentFixerActionFactory fixerActionFactory) {
super(list, i, toType, fixerActionFactory);
}
@Nls
@NotNull
@Override
public String getText() {
return myArgList.getExpressions().length == 1
? QuickFixBundle.message("wrap.with.java.io.file.parameter.single.text")
: QuickFixBundle.message("wrap.with.java.io.file.parameter.multiple.text", myIndex + 1);
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return PsiUtil.isLanguageLevel8OrHigher(file) && super.isAvailable(project, editor, file);
}
}
public static class MyMethodArgumentFixerFactory extends ArgumentFixerActionFactory {
@Nullable
@Override
protected PsiExpression getModifiedArgument(final PsiExpression expression, final PsiType toType) throws IncorrectOperationException {
return isStringType(expression) && toType.equalsToText(CommonClassNames.JAVA_IO_FILE) ? (PsiExpression)getModifiedExpression(expression) : null;
}
@Override
public boolean areTypesConvertible(@NotNull final PsiType exprType, @NotNull final PsiType parameterType, @NotNull final PsiElement context) {
return parameterType.isConvertibleFrom(exprType) || (parameterType.equalsToText(CommonClassNames.JAVA_IO_FILE) && exprType.equalsToText(CommonClassNames.JAVA_LANG_STRING));
}
@Override
public MethodArgumentFix createFix(final PsiExpressionList list, final int i, final PsiType toType) {
return new MyMethodArgumentFix(list, i, toType, this);
}
}
}
@@ -655,4 +655,10 @@ public class EmptyQuickFixFactory extends QuickFixFactory {
public IntentionAction createInsertMethodCallFix(@NotNull PsiMethodCallExpression call, PsiMethod method) {
return QuickFixes.EMPTY_FIX;
}
@NotNull
@Override
public IntentionAction createWrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
return QuickFixes.EMPTY_FIX;
}
}
@@ -837,4 +837,10 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
public IntentionAction createInsertMethodCallFix(@NotNull PsiMethodCallExpression call, PsiMethod method) {
return new InsertMethodCallFix(call, method);
}
@NotNull
@Override
public IntentionAction createWrapStringWithFileFix(@Nullable PsiType type, @NotNull PsiExpression expression) {
return new WrapStringWithFileFix(type, expression);
}
}
@@ -0,0 +1,14 @@
// "Wrap parameter using 'new File()'" "true"
import java.io.File;
class Test {
void m() {
new FileReader(new File("my.txt"));
}
}
class FileReader {
public FileReader(File file) {
}
}
@@ -0,0 +1,14 @@
// "Wrap 2nd parameter using 'new File()'" "true"
import java.io.File;
class Test {
void m() {
readFile(0, new File("my.txt"), 2);
}
static String readFile(int additionalParameter1, File f, int additionalParameter2) {
return null;
}
}
@@ -0,0 +1,14 @@
// "Wrap parameter using 'new File()'" "true"
import java.io.File;
class Test {
void m() {
readFile(new File("my.txt"));
}
static String readFile(File f) {
return null;
}
}
@@ -0,0 +1,14 @@
// "Wrap parameter using 'new File()'" "true"
import java.io.File;
class Test {
void m() {
new FileReader("m<caret>y.txt");
}
}
class FileReader {
public FileReader(File file) {
}
}
@@ -0,0 +1,14 @@
// "Wrap 2nd parameter using 'new File()'" "true"
import java.io.File;
class Test {
void m() {
readFile(0, "m<caret>y.txt", 2);
}
static String readFile(int additionalParameter1, File f, int additionalParameter2) {
return null;
}
}
@@ -0,0 +1,14 @@
// "Wrap parameter using 'new File()'" "false"
import java.io.File;
class Test {
void m(CharSequence sequence) {
readFile(sequen<caret>ce);
}
static String readFile(File f) {
return null;
}
}
@@ -0,0 +1,14 @@
// "Wrap parameter using 'new File()'" "true"
import java.io.File;
class Test {
void m() {
readFile("my<caret>.txt");
}
static String readFile(File f) {
return null;
}
}
@@ -0,0 +1,27 @@
/*
* Copyright 2000-2017 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 WrapStringWithFileFixTest extends LightQuickFixParameterizedTestCase {
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/wrapStringWithFile";
}
}
@@ -304,4 +304,8 @@ collection.to.array.text=Apply conversion ''.toArray({0})''
collection.to.array.family.name=Apply conversion '.toArray()'
insert.sam.method.call.fix.name=Insert ''.{0}'' to call functional interface method
insert.sam.method.call.fix.family.name=Insert single abstract method call
insert.sam.method.call.fix.family.name=Insert single abstract method call
wrap.with.java.io.file.text=Wrap using 'new File()'
wrap.with.java.io.file.parameter.single.text=Wrap parameter using 'new File()'
wrap.with.java.io.file.parameter.multiple.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} parameter using ''new File()''