Generate not-null assertions on method parameters

Intrinsics.checkParameterIsNotNull() gets its caller's class and method names
from the stack trace to render them in an exception message.

Fix codegen tests because now it's now allowed to pass null to non-null
argument in tests
This commit is contained in:
Alexander Udalov
2012-10-04 15:46:25 +04:00
parent 753ae9e550
commit c9984c3d06
10 changed files with 108 additions and 9 deletions
+18 -1
View File
@@ -51,6 +51,23 @@ public class Intrinsics {
}
}
public static void checkParameterIsNotNull(Object value, String paramName) {
if (value == null) {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
// #0 is Thread.getStackTrace(), #1 is Intrinsics.checkParameterIsNotNull, #2 is our caller
StackTraceElement caller = stackTraceElements[2];
String className = caller.getClassName();
String methodName = caller.getMethodName();
IllegalArgumentException exception =
new IllegalArgumentException("Parameter specified as non-null contains null: " +
"method " + className + "." + methodName +
", parameter " + paramName);
throw sanitizeStackTrace(exception);
}
}
public static <T> Class<T> getJavaClass(T self) {
return (Class<T>) self.getClass();
}
@@ -74,7 +91,7 @@ public class Intrinsics {
}
private static final Set<String> METHOD_NAMES_TO_SKIP = new HashSet<String>(Arrays.asList(
"throwNpe", "checkReturnedValueIsNotNull", "checkFieldIsNotNull"
"throwNpe", "checkReturnedValueIsNotNull", "checkFieldIsNotNull", "checkParameterIsNotNull"
));
private static <T extends Throwable> T sanitizeStackTrace(T throwable) {