mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
add single static import on inaccessible element - retrieve accessible or do not suggest intention (IDEA-18050 )
This commit is contained in:
+23
-5
@@ -62,7 +62,7 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
if (parameterList != null && parameterList.getFirstChild() != null) return null;
|
||||
PsiElement resolved = refExpr.resolve();
|
||||
if (resolved instanceof PsiMember && ((PsiModifierListOwner)resolved).hasModifierProperty(PsiModifier.STATIC)) {
|
||||
PsiClass aClass = ((PsiMember)resolved).getContainingClass();
|
||||
PsiClass aClass = getResolvedClass(element, (PsiMember)resolved);
|
||||
if (aClass != null && !PsiTreeUtil.isAncestor(aClass, element, true)) {
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null && !Comparing.strEqual(qName, aClass.getName())) {
|
||||
@@ -89,7 +89,22 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
private static PsiClass getResolvedClass(PsiElement element, PsiMember resolved) {
|
||||
PsiClass aClass = resolved.getContainingClass();
|
||||
if (!PsiUtil.isAccessible(aClass, element, null)) {
|
||||
final PsiElement qualifier = ((PsiJavaCodeReferenceElement)element.getParent()).getQualifier();
|
||||
if (qualifier instanceof PsiReferenceExpression) {
|
||||
final PsiElement qResolved = ((PsiReferenceExpression)qualifier).resolve();
|
||||
if (qResolved instanceof PsiVariable) {
|
||||
aClass = PsiUtil.resolveClassInClassTypeOnly(((PsiVariable)qResolved).getType());
|
||||
}
|
||||
}
|
||||
}
|
||||
return aClass;
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
String classQName = getStaticImportClass(element);
|
||||
if (classQName != null) {
|
||||
@@ -98,7 +113,7 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
return classQName != null;
|
||||
}
|
||||
|
||||
public static void invoke(PsiFile file, PsiElement element) {
|
||||
public static void invoke(PsiFile file, final PsiElement element) {
|
||||
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
|
||||
|
||||
final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement)element.getParent();
|
||||
@@ -120,7 +135,7 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
|
||||
if (resolved != null) {
|
||||
RefactoringUtil.bindToElementViaStaticImport(
|
||||
((PsiMember)resolved).getContainingClass(), ((PsiNamedElement)resolved).getName(), ((PsiJavaFile)file).getImportList()
|
||||
getResolvedClass(element, (PsiMember)resolved), ((PsiNamedElement)resolved).getName(), ((PsiJavaFile)file).getImportList()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -151,7 +166,10 @@ public class AddSingleMemberStaticImportAction extends PsiElementBaseIntentionAc
|
||||
} else {
|
||||
if (qualifierExpression instanceof PsiJavaCodeReferenceElement) {
|
||||
PsiElement aClass = ((PsiJavaCodeReferenceElement)qualifierExpression).resolve();
|
||||
if (aClass instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass)aClass, ((PsiMember)resolved).getContainingClass(), true)) {
|
||||
if (aClass instanceof PsiVariable) {
|
||||
aClass = PsiUtil.resolveClassInClassTypeOnly(((PsiVariable)aClass).getType());
|
||||
}
|
||||
if (aClass instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass)aClass, getResolvedClass(element, (PsiMember)resolved), true)) {
|
||||
boolean foundMemberByName = false;
|
||||
if (referent instanceof PsiMember) {
|
||||
final String memberName = ((PsiMember)referent).getName();
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package impl;
|
||||
import foo.Foo;
|
||||
public class FooImpl extends Foo {}
|
||||
class Bar {
|
||||
void doSmth(FooImpl im) {
|
||||
im.f<caret>oo();
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package impl;
|
||||
import foo.Foo;
|
||||
|
||||
import static impl.FooImpl.foo;
|
||||
|
||||
public class FooImpl extends Foo {}
|
||||
class Bar {
|
||||
void doSmth(FooImpl im) {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2000-2011 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.intention;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
|
||||
|
||||
public class AddSingleStaticImportActionTest extends JavaCodeInsightFixtureTestCase {
|
||||
|
||||
public void testInaccessible() {
|
||||
myFixture.addClass("package foo; class Foo {public static void foo(){}}");
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
|
||||
final IntentionAction intentionAction = myFixture.findSingleIntention("Add static import for 'impl.FooImpl.foo'");
|
||||
assertNotNull(intentionAction);
|
||||
myFixture.launchAction(intentionAction);
|
||||
myFixture.checkResultByFile(getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/quickFix/addSingleStaticImport";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user