From 2e01d3402f8eb7f3d3cf6510e7e03ea61e9a9a6f Mon Sep 17 00:00:00 2001 From: jrm Date: Wed, 18 Nov 2015 11:37:56 -0800 Subject: [PATCH] adding some tests to outline the behaviour of the @Expensive checker with inheritance Summary: public Some more examples to explain the behaviour of the type checker with inheritance Reviewed By: cristianoc Differential Revision: D2639908 fb-gh-sync-id: d038061 --- .../checkers/ExpensiveInheritanceExample.java | 60 +++++++++++++++++++ .../checkers/ExpensiveInheritanceTest.java | 55 +++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 infer/tests/codetoanalyze/java/checkers/ExpensiveInheritanceExample.java create mode 100644 infer/tests/endtoend/java/checkers/ExpensiveInheritanceTest.java diff --git a/infer/tests/codetoanalyze/java/checkers/ExpensiveInheritanceExample.java b/infer/tests/codetoanalyze/java/checkers/ExpensiveInheritanceExample.java new file mode 100644 index 000000000..84e7d38e7 --- /dev/null +++ b/infer/tests/codetoanalyze/java/checkers/ExpensiveInheritanceExample.java @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2013 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package codetoanalyze.java.checkers; + +import com.facebook.infer.annotation.Expensive; +import com.facebook.infer.annotation.PerformanceCritical; + +interface I { + void foo(); +} + +class A implements I { + + @SuppressWarnings("infer") // In case we really want to implement foo as expensive + @Expensive + public void foo() {} + +} + +class B extends A implements I { + public void foo() {} +} + +public class ExpensiveInheritanceExample { + + @PerformanceCritical + void shouldNotReportBecauseInterfaceNotAnnotated(I i) { + i.foo(); + } + + @PerformanceCritical + void reportsBecauseFooIsExpensiveInA() { + A a = new A(); + a.foo(); + } + + @PerformanceCritical + void doesNotreportBecauseFooIsNotExpensiveInB() { + A a = new B(); + a.foo(); + } + + A actuallyReturnsObjectOfTypeB() { + return new B(); + } + + @PerformanceCritical + void reportsAssumingObjectOfTypeA() { + A a = actuallyReturnsObjectOfTypeB(); + a.foo(); + } + +} diff --git a/infer/tests/endtoend/java/checkers/ExpensiveInheritanceTest.java b/infer/tests/endtoend/java/checkers/ExpensiveInheritanceTest.java new file mode 100644 index 000000000..9c3dd97b3 --- /dev/null +++ b/infer/tests/endtoend/java/checkers/ExpensiveInheritanceTest.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2015 - present Facebook, Inc. +* All rights reserved. +* +* This source code is licensed under the BSD style license found in the +* LICENSE file in the root directory of this source tree. An additional grant +* of patent rights can be found in the PATENTS file in the same directory. +*/ + +package endtoend.java.checkers; + +import static org.hamcrest.MatcherAssert.assertThat; +import static utils.matchers.ResultContainsExactly.containsExactly; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; + +import utils.InferException; +import utils.InferResults; + +public class ExpensiveInheritanceTest { + + public static final String SOURCE_FILE = + "infer/tests/codetoanalyze/java/checkers/ExpensiveInheritanceExample.java"; + + public static final String CALLS_EXPENSIVE_METHOD = + "CHECKERS_CALLS_EXPENSIVE_METHOD"; + + private static InferResults inferResults; + + @BeforeClass + public static void loadResults() throws InterruptedException, IOException { + inferResults = + InferResults.loadCheckersResults(ExpensiveInheritanceTest.class, SOURCE_FILE); + } + + @Test + public void matchErrors() + throws IOException, InterruptedException, InferException { + String[] methods = { + "reportsBecauseFooIsExpensiveInA", + "reportsAssumingObjectOfTypeA", + }; + assertThat( + "Results should contain " + CALLS_EXPENSIVE_METHOD, + inferResults, + containsExactly( + CALLS_EXPENSIVE_METHOD, + SOURCE_FILE, + methods)); + } + +}