|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
[nullsafe] Prepare to introduce gradual mode: split tests
Summary:
In next diff, we are going to introduce a new mode of nullsafe
(gradual). For testing, we are going to employ the strategy used by jvillard
for Pulse.
In this diff we split tests into two subfolders, one for the default and one for the gradual
mode.
We are planning to make the gradual mode default eventually. For that, most
new features will make sense for gradual mode, and we will mostly evolve
tests for that mode.
As for 'default' mode, we need to preserve tests mostly to ensure we don't introduce
regressions.
Occasionally, we might make changes that make sense for both modes, in
this (expected relatively rare) cases we will make changes to both set
of tests.
An alternative strategy would be to have two sets of issues.exp files,
one for gradual and one for default mode. This has an advantage of each
java file to be always tested twice, but disadvantage is that it will be
harder to write meaningful test code so that it makes sense for both
modes simultaneously.
Reviewed By: ngorogiannis
Differential Revision: D17156724
fbshipit-source-id: a92a9208f
5 years ago
|
|
|
package codetoanalyze.java.nullsafe_default;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
public class NullFieldAccess {
|
|
|
|
|
|
|
|
interface I {
|
|
|
|
@Nullable Object nullable = new Object();
|
|
|
|
Object notNull = new Object();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable Object nullable;
|
|
|
|
Object notNull;
|
|
|
|
|
|
|
|
static final @Nullable Object nullableStatic = new Object();
|
|
|
|
static final Object notNullStatic = new Object();
|
|
|
|
|
|
|
|
@Nullable Object[] nullableArray;
|
|
|
|
Object[] notNullArray;
|
|
|
|
|
|
|
|
NullFieldAccess() {
|
|
|
|
nullable = new Object();
|
|
|
|
notNull = new Object();
|
|
|
|
nullableArray = new Object[1];
|
|
|
|
notNullArray = new Object[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void testNonStaticFields() {
|
|
|
|
Object bad = nullable;
|
|
|
|
bad.toString(); // BAD: `bad` can be null
|
|
|
|
|
|
|
|
Object good = notNull;
|
|
|
|
good.toString(); // OK: `good` is not null
|
|
|
|
}
|
|
|
|
|
|
|
|
void testStatic() {
|
|
|
|
Object bad = nullableStatic;
|
|
|
|
bad.toString(); // BAD: `bad` can be null
|
|
|
|
|
|
|
|
Object good = notNullStatic;
|
|
|
|
good.toString(); // OK: `good` is not null
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInterface() {
|
|
|
|
Object bad = I.nullable;
|
|
|
|
bad.toString(); // BAD: `bad` can be null
|
|
|
|
|
|
|
|
Object good = I.notNull;
|
|
|
|
good.toString(); // OK: `good` is not null
|
|
|
|
}
|
|
|
|
|
|
|
|
void testArray() {
|
|
|
|
int i1 = nullableArray.length; // BAD: array can be null
|
|
|
|
Object o1 = nullableArray[0]; // BAD: array can be null
|
|
|
|
|
|
|
|
int i2 = notNullArray.length; // OK: arrays is not null
|
|
|
|
Object o2 = notNullArray[0]; // OK: array is not null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|