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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user