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: d038061master
parent
8fb90ccf54
commit
2e01d3402f
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue