fix UnusedDeclarationJvmTest

This commit is contained in:
Dmitry Batkovich
2018-10-08 13:13:24 +03:00
parent f847a1b240
commit 913efb1fef
3 changed files with 37 additions and 3 deletions

View File

@@ -284,11 +284,18 @@ public class RefClassImpl extends RefJavaElementImpl implements RefClass {
public void buildReferences() {
UClass uClass = getUastElement();
if (uClass != null) {
if (uClass instanceof UAnonymousClass) {
UObjectLiteralExpression objectAccess = UastUtils.getParentOfType(uClass, UObjectLiteralExpression.class);
if (objectAccess != null && objectAccess.getDeclaration().getSourcePsi() == uClass.getSourcePsi()) {
RefJavaUtil.getInstance().addReferencesTo(uClass, this, objectAccess.getValueArguments().toArray(UElementKt.EMPTY_ARRAY));
}
}
for (UClassInitializer classInitializer : uClass.getInitializers()) {
RefJavaUtil.getInstance().addReferencesTo(uClass, this, classInitializer.getUastBody());
}
RefJavaUtil.getInstance().addReferencesTo(uClass, this, ((UAnnotated)uClass).getAnnotations().toArray(new UElement[0]));
RefJavaUtil.getInstance().addReferencesTo(uClass, this, ((UAnnotated)uClass).getAnnotations().toArray(UElementKt.EMPTY_ARRAY));
for (PsiTypeParameter parameter : uClass.getJavaPsi().getTypeParameters()) {
UElement uTypeParameter = UastContextKt.toUElement(parameter);
@@ -311,7 +318,7 @@ public class RefClassImpl extends RefJavaElementImpl implements RefClass {
getRefManager().getReference(uMethod.getSourcePsi());
}
RefJavaUtil.getInstance().addReferencesTo(uClass, this, uClass.getUastSuperTypes().toArray(new UElement[0]));
RefJavaUtil.getInstance().addReferencesTo(uClass, this, uClass.getUastSuperTypes().toArray(UElementKt.EMPTY_ARRAY));
getRefManager().fireBuildReferences(this);
}

View File

@@ -5,6 +5,7 @@ package com.intellij.codeInspection.reference;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.MethodSignatureUtil;
import com.intellij.psi.util.PsiUtil;
@@ -214,6 +215,9 @@ public class RefJavaUtilImpl extends RefJavaUtil {
if (psiResolved == null) {
psiResolved = tryFindKotlinParameter(node, decl);
}
if (psiResolved instanceof LightElement) {
psiResolved = psiResolved.getNavigationElement();
}
RefElement refResolved = refFrom.getRefManager().getReference(psiResolved);
boolean writing = isAccessedForWriting(node);
boolean reading = isAccessedForReading(node);

View File

@@ -1,10 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>B.kt</file>
<line>1</line>
<problem_class>unused declaration</problem_class>
<description>Method has one usage but it is not reachable from entry points.</description>
</problem>
<problem>
<file>C.kt</file>
<line>6</line>
<problem_class>unused declaration</problem_class>
<description>Method has usage(s) but they all belong to calls chain that has no members reachable from entry points.</description>
</problem>
<problem>
<file>C.kt</file>
<line>2</line>
<entry_point TYPE="method" FQNAME="C void xxx()" />
<problem_class>unused declaration</problem_class>
<description>Method has usage(s) but they all belong to calls chain that has no members reachable from entry points.</description>
</problem>
<problem>
<file>A.java</file>
<line>2</line>
<entry_point TYPE="method" FQNAME="A void xxx()" />
<problem_class>unused declaration</problem_class>
<description>Method is never used.</description>
<description>Method has one usage but it is not reachable from entry points.</description>
</problem>
</problems>