fixed usage type for static fields and methods (IDEA-143229)

This commit is contained in:
nik
2015-07-29 12:13:08 +03:00
parent 33e3f1727d
commit ef1f9f2d46
6 changed files with 80 additions and 1 deletions
@@ -256,7 +256,7 @@ public class JavaUsageTypeProvider implements UsageTypeProviderEx {
private static boolean isNestedClassOf(PsiJavaCodeReferenceElement classReference, @NotNull UsageTarget[] targets) {
final PsiElement qualifier = classReference.getQualifier();
if (qualifier instanceof PsiJavaCodeReferenceElement) {
return qualifiesToTargetClasses((PsiJavaCodeReferenceElement)qualifier, targets);
return qualifiesToTargetClasses((PsiJavaCodeReferenceElement)qualifier, targets) && classReference.resolve() instanceof PsiClass;
}
return false;
}
@@ -0,0 +1,7 @@
public class Foo {
public static int STATIC_FIELD = 0;
public static void staticMethod() {}
public static class Inner {
}
}
@@ -0,0 +1,5 @@
public class NestedClassAccess {
void m() {
<caret>Foo.Inner inner = null;
}
}
@@ -0,0 +1,5 @@
public class StaticMethodCall {
void m() {
int i = <caret>Foo.STATIC_FIELD;
}
}
@@ -0,0 +1,5 @@
public class StaticMethodCall {
void m() {
<caret>Foo.staticMethod();
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2015 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.psi.usages;
import com.intellij.JavaTestUtil;
import com.intellij.find.findUsages.PsiElement2UsageTargetAdapter;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.usages.UsageTarget;
import com.intellij.usages.impl.rules.JavaUsageTypeProvider;
import com.intellij.usages.impl.rules.UsageType;
/**
* @author nik
*/
public class JavaUsageTypeProviderTest extends LightCodeInsightFixtureTestCase {
public void testNestedClassAccess() throws Exception {
myFixture.configureByFiles("NestedClassAccess.java", "Foo.java");
assertUsageType(UsageType.CLASS_NESTED_CLASS_ACCESS, myFixture.findClass("Foo"));
}
public void testStaticMethodCall() throws Exception {
myFixture.configureByFiles("StaticMethodCall.java", "Foo.java");
assertUsageType(UsageType.CLASS_STATIC_MEMBER_ACCESS, myFixture.findClass("Foo"));
}
public void testStaticFieldUsage() throws Exception {
myFixture.configureByFiles("StaticFieldUsage.java", "Foo.java");
assertUsageType(UsageType.CLASS_STATIC_MEMBER_ACCESS, myFixture.findClass("Foo"));
}
private void assertUsageType(UsageType expected, PsiClass target) {
UsageTarget[] targets = {new PsiElement2UsageTargetAdapter(target)};
PsiElement element = myFixture.getReferenceAtCaretPositionWithAssertion().getElement();
UsageType usageType = new JavaUsageTypeProvider().getUsageType(element, targets);
assertEquals(expected, usageType);
}
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/psi/usages/";
}
}