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.
158 lines
3.2 KiB
158 lines
3.2 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;
|
||
|
|
||
9 years ago
|
import android.support.v4.app.FragmentActivity;
|
||
|
import android.view.View;
|
||
6 years ago
|
import android.widget.ImageView;
|
||
9 years ago
|
import com.facebook.infer.annotation.Expensive;
|
||
|
import com.facebook.infer.annotation.PerformanceCritical;
|
||
8 years ago
|
import javax.annotation.Nullable;
|
||
9 years ago
|
|
||
9 years ago
|
interface AnnotatedInterface {
|
||
|
|
||
|
@PerformanceCritical
|
||
|
void annotatedPerformanceCriticalInInterface();
|
||
|
}
|
||
|
|
||
9 years ago
|
class Other {
|
||
|
|
||
|
@Expensive
|
||
6 years ago
|
void expensive() {}
|
||
9 years ago
|
|
||
|
void callsExpensive1() {
|
||
|
expensive();
|
||
|
}
|
||
|
|
||
6 years ago
|
void inexpensiveMethod() {}
|
||
9 years ago
|
}
|
||
|
|
||
|
@Expensive
|
||
|
class ExpensiveClass {
|
||
|
|
||
6 years ago
|
void anExpensiveMethod() {}
|
||
9 years ago
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
|
class PerformanceCriticalClass {
|
||
|
|
||
|
void performanceCriticalMethod1(ExpensiveClass c) {
|
||
|
c.anExpensiveMethod(); // should report
|
||
|
}
|
||
|
|
||
|
void performanceCriticalMethod2(Other o) {
|
||
|
o.expensive(); // should report
|
||
|
}
|
||
|
|
||
|
void performanceCriticalMethod3(Other o) {
|
||
|
o.callsExpensive1(); // should report
|
||
|
}
|
||
|
|
||
|
void performanceCriticalMethod4(Other o) {
|
||
|
o.inexpensiveMethod(); // should not report
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class ExpensiveSubclass extends ExpensiveClass {
|
||
|
|
||
6 years ago
|
void anotherExpensiveMethod() {}
|
||
9 years ago
|
}
|
||
|
|
||
|
class PerformanceCriticalSubclass extends PerformanceCriticalClass {
|
||
|
|
||
|
void subclassPerformanceCriticalMethod1(ExpensiveClass c) {
|
||
|
c.anExpensiveMethod(); // should report
|
||
|
}
|
||
|
|
||
|
void subclassPerformanceCriticalMethod2(ExpensiveSubclass c) {
|
||
|
c.anotherExpensiveMethod(); // should report
|
||
|
}
|
||
|
|
||
|
void subclassPerformanceCriticalMethod3(Other o) {
|
||
|
o.callsExpensive1(); // should report;
|
||
|
}
|
||
|
|
||
|
void subclassPerformanceCriticalMethod4(Other o) {
|
||
|
o.inexpensiveMethod(); // should not report;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
public class ExpensiveCallExample implements AnnotatedInterface {
|
||
9 years ago
|
|
||
8 years ago
|
@Nullable Other mOther;
|
||
9 years ago
|
|
||
9 years ago
|
void nonExpensiveMethod() {}
|
||
|
|
||
9 years ago
|
@Expensive
|
||
9 years ago
|
void expensiveMethod() {
|
||
|
// The checker should still report the expensive call stack despite the call cycle
|
||
|
methodWrapper();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void methodWrapper() {
|
||
|
expensiveMethod();
|
||
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
|
void notCallingExpensiveMethod() {
|
||
|
nonExpensiveMethod();
|
||
|
}
|
||
|
|
||
9 years ago
|
@PerformanceCritical
|
||
9 years ago
|
void directlyCallingExpensiveMethod() {
|
||
9 years ago
|
expensiveMethod();
|
||
|
}
|
||
|
|
||
9 years ago
|
@PerformanceCritical
|
||
|
void indirectlyCallingExpensiveMethod() {
|
||
|
methodWrapper();
|
||
|
}
|
||
|
|
||
9 years ago
|
@PerformanceCritical
|
||
|
void callingExpensiveMethodFromInterface(ExpensiveInterfaceExample object) {
|
||
|
object.m5();
|
||
|
}
|
||
|
|
||
9 years ago
|
void callsExpensive2() {
|
||
|
mOther.callsExpensive1();
|
||
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
|
void longerCallStackToExpensive() {
|
||
|
callsExpensive2();
|
||
|
}
|
||
|
|
||
9 years ago
|
@PerformanceCritical
|
||
|
View callsFindViewByIdFromView(ImageView view, int id) {
|
||
|
return view.findViewById(id);
|
||
|
}
|
||
|
|
||
|
@PerformanceCritical
|
||
|
View callsFindViewByIdFromActivity(FragmentActivity activity, int id) {
|
||
|
return activity.findViewById(id);
|
||
|
}
|
||
|
|
||
9 years ago
|
@PerformanceCritical
|
||
|
void callMethodOnExpensiveClass(ExpensiveClass c) {
|
||
|
c.anExpensiveMethod();
|
||
|
}
|
||
|
|
||
9 years ago
|
public void annotatedPerformanceCriticalInInterface() {
|
||
|
mOther.callsExpensive1();
|
||
|
}
|
||
|
|
||
9 years ago
|
native boolean test();
|
||
|
|
||
|
@PerformanceCritical
|
||
|
void callsExpensiveInConditionalBranch() {
|
||
|
if (test()) {
|
||
|
expensiveMethod();
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|