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
master
Josh Berdine 9 years ago committed by facebook-github-bot-5
parent 01aab39692
commit b729c93dd5

@ -140,6 +140,7 @@ let annotated_list_nullable =
o1, "com.google.common.util.concurrent.SettableFuture.setException(java.lang.Throwable):boolean"; o1, "com.google.common.util.concurrent.SettableFuture.setException(java.lang.Throwable):boolean";
o1, "java.io.File.<init>(java.lang.String)"; o1, "java.io.File.<init>(java.lang.String)";
n1, "java.io.PrintStream.print(java.lang.String):void"; n1, "java.io.PrintStream.print(java.lang.String):void";
(n, [o]), "java.lang.Class.getResource(java.lang.String):java.net.URL";
o1, "java.lang.Class.isAssignableFrom(java.lang.Class):boolean"; o1, "java.lang.Class.isAssignableFrom(java.lang.Class):boolean";
n1, "java.lang.Integer.equals(java.lang.Object):boolean"; n1, "java.lang.Integer.equals(java.lang.Object):boolean";
n2, "java.lang.RuntimeException.<init>(java.lang.String,java.lang.Throwable)"; n2, "java.lang.RuntimeException.<init>(java.lang.String,java.lang.Throwable)";

@ -10,6 +10,7 @@
package codetoanalyze.java.eradicate; package codetoanalyze.java.eradicate;
import java.lang.System; import java.lang.System;
import java.net.URL;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
@ -71,4 +72,8 @@ public class ParameterNotNullable {
return s; return s;
} }
static @Nullable URL testClassGetResourceArgument(Class cls) {
return cls.getResource(null);
}
} }

@ -11,6 +11,7 @@ package codetoanalyze.java.eradicate;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import java.net.URL;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
@ -115,4 +116,9 @@ public class ReturnNotNullable {
return null; return null;
} }
} }
URL getResourceNullable(Class cls, String name) {
return cls.getResource(name);
}
} }

@ -43,6 +43,7 @@ public class ParameterNotNullableTest {
"callNull", "callNull",
"callNullable", "callNullable",
"testSystemGetPropertyArgument", "testSystemGetPropertyArgument",
"testClassGetResourceArgument",
}; };
assertThat( assertThat(
"Results should contain " + PARAMETER_NOT_NULLABLE, "Results should contain " + PARAMETER_NOT_NULLABLE,

@ -58,6 +58,7 @@ public class ReturnNotNullableTest {
"returnNullable", "returnNullable",
"return_null_in_catch", "return_null_in_catch",
"return_null_in_catch_after_throw", "return_null_in_catch_after_throw",
"getResourceNullable",
}; };
List<ErrorPattern> errorPatterns = createPatterns( List<ErrorPattern> errorPatterns = createPatterns(
RETURN_NOT_NULLABLE, RETURN_NOT_NULLABLE,

Loading…
Cancel
Save