From a8a2fcb2e1d33bdf866714275286b4374e4883f8 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Thu, 13 Jul 2017 17:56:50 +0200 Subject: [PATCH] unused declarations: don't search for usages under ReadLock(IDEA-175710) when jvm-like langs are processed, refs should be already in graph, otherwise they will be caught by post processing task --- .../UnusedDeclarationInspectionBase.java | 16 ++++++---------- .../deadCode/UnusedDeclarationPresentation.java | 1 + .../deadCode/referenceFromGroovy/expected.xml | 4 ++++ .../referenceFromGroovy/src/GroovyUsage.groovy | 5 +++++ .../deadCode/referenceFromGroovy/src/Test.java | 3 +++ .../codeInspection/UnusedDeclarationTest.java | 4 ++++ 6 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 java/java-tests/testData/inspection/deadCode/referenceFromGroovy/expected.xml create mode 100644 java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/GroovyUsage.groovy create mode 100644 java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/Test.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationInspectionBase.java index 9bd6120c87de..0b399d0e273a 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationInspectionBase.java @@ -43,7 +43,6 @@ import com.intellij.psi.search.DelegatingGlobalSearchScope; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.PsiNonJavaFileReferenceProcessor; import com.intellij.psi.search.PsiSearchHelper; -import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.util.PsiMethodUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.HashMap; @@ -333,16 +332,13 @@ public class UnusedDeclarationInspectionBase extends GlobalInspectionTool { } }; - if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) { - final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst(); - if (reference != null) { + helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope); + + //references from java-like are already in graph or + //they would be checked during GlobalJavaInspectionContextImpl.performPostRunActivities + for (RefElement element : refElement.getInReferences()) { + if (!(element instanceof RefJavaElement)) { getEntryPointsManager(globalContext).addEntryPoint(refElement, false); - for (PsiMethod method : psiClass.getMethods()) { - final RefElement refMethod = refManager.getReference(method); - if (refMethod != null) { - getEntryPointsManager(globalContext).addEntryPoint(refMethod, false); - } - } } } } diff --git a/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java b/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java index 2dfde213c650..a9b06f117450 100644 --- a/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java +++ b/java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java @@ -125,6 +125,7 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta if (!((RefElementImpl)refElement).hasSuspiciousCallers() || ((RefJavaElementImpl)refElement).isSuspiciousRecursive()) return 1; for (RefElement element : refElement.getInReferences()) { + if (refElement instanceof RefFile) return 1; if (((UnusedDeclarationInspectionBase)myTool).isEntryPoint(element)) return 1; } diff --git a/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/expected.xml b/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/expected.xml new file mode 100644 index 000000000000..79e1a0fd0a57 --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/expected.xml @@ -0,0 +1,4 @@ + + + + diff --git a/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/GroovyUsage.groovy b/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/GroovyUsage.groovy new file mode 100644 index 000000000000..228df3c79108 --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/GroovyUsage.groovy @@ -0,0 +1,5 @@ +class MyGroovyUsage { + static void "to use java"() { + new Util().foo() + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/Test.java b/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/Test.java new file mode 100644 index 000000000000..528a8c6b4f97 --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/referenceFromGroovy/src/Test.java @@ -0,0 +1,3 @@ +class Util { + void foo() { } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java index ad69e4204aa1..51386626eb6e 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java @@ -198,4 +198,8 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { public void testReferenceFromReflection() { doTest(); } + + public void testReferenceFromGroovy() { + doTest(); + } }