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.
99 lines
2.5 KiB
99 lines
2.5 KiB
4 years ago
|
/*
|
||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*/
|
||
|
package codetoanalyze.java.infer;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
|
||
4 years ago
|
public class PreconditionsExample {
|
||
4 years ago
|
|
||
|
Integer i1;
|
||
|
|
||
|
/*
|
||
|
* Note: Although the Guava checkNotNull method throws a NullPointerException when a given
|
||
|
* value x is null, Pulse will simply try to go ahead assuming that x != null.
|
||
|
* This means that it will not report NPEs on values passed to checkNotNull.
|
||
4 years ago
|
* The analogous approach has been taken for checkState and checkArgument.
|
||
4 years ago
|
*/
|
||
|
|
||
|
void testCheckNotNullOnNullValueOk() {
|
||
4 years ago
|
PreconditionsExample x = null;
|
||
4 years ago
|
// This should not be reported due to the use of checkNotNull
|
||
|
Preconditions.checkNotNull(x);
|
||
|
Integer i1 = x.i1;
|
||
|
}
|
||
|
|
||
|
void testCheckNotNullOnNonNullValueOk() {
|
||
4 years ago
|
PreconditionsExample x = new PreconditionsExample();
|
||
4 years ago
|
x.i1 = new Integer(10);
|
||
4 years ago
|
PreconditionsExample y = Preconditions.checkNotNull(x);
|
||
4 years ago
|
// This should not cause bug, as y.i1 should be equal to x.i1
|
||
|
if (!y.i1.equals(10)) {
|
||
|
Object o = null;
|
||
|
o.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void testCheckNotNullPassingFurtherArgsOk() {
|
||
|
Object x = null;
|
||
|
Preconditions.checkNotNull(x, "errorMsg");
|
||
|
x.toString();
|
||
|
}
|
||
|
|
||
|
void testCheckNotNullOtherDerefOk() {
|
||
|
Object x = null;
|
||
|
Object y = null;
|
||
|
Preconditions.checkNotNull(x, "errorMsg");
|
||
|
y.toString();
|
||
|
}
|
||
|
|
||
|
void testCheckNotNullArgOk(Object x) {
|
||
|
Object y = null;
|
||
|
Preconditions.checkNotNull(x);
|
||
|
x.toString();
|
||
|
}
|
||
|
|
||
|
void testCheckNotNullArgBad(Object x) {
|
||
|
Object y = null;
|
||
|
Preconditions.checkNotNull(x);
|
||
|
y.toString();
|
||
|
}
|
||
4 years ago
|
|
||
|
void checkStateConditionSatBad() {
|
||
|
PreconditionsExample x = new PreconditionsExample();
|
||
|
PreconditionsExample y = null;
|
||
|
x.i1 = 1000;
|
||
|
Preconditions.checkState(x.i1.equals(1000));
|
||
|
y.toString();
|
||
|
}
|
||
|
|
||
|
void checkStateConditionUnsatOk() {
|
||
|
PreconditionsExample x = new PreconditionsExample();
|
||
|
PreconditionsExample y = null;
|
||
|
x.i1 = 1000;
|
||
|
Preconditions.checkState(x.i1.equals(100), "errorMsg");
|
||
|
y.toString();
|
||
|
}
|
||
|
|
||
|
void checkArgumentUnsatOk() {
|
||
|
int age = -18;
|
||
|
Object x = null;
|
||
|
Preconditions.checkArgument(age > 0);
|
||
|
x.getClass();
|
||
|
}
|
||
|
|
||
|
void checkAgeOver18(int age) {
|
||
|
Preconditions.checkArgument(age > 18, "errorMsg");
|
||
|
}
|
||
|
|
||
|
void checkArgumentSatBad() {
|
||
|
int age = 20;
|
||
|
Object x = null;
|
||
|
checkAgeOver18(age);
|
||
|
x.getClass();
|
||
|
}
|
||
4 years ago
|
}
|