diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspection.java index 7cb49d1bae51..6bd530ccd7b9 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspection.java @@ -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 diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/ElementOnlyUsedFromTestCode.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/ElementOnlyUsedFromTestCode.java new file mode 100644 index 000000000000..ba22993b0fd2 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/ElementOnlyUsedFromTestCode.java @@ -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(); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/expected.xml new file mode 100644 index 000000000000..3934177eb63e --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/expected.xml @@ -0,0 +1,14 @@ + + + + ElementOnlyUsedFromTestCode.java + 15 + class <code>Bee</code> only used from test code #loc + + + + ElementOnlyUsedFromTestCode.java + 21 + method <code>method2()</code> only called from test code #loc + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/junit/org/junit/Test.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/junit/org/junit/Test.java new file mode 100644 index 000000000000..ab62bdca1157 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/redundancy/element_only_used_from_test_code/junit/org/junit/Test.java @@ -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 {} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspectionTest.java new file mode 100644 index 000000000000..7dea8a820df4 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/redundancy/ElementOnlyUsedFromTestCodeInspectionTest.java @@ -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()); + } + +}