qualify unresolved refs with static accessor fixes (IDEA-168929)

This commit is contained in:
Anna Kozlova
2017-06-27 13:20:39 +03:00
parent 6cb35eac1d
commit 0314bd07e8
10 changed files with 144 additions and 3 deletions
@@ -203,6 +203,7 @@ public abstract class QuickFixFactory {
@NotNull public abstract IntentionAction createCreateGetterSetterPropertyFromUsageFix(@NotNull PsiMethodCallExpression call);
@NotNull public abstract IntentionAction createStaticImportMethodFix(@NotNull PsiMethodCallExpression call);
@NotNull public abstract IntentionAction createQualifyStaticMethodCallFix(@NotNull PsiMethodCallExpression call);
@NotNull public abstract IntentionAction createReplaceAddAllArrayToCollectionFix(@NotNull PsiMethodCallExpression call);
@@ -461,6 +461,8 @@ public class HighlightMethodUtil {
QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createCreateAbstractMethodFromUsageFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createCreatePropertyFromUsageFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createStaticImportMethodFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createQualifyStaticMethodCallFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.addMethodQualifierFix(methodCall));
if (resolved instanceof PsiVariable && languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(((PsiVariable)resolved).getType());
if (method != null) {
@@ -854,6 +856,7 @@ public class HighlightMethodUtil {
registerChangeParameterClassFix(methodCall, list, highlightInfo);
if (methodCandidates.length == 0) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createStaticImportMethodFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createQualifyStaticMethodCallFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.addMethodQualifierFix(methodCall));
}
for (IntentionAction action : QUICK_FIX_FACTORY.getVariableTypeFromCallFixes(methodCall, list)) {
@@ -455,6 +455,12 @@ public class EmptyQuickFixFactory extends QuickFixFactory {
return QuickFixes.EMPTY_FIX;
}
@NotNull
@Override
public IntentionAction createQualifyStaticMethodCallFix(@NotNull PsiMethodCallExpression call) {
return QuickFixes.EMPTY_FIX;
}
@NotNull
@Override
public IntentionAction createReplaceAddAllArrayToCollectionFix(@NotNull PsiMethodCallExpression psiMethodCallExpression) {
@@ -44,6 +44,7 @@ public class DefaultQuickFixProvider extends UnresolvedReferenceQuickFixProvider
registrar.register(new ImportClassFix(ref));
registrar.register(new StaticImportConstantFix(ref));
registrar.register(new QualifyStaticConstantFix(ref));
registrar.register(QuickFixFactory.getInstance().createSetupJDKFix());
OrderEntryFix.registerFixes(registrar, ref);
@@ -0,0 +1,59 @@
/*
* 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.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiReferenceExpression;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class QualifyStaticConstantFix extends StaticImportConstantFix {
public QualifyStaticConstantFix(@NotNull PsiJavaCodeReferenceElement referenceElement) {
super(referenceElement);
}
@NotNull
@Override
protected String getBaseText() {
return "Qualify static constant access";
}
@NotNull
@Override
protected StaticImportMethodQuestionAction<PsiField> createQuestionAction(List<PsiField> fieldsToImport,
@NotNull Project project,
Editor editor) {
return new StaticImportMethodQuestionAction<PsiField>(project, editor, fieldsToImport, myRef) {
@NotNull
@Override
protected String getPopupTitle() {
return QuickFixBundle.message("field.to.import.chooser.title");
}
@Override
protected void doImport(PsiField toImport) {
PsiJavaCodeReferenceElement element = myRef.getElement();
if (!(element instanceof PsiReferenceExpression)) return;
QualifyStaticMethodCallFix.qualifyStatically(toImport, project, (PsiReferenceExpression)element);
}
};
}
}
@@ -0,0 +1,65 @@
/*
* 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.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class QualifyStaticMethodCallFix extends StaticImportMethodFix {
public QualifyStaticMethodCallFix(@NotNull PsiMethodCallExpression methodCallExpression) {
super(methodCallExpression);
}
@NotNull
@Override
protected String getBaseText() {
return "Qualify static call";
}
@NotNull
@Override
protected StaticImportMethodQuestionAction<PsiMethod> createQuestionAction(List<PsiMethod> methodsToImport,
@NotNull Project project,
Editor editor) {
return new StaticImportMethodQuestionAction<PsiMethod>(project, editor, methodsToImport, myMethodCall) {
@Override
protected void doImport(PsiMethod toImport) {
PsiMethodCallExpression element = myMethodCall.getElement();
if (element == null) return;
qualifyStatically(toImport, project, element.getMethodExpression());
}
};
}
public static void qualifyStatically(PsiMember toImport,
Project project,
PsiReferenceExpression qualifiedExpression) {
PsiClass containingClass = toImport.getContainingClass();
if (containingClass == null) return;
PsiReferenceExpression qualifier = JavaPsiFacade.getElementFactory(project).createReferenceExpression(containingClass);
WriteCommandAction.runWriteCommandAction(project, "Qualify Static Access", null, () -> {
qualifiedExpression.setQualifierExpression(qualifier);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(qualifiedExpression);
}
);
}
}
@@ -31,7 +31,7 @@ import java.util.Collections;
import java.util.List;
public class StaticImportConstantFix extends StaticImportMemberFix<PsiField> {
private final SmartPsiElementPointer<PsiJavaCodeReferenceElement> myRef;
protected final SmartPsiElementPointer<PsiJavaCodeReferenceElement> myRef;
public StaticImportConstantFix(@NotNull PsiJavaCodeReferenceElement referenceElement) {
myRef = SmartPointerManager.getInstance(referenceElement.getProject()).createSmartPsiElementPointer(referenceElement);
@@ -32,7 +32,7 @@ import java.util.Collections;
import java.util.List;
public class StaticImportMethodFix extends StaticImportMemberFix<PsiMethod> {
private final SmartPsiElementPointer<PsiMethodCallExpression> myMethodCall;
protected final SmartPsiElementPointer<PsiMethodCallExpression> myMethodCall;
public StaticImportMethodFix(@NotNull PsiMethodCallExpression methodCallExpression) {
myMethodCall = SmartPointerManager.getInstance(methodCallExpression.getProject()).createSmartPsiElementPointer(methodCallExpression);
@@ -87,7 +87,7 @@ public class StaticImportMethodQuestionAction<T extends PsiMember> implements Qu
return true;
}
private void doImport(final T toImport) {
protected void doImport(final T toImport) {
final Project project = toImport.getProject();
final PsiElement element = myRef.getElement();
if (element == null) return;
@@ -535,6 +535,12 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
return new StaticImportMethodFix(call);
}
@NotNull
@Override
public IntentionAction createQualifyStaticMethodCallFix(@NotNull PsiMethodCallExpression call) {
return new QualifyStaticMethodCallFix(call);
}
@NotNull
@Override
public IntentionAction createReplaceAddAllArrayToCollectionFix(@NotNull PsiMethodCallExpression call) {