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

/*
* 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.nullsafe_gradual;
import android.annotation.SuppressLint;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Add model of java.lang.Class.getResource Summary: public It is possible to return null according to http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String). Also, getResource throws NPE if passed null: $ cat -n TestClassGetResourceArgument.java 1 import java.net.URL; 2 3 public class TestClassGetResourceArgument { 4 5 static URL testClassGetResourceArgument(Class cls) { 6 return cls.getResource(null); 7 } 8 9 public static void main(String[] args) { 10 System.out.println(testClassGetResourceArgument("".getClass()).toString()); 11 } 12 13 } $ javac TestClassGetResourceArgument.java && java TestClassGetResourceArgument Exception in thread "main" java.lang.NullPointerException at sun.misc.MetaIndex.mayContain(MetaIndex.java:243) at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:830) at sun.misc.URLClassPath.getResource(URLClassPath.java:199) at sun.misc.URLClassPath.getResource(URLClassPath.java:251) at java.lang.ClassLoader.getBootstrapResource(ClassLoader.java:1305) at java.lang.ClassLoader.getResource(ClassLoader.java:1144) at java.lang.ClassLoader.getResource(ClassLoader.java:1142) at java.lang.ClassLoader.getSystemResource(ClassLoader.java:1267) at java.lang.Class.getResource(Class.java:2145) at TestClassGetResourceArgument.testClassGetResourceArgument(TestClassGetResourceArgument.java:6) at TestClassGetResourceArgument.main(TestClassGetResourceArgument.java:10) Reviewed By: cristianoc Differential Revision: D2752301 fb-gh-sync-id: 888baf1
9 years ago
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.Nullable;
public class ParameterNotNullable {
boolean field = false;
ParameterNotNullable() {
testPrimitive(field);
}
void testPrimitive(boolean f) {}
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);
}
private ParameterNotNullable(@Nullable String s) {}
class Builder {
ParameterNotNullable getEradicateParameterNotNullable() {
return new ParameterNotNullable(null);
}
}
public @Nullable String testSystemGetPropertyArgument() {
String s = System.getProperty(null);
return s;
}
@Nullable
String testSystemGetenvBad() {
return System.getenv(null);
}
Add model of java.lang.Class.getResource Summary: public It is possible to return null according to http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String). Also, getResource throws NPE if passed null: $ cat -n TestClassGetResourceArgument.java 1 import java.net.URL; 2 3 public class TestClassGetResourceArgument { 4 5 static URL testClassGetResourceArgument(Class cls) { 6 return cls.getResource(null); 7 } 8 9 public static void main(String[] args) { 10 System.out.println(testClassGetResourceArgument("".getClass()).toString()); 11 } 12 13 } $ javac TestClassGetResourceArgument.java && java TestClassGetResourceArgument Exception in thread "main" java.lang.NullPointerException at sun.misc.MetaIndex.mayContain(MetaIndex.java:243) at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:830) at sun.misc.URLClassPath.getResource(URLClassPath.java:199) at sun.misc.URLClassPath.getResource(URLClassPath.java:251) at java.lang.ClassLoader.getBootstrapResource(ClassLoader.java:1305) at java.lang.ClassLoader.getResource(ClassLoader.java:1144) at java.lang.ClassLoader.getResource(ClassLoader.java:1142) at java.lang.ClassLoader.getSystemResource(ClassLoader.java:1267) at java.lang.Class.getResource(Class.java:2145) at TestClassGetResourceArgument.testClassGetResourceArgument(TestClassGetResourceArgument.java:6) at TestClassGetResourceArgument.main(TestClassGetResourceArgument.java:10) Reviewed By: cristianoc Differential Revision: D2752301 fb-gh-sync-id: 888baf1
9 years ago
static @Nullable URL testClassGetResourceArgument(Class cls) {
return cls.getResource(null);
}
void threeParameters(String s1, String s2, String s3) {}
void testThreeParameters() {
String s = "";
threeParameters(null, s, s);
threeParameters(s, null, s);
threeParameters(s, s, null);
}
class ConstructorCall {
ConstructorCall(int x, String s) {}
ConstructorCall() {
this(3, ""); // OK
}
ConstructorCall(int x) {
this(3, null); // NPE
}
}
void indirectSignatureLookupOk(SomeClass c) {
c.acceptsNullableParameter(null);
}
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);
}
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);
}
void testParsingNullStringToNumber() {
String ns = null;
long l = Long.parseLong(ns);
int i = Integer.parseInt(ns);
}
}
interface SomeInterface {
void acceptsNullableParameter(@Nullable Object object);
}
abstract class SomeClass implements SomeInterface {}