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.
196 lines
4.3 KiB
196 lines
4.3 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
|
|
||
6 years ago
|
import android.annotation.SuppressLint;
|
||
6 years ago
|
import com.google.common.collect.ImmutableList;
|
||
|
import com.google.common.collect.ImmutableMap;
|
||
|
import com.google.common.collect.ImmutableSet;
|
||
9 years ago
|
import java.net.URL;
|
||
6 years ago
|
import java.util.Collection;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
10 years ago
|
import javax.annotation.Nullable;
|
||
|
|
||
|
public class ParameterNotNullable {
|
||
|
|
||
|
boolean field = false;
|
||
|
|
||
|
ParameterNotNullable() {
|
||
|
testPrimitive(field);
|
||
|
}
|
||
|
|
||
6 years ago
|
void testPrimitive(boolean f) {}
|
||
10 years ago
|
|
||
|
void test(String s) {
|
||
|
int n = s.length();
|
||
|
}
|
||
|
|
||
|
void testN(@Nullable String s) {
|
||
|
int n = s != null ? s.length() : 0;
|
||
|
}
|
||
|
|
||
|
void callNull() {
|
||
|
String s = null;
|
||
|
test(s);
|
||
|
}
|
||
|
|
||
|
@SuppressLint("ERADICATE_PARAMETER_NOT_NULLABLE")
|
||
|
void callNullSuppressed() {
|
||
|
String s = null;
|
||
|
test(s);
|
||
|
}
|
||
|
|
||
|
void callNullable(@Nullable String s) {
|
||
|
test(s);
|
||
|
}
|
||
|
|
||
|
void callNullOK() {
|
||
|
String s = null;
|
||
|
testN(s);
|
||
|
}
|
||
|
|
||
|
void callNullableOK(@Nullable String s) {
|
||
|
testN(s);
|
||
|
}
|
||
|
|
||
6 years ago
|
private ParameterNotNullable(@Nullable String s) {}
|
||
10 years ago
|
|
||
|
class Builder {
|
||
|
ParameterNotNullable getEradicateParameterNotNullable() {
|
||
|
return new ParameterNotNullable(null);
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
public @Nullable String testSystemGetPropertyArgument() {
|
||
|
String s = System.getProperty(null);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
6 years ago
|
@Nullable
|
||
|
String testSystemGetenvBad() {
|
||
8 years ago
|
return System.getenv(null);
|
||
|
}
|
||
|
|
||
9 years ago
|
static @Nullable URL testClassGetResourceArgument(Class cls) {
|
||
|
return cls.getResource(null);
|
||
|
}
|
||
|
|
||
6 years ago
|
void threeParameters(String s1, String s2, String s3) {}
|
||
8 years ago
|
|
||
|
void testThreeParameters() {
|
||
|
String s = "";
|
||
|
threeParameters(null, s, s);
|
||
|
threeParameters(s, null, s);
|
||
|
threeParameters(s, s, null);
|
||
|
}
|
||
8 years ago
|
|
||
|
class ConstructorCall {
|
||
6 years ago
|
ConstructorCall(int x, String s) {}
|
||
8 years ago
|
|
||
|
ConstructorCall() {
|
||
|
this(3, ""); // OK
|
||
|
}
|
||
|
|
||
|
ConstructorCall(int x) {
|
||
|
this(3, null); // NPE
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
void indirectSignatureLookupOk(SomeClass c) {
|
||
6 years ago
|
c.acceptsNullableParameter(null);
|
||
|
}
|
||
6 years ago
|
|
||
|
void doesNotAcceptNullableFirstParameter(Object object, boolean test) {}
|
||
|
|
||
|
void callWithNullableFirstParameter(boolean t1, boolean t2) {
|
||
|
doesNotAcceptNullableFirstParameter(null, t1 && t2);
|
||
|
}
|
||
|
|
||
|
void callWithConditionalAssignment(Object object, boolean test) {
|
||
|
doesNotAcceptNullableFirstParameter(test ? object : null, test);
|
||
|
}
|
||
6 years ago
|
|
||
|
void testImmutableListOfnotNullArguments() {
|
||
|
|
||
|
Object notNull = new Object();
|
||
|
|
||
|
ImmutableList.of(null);
|
||
|
ImmutableList.of(null, null);
|
||
|
ImmutableList.of(notNull, notNull);
|
||
|
ImmutableList.of(notNull, null);
|
||
|
}
|
||
|
|
||
|
void testImmutableListCopyOfNotNullArguments() {
|
||
|
|
||
|
Iterable nullIterable = null;
|
||
|
Iterator nullIterator = null;
|
||
|
Collection nullCollection = null;
|
||
|
|
||
|
ImmutableList.copyOf(nullIterable);
|
||
|
ImmutableList.copyOf(nullIterator);
|
||
|
ImmutableList.copyOf(nullCollection);
|
||
|
}
|
||
|
|
||
|
void testImmutableListSortedCopyOfNotNullArguments() {
|
||
|
ImmutableList.sortedCopyOf(null, null);
|
||
|
}
|
||
|
|
||
|
void testImmutableSetOfnotNullArguments() {
|
||
|
|
||
|
Object notNull = new Object();
|
||
|
|
||
|
ImmutableSet.of(null);
|
||
|
ImmutableSet.of(null, null);
|
||
|
ImmutableSet.of(notNull, notNull);
|
||
|
ImmutableSet.of(notNull, null);
|
||
|
ImmutableSet.of(notNull, null, notNull, null, notNull);
|
||
|
}
|
||
|
|
||
|
void testImmutableSetCopyOfNotNullArguments() {
|
||
|
|
||
|
Iterable nullIterable = null;
|
||
|
Iterator nullIterator = null;
|
||
|
Collection nullCollection = null;
|
||
|
|
||
|
ImmutableSet.copyOf(nullIterable);
|
||
|
ImmutableSet.copyOf(nullIterator);
|
||
|
ImmutableSet.copyOf(nullCollection);
|
||
|
}
|
||
|
|
||
|
void testImmutableMapOfnotNullArguments() {
|
||
|
|
||
|
Object notNull = new Object();
|
||
|
|
||
|
ImmutableMap.of(null, null);
|
||
|
ImmutableMap.of(notNull, notNull);
|
||
|
ImmutableMap.of(notNull, null, notNull, null);
|
||
|
}
|
||
|
|
||
|
void testImmutableMapCopyOfNotNullArguments() {
|
||
|
|
||
|
Iterable nullIterable = null;
|
||
|
Map nullMap = null;
|
||
|
|
||
|
ImmutableMap.copyOf(nullIterable);
|
||
|
ImmutableMap.copyOf(nullMap);
|
||
|
}
|
||
6 years ago
|
|
||
|
void testParsingNullStringToNumber() {
|
||
|
String ns = null;
|
||
|
long l = Long.parseLong(ns);
|
||
|
int i = Integer.parseInt(ns);
|
||
|
}
|
||
10 years ago
|
}
|
||
6 years ago
|
|
||
|
interface SomeInterface {
|
||
|
void acceptsNullableParameter(@Nullable Object object);
|
||
|
}
|
||
|
|
||
|
abstract class SomeClass implements SomeInterface {}
|