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.
163 lines
2.8 KiB
163 lines
2.8 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
|
|
||
6 years ago
|
package codetoanalyze.java.nullsafe_default;
|
||
10 years ago
|
|
||
10 years ago
|
import android.support.annotation.NonNull;
|
||
10 years ago
|
import android.widget.EditText;
|
||
9 years ago
|
import com.facebook.infer.annotation.SuppressViewNullability;
|
||
10 years ago
|
import javax.annotation.Nonnull;
|
||
|
import javax.annotation.Nullable;
|
||
10 years ago
|
import javax.inject.Inject;
|
||
10 years ago
|
|
||
7 years ago
|
// for butterknife
|
||
|
@interface Bind {}
|
||
|
|
||
10 years ago
|
public class FieldNotInitialized {
|
||
|
|
||
10 years ago
|
String a;
|
||
|
|
||
|
@Nullable String b;
|
||
|
|
||
|
@Nonnull String c; // Means: assume it will be initialized to a nonnull value somewhere else.
|
||
|
|
||
|
@Inject String d; // Means: assume it will be initialized via dependency injection
|
||
|
|
||
10 years ago
|
@NonNull String e;
|
||
|
|
||
7 years ago
|
@Bind EditText f; // Means: assume it will be initialized, and ignore null assignment
|
||
9 years ago
|
|
||
|
@SuppressViewNullability EditText g;
|
||
10 years ago
|
|
||
10 years ago
|
// Eradicate should only report one initialization error
|
||
|
FieldNotInitialized() {}
|
||
10 years ago
|
|
||
9 years ago
|
void testNullifyFields() {
|
||
|
f = null; // OK the framework could write null into the field
|
||
|
g = null; // OK the framework could write null into the field
|
||
|
}
|
||
8 years ago
|
|
||
|
class OnlyRead {
|
||
|
Object o;
|
||
|
|
||
|
OnlyRead() {
|
||
|
Object x = o; // not initialized
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class WriteItself {
|
||
|
Object o;
|
||
|
|
||
|
WriteItself() {
|
||
|
o = o; // not initialized
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Swap {
|
||
|
Object o1;
|
||
|
Object o2;
|
||
|
|
||
|
Swap() {
|
||
|
o1 = o2; // not initialized
|
||
|
o2 = new Object();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class SwapOK {
|
||
|
Object o1;
|
||
|
Object o2;
|
||
|
|
||
|
SwapOK() {
|
||
|
o1 = new Object();
|
||
|
o2 = o1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class OnlyReadIndirect {
|
||
|
Object o1;
|
||
|
Object o2;
|
||
|
|
||
|
private void indirect() {
|
||
|
Object x = o1; // not initialized
|
||
|
o2 = new Object();
|
||
|
}
|
||
|
|
||
|
OnlyReadIndirect() {
|
||
|
indirect();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
class ConditionalFieldInit {
|
||
|
Object o1;
|
||
|
@Nullable Object o2 = null;
|
||
6 years ago
|
|
||
8 years ago
|
public ConditionalFieldInit() {
|
||
6 years ago
|
if (o2 != null) {
|
||
|
o1 = new Object(); // Not always initialized
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
class InitIfNull {
|
||
|
Object o;
|
||
|
|
||
|
public InitIfNull() {
|
||
6 years ago
|
if (o == null) o = new Object();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
class InitIfNull2 {
|
||
|
Object o;
|
||
|
|
||
|
public InitIfNull2(Object x) {
|
||
6 years ago
|
if (o == null) o = x;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
class InitIfNull3 {
|
||
|
Object o;
|
||
|
|
||
|
Object getNotNull() {
|
||
|
return new Object();
|
||
|
}
|
||
|
|
||
|
public InitIfNull3() {
|
||
6 years ago
|
if (o == null) o = getNotNull();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
class InitCircular {
|
||
|
String s;
|
||
|
|
||
|
InitCircular() {
|
||
|
String tmp = s;
|
||
|
s = tmp; // s is not initialized: circular initialization
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class InitWithOtherClass {
|
||
|
class OtherClass {
|
||
|
String s = "";
|
||
|
}
|
||
|
|
||
|
String s;
|
||
|
|
||
|
InitWithOtherClass(OtherClass x) {
|
||
|
s = x.s;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class InitWithThisClass {
|
||
|
|
||
|
String s;
|
||
|
|
||
|
InitWithThisClass(InitWithThisClass x) {
|
||
|
s = x.s;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|