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.

119 lines
2.4 KiB

/*
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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.eradicate;
import android.annotation.SuppressLint;
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 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);
}
}
interface SomeInterface {
void acceptsNullableParameter(@Nullable Object object);
}
abstract class SomeClass implements SomeInterface {}