compilation error fix: transform getClass()->.class (IDEA-117106)

This commit is contained in:
Anna Kozlova
2018-01-03 21:10:47 +01:00
parent 7cbfe6c753
commit 2dd4d855fb
5 changed files with 104 additions and 0 deletions
@@ -18,6 +18,7 @@ package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.daemon.impl.quickfix.ReplaceAssignmentFromVoidWithStatementIntentionAction;
import com.intellij.codeInsight.daemon.impl.quickfix.ReplaceGetClassWithClassLiteralFix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInsight.intention.impl.PriorityActionWrapper;
@@ -211,6 +212,9 @@ public class HighlightFixUtil {
if (place instanceof PsiReferenceExpression && refElement instanceof PsiField) {
QuickFixAction.registerQuickFixAction(errorResult, QUICK_FIX_FACTORY.createCreateFieldFromUsageFix((PsiReferenceExpression)place));
}
if (place instanceof PsiReferenceExpression && place.getParent() instanceof PsiMethodCallExpression) {
ReplaceGetClassWithClassLiteralFix.registerFix((PsiMethodCallExpression)place.getParent(), errorResult);
}
}
private static boolean isInstanceReference(@NotNull PsiJavaCodeReferenceElement place) {
@@ -0,0 +1,70 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
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.PsiTreeUtil;
import com.siyeh.ig.callMatcher.CallMatcher;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ReplaceGetClassWithClassLiteralFix extends LocalQuickFixAndIntentionActionOnPsiElement implements HighPriorityAction {
private String myText;
public ReplaceGetClassWithClassLiteralFix(PsiMethodCallExpression expression) {
super(expression);
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
PsiClass aClass = PsiTreeUtil.getParentOfType(startElement, PsiClass.class);
assert aClass != null;
PsiExpression classLiteral = JavaPsiFacade.getElementFactory(project).createExpressionFromText(aClass.getName() + ".class", startElement);
new CommentTracker().replaceAndRestoreComments(startElement, classLiteral);
}
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
PsiClass aClass = PsiTreeUtil.getParentOfType(startElement, PsiClass.class);
if (aClass == null) return false;
String className = aClass.getName();
if (className == null) return false;
myText = "Replace with " + className + ".class";
return super.isAvailable(project, file, startElement, endElement);
}
@NotNull
@Override
public String getText() {
return myText;
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Replace getClass() with .class literal";
}
public static void registerFix(PsiMethodCallExpression callExpression, HighlightInfo errorResult) {
if (callExpression.getMethodExpression().getQualifierExpression() == null &&
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "getClass").test(callExpression)) {
QuickFixAction.registerQuickFixAction(errorResult, new ReplaceGetClassWithClassLiteralFix(callExpression));
}
}
}
@@ -0,0 +1,7 @@
// "Replace with Test.class" "true"
class Test {
static void foo() {
System.out.println(Test.class);
}
}
@@ -0,0 +1,7 @@
// "Replace with Test.class" "true"
class Test {
static void foo() {
System.out.println(get<caret>Class());
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
public class ReplaceGetClassWithClassLiteralTest extends LightQuickFixParameterizedTestCase {
public void test() { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/getClass2ClassLiteral";
}
}