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.
68 lines
1.6 KiB
68 lines
1.6 KiB
10 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
|
*/
|
||
10 years ago
|
|
||
5 years ago
|
package codetoanalyze.java.nullsafe_default;
|
||
10 years ago
|
|
||
|
import javax.annotation.Nullable;
|
||
|
|
||
|
public class NullFieldAccess {
|
||
|
|
||
|
interface I {
|
||
5 years ago
|
@Nullable Object nullable = new Object();
|
||
|
Object notNull = new Object();
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
@Nullable Object nullable;
|
||
|
Object notNull;
|
||
|
|
||
|
static final @Nullable Object nullableStatic = new Object();
|
||
|
static final Object notNullStatic = new Object();
|
||
|
|
||
|
@Nullable Object[] nullableArray;
|
||
|
Object[] notNullArray;
|
||
10 years ago
|
|
||
|
NullFieldAccess() {
|
||
5 years ago
|
nullable = new Object();
|
||
|
notNull = new Object();
|
||
|
nullableArray = new Object[1];
|
||
|
notNullArray = new Object[1];
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
void testNonStaticFields() {
|
||
|
Object bad = nullable;
|
||
|
bad.toString(); // BAD: `bad` can be null
|
||
10 years ago
|
|
||
5 years ago
|
Object good = notNull;
|
||
|
good.toString(); // OK: `good` is not null
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
void testStatic() {
|
||
|
Object bad = nullableStatic;
|
||
|
bad.toString(); // BAD: `bad` can be null
|
||
10 years ago
|
|
||
5 years ago
|
Object good = notNullStatic;
|
||
|
good.toString(); // OK: `good` is not null
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
void testInterface() {
|
||
|
Object bad = I.nullable;
|
||
|
bad.toString(); // BAD: `bad` can be null
|
||
10 years ago
|
|
||
5 years ago
|
Object good = I.notNull;
|
||
|
good.toString(); // OK: `good` is not null
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
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
|
||
10 years ago
|
}
|
||
5 years ago
|
|
||
10 years ago
|
}
|