Improve stack trace analysis logic in parameter null check intrinsic
This doesn't change behavior for the default JVM, so no new tests are added. The current behavior is already tested in `javaInterop/notNullAssertions/paramAssertionMessage.kt`. But this will improve exception message on Android, where there are 2 platform frames in the stack trace instead of 1. #KT-50083 Fixed
This commit is contained in:
@@ -143,12 +143,14 @@ public class Intrinsics {
|
|||||||
private static String createParameterIsNullExceptionMessage(String paramName) {
|
private static String createParameterIsNullExceptionMessage(String paramName) {
|
||||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||||
|
|
||||||
// #0 Thread.getStackTrace()
|
String thisClassName = Intrinsics.class.getName();
|
||||||
// #1 Intrinsics.createParameterIsNullExceptionMessage
|
int i = 0;
|
||||||
// #2 Intrinsics.throwParameterIsNullIAE/throwParameterIsNullNPE
|
// Skip platform frames such as Thread.getStackTrace.
|
||||||
// #3 Intrinsics.checkParameterIsNotNull/checkNotNullParameter
|
while (!stackTraceElements[i].getClassName().equals(thisClassName)) i++;
|
||||||
// #4 our caller
|
// Skip all frames of this class such as createParameterIsNullExceptionMessage, throwParameterIsNullNPE, checkNotNullParameter.
|
||||||
StackTraceElement caller = stackTraceElements[4];
|
while (stackTraceElements[i].getClassName().equals(thisClassName)) i++;
|
||||||
|
// This frame is our caller.
|
||||||
|
StackTraceElement caller = stackTraceElements[i];
|
||||||
String className = caller.getClassName();
|
String className = caller.getClassName();
|
||||||
String methodName = caller.getMethodName();
|
String methodName = caller.getMethodName();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user