mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fix IDEA-112499 for single member static imports
This commit is contained in:
+28
-11
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2014 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.
|
||||
@@ -21,7 +21,7 @@ package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -29,12 +29,14 @@ import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAction {
|
||||
public class AddSingleMemberStaticImportAction extends BaseElementAtCaretIntentionAction {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.intention.impl.AddSingleMemberStaticImportAction");
|
||||
private static final Key<PsiElement> TEMP_REFERENT_USER_DATA = new Key<PsiElement>("TEMP_REFERENT_USER_DATA");
|
||||
|
||||
@@ -51,7 +53,7 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
* @return not-null qualified name of the class which method may be statically imported if any; <code>null</code> otherwise
|
||||
*/
|
||||
@Nullable
|
||||
public static String getStaticImportClass(@NotNull PsiElement element, boolean useExisting) {
|
||||
public static String getStaticImportClass(@NotNull PsiElement element) {
|
||||
if (!PsiUtil.isLanguageLevel5OrHigher(element)) return null;
|
||||
if (element instanceof PsiIdentifier) {
|
||||
final PsiElement parent = element.getParent();
|
||||
@@ -64,12 +66,27 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
if (resolved instanceof PsiMember && ((PsiModifierListOwner)resolved).hasModifierProperty(PsiModifier.STATIC)) {
|
||||
PsiClass aClass = getResolvedClass(element, (PsiMember)resolved);
|
||||
if (aClass != null && !PsiTreeUtil.isAncestor(aClass, element, true) && !aClass.hasModifierProperty(PsiModifier.PRIVATE)) {
|
||||
if (findExistingImport(element.getContainingFile(), aClass, refExpr.getReferenceName()) == null || useExisting) {
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null && !Comparing.strEqual(qName, aClass.getName())) {
|
||||
return qName + "." +refExpr.getReferenceName();
|
||||
final PsiElement gParent = refExpr.getParent();
|
||||
if (gParent instanceof PsiMethodCallExpression) {
|
||||
final PsiMethodCallExpression call = (PsiMethodCallExpression)gParent.copy();
|
||||
final PsiElement qualifier = call.getMethodExpression().getQualifier();
|
||||
if (qualifier == null) return null;
|
||||
qualifier.delete();
|
||||
final PsiMethod method = call.resolveMethod();
|
||||
if (method != null && method.getContainingClass() != aClass) return null;
|
||||
}
|
||||
else {
|
||||
final PsiJavaCodeReferenceElement copy = (PsiJavaCodeReferenceElement)refExpr.copy();
|
||||
final PsiElement qualifier = copy.getQualifier();
|
||||
if (qualifier == null) return null;
|
||||
qualifier.delete();
|
||||
final PsiElement target = copy.resolve();
|
||||
if (target != null && PsiTreeUtil.getParentOfType(target, PsiClass.class) != aClass) return null;
|
||||
}
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null && !Comparing.strEqual(qName, aClass.getName())) {
|
||||
return qName + "." +refExpr.getReferenceName();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,7 +140,7 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
String classQName = getStaticImportClass(element, false);
|
||||
String classQName = getStaticImportClass(element);
|
||||
if (classQName != null) {
|
||||
setText(CodeInsightBundle.message("intention.add.single.member.static.import.text", classQName));
|
||||
}
|
||||
|
||||
+1
-1
@@ -119,7 +119,7 @@ public class ShortenToStaticImportProcessor implements TemplateOptionalProcessor
|
||||
private static class SingleMemberStaticImporter implements StaticImporter {
|
||||
@Override
|
||||
public boolean canPerform(@NotNull PsiElement element) {
|
||||
return AddSingleMemberStaticImportAction.getStaticImportClass(element, true) != null;
|
||||
return AddSingleMemberStaticImportAction.getStaticImportClass(element) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Add static import for 'java.lang.Math.abs'" "true"
|
||||
package test;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
public class X {{
|
||||
abs(1.0);
|
||||
}}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
// "Add static import for 'test.Bar.f'" "true"
|
||||
package test;
|
||||
|
||||
import static test.Bar.f;
|
||||
|
||||
class Bar {
|
||||
public static final void f() {}
|
||||
}
|
||||
public class Foo {
|
||||
public static final void f(int i) {}
|
||||
{
|
||||
Bar.<caret>f();
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
// "Add static import for 'test.Bar.f'" "true"
|
||||
package test;
|
||||
|
||||
import static test.Bar.f;
|
||||
|
||||
class Bar {
|
||||
public static final void f() {}
|
||||
}
|
||||
public class Foo extends FooSuper{
|
||||
{
|
||||
Bar.<caret>f();
|
||||
}
|
||||
}
|
||||
|
||||
class FooSuper {
|
||||
public static final void f(int i) {}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Add static import for 'java.lang.Math.abs'" "true"
|
||||
package test;
|
||||
|
||||
import static java.lang.Math.abs;
|
||||
|
||||
public class X {{
|
||||
Math.abs<caret>(1.0);
|
||||
}}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Add static import for 'test.Bar.f'" "true"
|
||||
// "Add static import for 'test.Bar.f'" "false"
|
||||
package test;
|
||||
|
||||
class Bar {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Add static import for 'test.Bar.f'" "true"
|
||||
// "Add static import for 'test.Bar.f'" "false"
|
||||
package test;
|
||||
|
||||
class Bar {
|
||||
|
||||
Reference in New Issue
Block a user