You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.8 KiB
72 lines
1.8 KiB
9 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
9 years ago
|
|
||
|
package codetoanalyze.java.checkers;
|
||
|
|
||
8 years ago
|
import android.annotation.SuppressLint;
|
||
9 years ago
|
import com.facebook.infer.annotation.Expensive;
|
||
|
import com.facebook.infer.annotation.PerformanceCritical;
|
||
|
|
||
|
interface I {
|
||
|
void foo();
|
||
|
}
|
||
|
|
||
|
class A implements I {
|
||
|
|
||
8 years ago
|
@SuppressLint("CHECKERS_EXPENSIVE_OVERRIDES_UNANNOTATED")
|
||
9 years ago
|
// Suppressing the sub-typing violation warning here as foo() is not annotated as @Expensive
|
||
|
// in the interface. This report is legit but is not relevant for the current test.
|
||
9 years ago
|
@Expensive
|
||
|
public void foo() {}
|
||
|
}
|
||
|
|
||
|
class B extends A implements I {
|
||
|
public void foo() {}
|
||
|
}
|
||
|
|
||
|
public class ExpensiveInheritanceExample {
|
||
|
|
||
9 years ago
|
// The objective of this test is to document the limitations of the checker, which just
|
||
|
// implements a type system. This means that the checker is flow insensitive and is only based
|
||
|
// on the static type information. Especially, it does not try to resolve dynamic dispatch.
|
||
|
// However, the checker is still exhaustive thanks to the sub-typing rule for
|
||
|
// the @Expensive annotation.
|
||
9 years ago
|
@PerformanceCritical
|
||
|
void shouldNotReportBecauseInterfaceNotAnnotated(I i) {
|
||
|
i.foo();
|
||
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
9 years ago
|
void reportsBecauseFooIsExpensiveInA(A a) {
|
||
9 years ago
|
a.foo();
|
||
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
9 years ago
|
void doesNotreportBecauseFooIsNotExpensiveInB(B b) {
|
||
|
b.foo();
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
native B createB();
|
||
|
|
||
9 years ago
|
A actuallyReturnsObjectOfTypeB() {
|
||
9 years ago
|
return createB();
|
||
9 years ago
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
|
void reportsAssumingObjectOfTypeA() {
|
||
|
A a = actuallyReturnsObjectOfTypeB();
|
||
|
a.foo();
|
||
|
}
|
||
|
|
||
9 years ago
|
@PerformanceCritical
|
||
9 years ago
|
void doesReportBecauseTypeFlowInsensitive(A a) {
|
||
9 years ago
|
if (a instanceof B) {
|
||
|
a.foo();
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|