IDEA-155323 Bogus "Test-only method is called" inspection
authorpeter <peter@jetbrains.com>
Fri, 29 Apr 2016 18:27:39 +0000 (20:27 +0200)
committerpeter <peter@jetbrains.com>
Fri, 29 Apr 2016 20:56:20 +0000 (22:56 +0200)
java/java-analysis-impl/src/com/intellij/codeInspection/testOnly/TestOnlyInspection.java
java/java-tests/testData/inspection/testOnly/visibleForTesting/expected.xml
java/java-tests/testData/inspection/testOnly/visibleForTesting/src/A.java
java/java-tests/testData/inspection/testOnly/visibleForTesting/src/B.java

index d6c4c80abfd4bfca6b38550cd7bedb36d392a2d3..a4a7cc4b8c4e0c09c11694a9517e99da4b5c0e98 100644 (file)
@@ -100,6 +100,7 @@ public class TestOnlyInspection extends BaseJavaBatchLocalInspectionTool {
       if (JavaResolveUtil.isAccessible(member, member.getContainingClass(), modList, reference, null, null)) {
         return;
       }
+      int a = 1;
     }
 
     reportProblem(reference, member, h);
@@ -121,7 +122,13 @@ public class TestOnlyInspection extends BaseJavaBatchLocalInspectionTool {
   @Nullable
   private static PsiAnnotation findVisibleForTestingAnnotation(@NotNull PsiMember member) {
     PsiAnnotation anno = AnnotationUtil.findAnnotation(member, "com.google.common.annotations.VisibleForTesting");
-    return anno != null ? anno : AnnotationUtil.findAnnotation(member, "com.android.annotations.VisibleForTesting");
+    if (anno == null) {
+      anno = AnnotationUtil.findAnnotation(member, "com.android.annotations.VisibleForTesting");
+    }
+    if (anno != null) return anno;
+
+    PsiClass containingClass = member.getContainingClass();
+    return containingClass != null ? findVisibleForTestingAnnotation(containingClass) : null;
   }
 
   private static boolean isInsideTestOnlyMethod(PsiElement e) {
index bf49e4aca6a23a6207b01d147710fce3041d77fd..bb5e5062608008f444bab86452386919f3b79651 100644 (file)
     <line>5</line>
     <description>Test-only method is called in production code</description>
   </problem>
+  <problem>
+    <file>B.java</file>
+    <line>6</line>
+    <description>Test-only class is referenced in production code</description>
+  </problem>
+  <problem>
+    <file>B.java</file>
+    <line>7</line>
+    <description>Test-only method is called in production code</description>
+  </problem>
+
 </problems>
index f65ba879eaf106121741784cbe81d947888b5f5a..4f56a174a80d908093b6e90b629083016c307696 100644 (file)
@@ -18,4 +18,16 @@ public class A {
   void relaxedToPackageLevel(int a) {
 
   }
+
+  @com.google.common.annotations.VisibleForTesting
+  static class FooException extends RuntimeException {
+    FooException(String message) {
+      super(message);
+    }
+  }
+
+  public static void usingExceptionPrivately(String[] args) {
+    A.FooException exception =
+      new A.FooException("");
+  }
 }
\ No newline at end of file
index 429a154ec49fc973250e176ffe7bb3e96cff5f6c..ec8595d6f41e93314f08341d114048386aae7222 100644 (file)
@@ -3,5 +3,7 @@ public class B {
     new A().invisibleMethod(2);
     new A().visibleMethod(2);
     new A().relaxedToPackageLevel(2);
+    A.FooException exception =
+      new A.FooException("");
   }
 }
\ No newline at end of file