IDEA-138720 (false positive "Symbol only used from test code" for interface methods (after global inspection))

This commit is contained in:
Bas Leijdekkers
2015-04-07 14:28:35 +02:00
parent 22c00c0688
commit 15f05d6419
5 changed files with 115 additions and 7 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Bas Leijdekkers
* Copyright 2011-2015 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@ public class ElementOnlyUsedFromTestCodeInspection
@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager,
@NotNull GlobalInspectionContext globalContext,
@NotNull ProblemDescriptionsProcessor processor) {
if (!isOnlyUsedFromTestCode(refEntity)) {
if (!isOnlyUsedFromTestCode(refEntity, false)) {
return null;
}
if (!(refEntity instanceof RefJavaElement)) {
@@ -63,6 +63,19 @@ public class ElementOnlyUsedFromTestCodeInspection
if (!javaElement.isReferenced()) {
return null;
}
if (refEntity instanceof RefMethod) {
final RefMethod refMethod = (RefMethod)refEntity;
for (RefMethod superMethod : refMethod.getSuperMethods()) {
if (!isOnlyUsedFromTestCode(superMethod, true)) {
return null;
}
}
for (RefMethod derivedMethod : refMethod.getDerivedMethods()) {
if (!isOnlyUsedFromTestCode(derivedMethod, true)) {
return null;
}
}
}
final PsiElement element = javaElement.getElement();
if (element instanceof PsiClass) {
final PsiClass aClass = (PsiClass)element;
@@ -119,7 +132,7 @@ public class ElementOnlyUsedFromTestCodeInspection
@Nullable
public static PsiClass getTopLevelParentClass(PsiElement e) {
PsiClass result = null;
PsiElement parent = e.getParent();
PsiElement parent = e;
while (parent != null && !(parent instanceof PsiFile)) {
if (parent instanceof PsiClass) {
result = (PsiClass)parent;
@@ -129,10 +142,12 @@ public class ElementOnlyUsedFromTestCodeInspection
return result;
}
private static boolean isOnlyUsedFromTestCode(RefEntity refElement) {
final Boolean usedFromTestCode =
refElement.getUserData(ONLY_USED_FROM_TEST_CODE);
return usedFromTestCode != null && usedFromTestCode.booleanValue();
private static boolean isOnlyUsedFromTestCode(RefEntity refElement, boolean orNotUsed) {
final Boolean usedFromTestCode = refElement.getUserData(ONLY_USED_FROM_TEST_CODE);
if (usedFromTestCode != null) {
return usedFromTestCode.booleanValue();
}
return orNotUsed;
}
private static class ElementOnlyUsedFromTestCodeAnnotator
@@ -0,0 +1,37 @@
package element_only_used_from_test_code;
interface Base {
void method();
void method2();
}
class Aardvark {
public
Aardvark(Base b) {
b.method();
}
}
class Bee implements Base {
@Override
public void method() {
}
@Override
public void method2() {
}
}
public class ElementOnlyUsedFromTestCode {
Bee b = new Bee();
@org.junit.Test
public void testSomething() {
b.method();
}
@org.junit.Test
public void testSomethingElse() {
b.method2();
}
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>ElementOnlyUsedFromTestCode.java</file>
<line>15</line>
<description>class &lt;code&gt;Bee&lt;/code&gt; only used from test code #loc</description>
</problem>
<problem>
<file>ElementOnlyUsedFromTestCode.java</file>
<line>21</line>
<description>method &lt;code&gt;method2()&lt;/code&gt; only called from test code #loc</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
package com.siyeh.igtest.redundancy.element_only_used_from_test_code.junit.org.junit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Test {}
@@ -0,0 +1,33 @@
/*
* 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.siyeh.ig.redundancy;
import com.intellij.testFramework.PsiTestUtil;
import com.siyeh.ig.IGInspectionTestCase;
/**
* @author Bas Leijdekkers
*/
public class ElementOnlyUsedFromTestCodeInspectionTest extends IGInspectionTestCase {
public void test() {
PsiTestUtil.addLibrary(myModule, getTestDataPath() + "/com/siyeh/igtest/redundancy/element_only_used_from_test_code/junit/");
doTest("com/siyeh/igtest/redundancy/element_only_used_from_test_code", new ElementOnlyUsedFromTestCodeInspection());
}
}