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.

80 lines
1.5 KiB

/*
* Copyright (c) 2013 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package codetoanalyze.java.eradicate;
import java.lang.System;
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;
import android.annotation.SuppressLint;
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;
}
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);
}
}