8
8
package com .facebook .react .devsupport ;
9
9
10
10
import androidx .annotation .Nullable ;
11
+ import com .facebook .infer .annotation .Assertions ;
12
+ import com .facebook .infer .annotation .Nullsafe ;
11
13
import com .facebook .react .bridge .JavaOnlyArray ;
12
14
import com .facebook .react .bridge .JavaOnlyMap ;
13
15
import com .facebook .react .bridge .ReadableArray ;
26
28
import org .json .JSONObject ;
27
29
28
30
/** Helper class converting JS and Java stack traces into arrays of {@link StackFrame} objects. */
31
+ @ Nullsafe (Nullsafe .Mode .LOCAL )
29
32
public class StackTraceHelper {
30
33
31
34
public static final String COLUMN_KEY = "column" ;
@@ -49,27 +52,29 @@ public class StackTraceHelper {
49
52
50
53
/** Represents a generic entry in a stack trace, be it originally from JS or Java. */
51
54
public static class StackFrameImpl implements StackFrame {
52
- private final String mFile ;
55
+ @ Nullable private final String mFile ;
53
56
private final String mMethod ;
54
57
private final int mLine ;
55
58
private final int mColumn ;
56
- private final String mFileName ;
59
+ @ Nullable private final String mFileName ;
57
60
private final boolean mIsCollapsed ;
58
61
59
- private StackFrameImpl (String file , String method , int line , int column , boolean isCollapsed ) {
62
+ private StackFrameImpl (
63
+ @ Nullable String file , String method , int line , int column , boolean isCollapsed ) {
60
64
mFile = file ;
61
65
mMethod = method ;
62
66
mLine = line ;
63
67
mColumn = column ;
64
- mFileName = file != null ? new File (file ).getName () : "" ;
68
+ mFileName = file != null ? new File (file ).getName () : null ;
65
69
mIsCollapsed = isCollapsed ;
66
70
}
67
71
68
- private StackFrameImpl (String file , String method , int line , int column ) {
72
+ private StackFrameImpl (@ Nullable String file , String method , int line , int column ) {
69
73
this (file , method , line , column , false );
70
74
}
71
75
72
- private StackFrameImpl (String file , String fileName , String method , int line , int column ) {
76
+ private StackFrameImpl (
77
+ @ Nullable String file , @ Nullable String fileName , String method , int line , int column ) {
73
78
mFile = file ;
74
79
mFileName = fileName ;
75
80
mMethod = method ;
@@ -84,7 +89,8 @@ private StackFrameImpl(String file, String fileName, String method, int line, in
84
89
* <p>JS traces return the full path to the file here, while Java traces only return the file
85
90
* name (the path is not known).
86
91
*/
87
- public String getFile () {
92
+ @ Override
93
+ public @ Nullable String getFile () {
88
94
return mFile ;
89
95
}
90
96
@@ -109,7 +115,8 @@ public int getColumn() {
109
115
* <p>For JS traces this is different from {@link #getFile()} in that it only returns the file
110
116
* name, not the full path. For Java traces there is no difference.
111
117
*/
112
- public String getFileName () {
118
+ @ Override
119
+ public @ Nullable String getFileName () {
113
120
return mFileName ;
114
121
}
115
122
@@ -119,9 +126,10 @@ public boolean isCollapsed() {
119
126
120
127
/** Convert the stack frame to a JSON representation. */
121
128
public JSONObject toJSON () {
129
+ String file = getFile ();
122
130
return new JSONObject (
123
131
MapBuilder .of (
124
- "file" , getFile () ,
132
+ "file" , ( file == null ) ? "" : file ,
125
133
"methodName" , getMethod (),
126
134
"lineNumber" , getLine (),
127
135
"column" , getColumn (),
@@ -136,36 +144,33 @@ public JSONObject toJSON() {
136
144
public static StackFrame [] convertJsStackTrace (@ Nullable ReadableArray stack ) {
137
145
int size = stack != null ? stack .size () : 0 ;
138
146
StackFrame [] result = new StackFrame [size ];
139
- for (int i = 0 ; i < size ; i ++) {
140
- // NULLSAFE_FIXME[Nullable Dereference]
141
- ReadableType type = stack .getType (i );
142
- if (type == ReadableType .Map ) {
143
- // NULLSAFE_FIXME[Nullable Dereference]
144
- ReadableMap frame = stack .getMap (i );
145
- // NULLSAFE_FIXME[Nullable Dereference]
146
- String methodName = frame .getString ("methodName" );
147
- // NULLSAFE_FIXME[Nullable Dereference]
148
- String fileName = frame .getString ("file" );
149
- boolean collapse =
150
- // NULLSAFE_FIXME[Nullable Dereference]
151
- frame .hasKey ("collapse" ) && !frame .isNull ("collapse" ) && frame .getBoolean ("collapse" );
152
- int lineNumber = -1 ;
153
- // NULLSAFE_FIXME[Nullable Dereference]
154
- if (frame .hasKey (LINE_NUMBER_KEY ) && !frame .isNull (LINE_NUMBER_KEY )) {
155
- // NULLSAFE_FIXME[Nullable Dereference]
156
- lineNumber = frame .getInt (LINE_NUMBER_KEY );
157
- }
158
- int columnNumber = -1 ;
159
- // NULLSAFE_FIXME[Nullable Dereference]
160
- if (frame .hasKey (COLUMN_KEY ) && !frame .isNull (COLUMN_KEY )) {
161
- // NULLSAFE_FIXME[Nullable Dereference]
162
- columnNumber = frame .getInt (COLUMN_KEY );
147
+ if (stack != null ) {
148
+ for (int i = 0 ; i < size ; i ++) {
149
+ ReadableType type = stack .getType (i );
150
+ if (type == ReadableType .Map ) {
151
+ ReadableMap frame = stack .getMap (i );
152
+ Assertions .assertNotNull (frame );
153
+ String methodName = frame .getString ("methodName" );
154
+ String fileName = frame .getString ("file" );
155
+ Assertions .assertNotNull (fileName );
156
+ Assertions .assertNotNull (methodName );
157
+ boolean collapse =
158
+ frame .hasKey ("collapse" ) && !frame .isNull ("collapse" ) && frame .getBoolean ("collapse" );
159
+ int lineNumber = -1 ;
160
+ if (frame .hasKey (LINE_NUMBER_KEY ) && !frame .isNull (LINE_NUMBER_KEY )) {
161
+ lineNumber = frame .getInt (LINE_NUMBER_KEY );
162
+ }
163
+ int columnNumber = -1 ;
164
+ if (frame .hasKey (COLUMN_KEY ) && !frame .isNull (COLUMN_KEY )) {
165
+ columnNumber = frame .getInt (COLUMN_KEY );
166
+ }
167
+ result [i ] = new StackFrameImpl (fileName , methodName , lineNumber , columnNumber , collapse );
168
+ } else if (type == ReadableType .String ) {
169
+ String stackFrame = stack .getString (i );
170
+ if (stackFrame != null ) {
171
+ result [i ] = new StackFrameImpl (null , stackFrame , -1 , -1 );
172
+ }
163
173
}
164
- // NULLSAFE_FIXME[Parameter Not Nullable]
165
- result [i ] = new StackFrameImpl (fileName , methodName , lineNumber , columnNumber , collapse );
166
- } else if (type == ReadableType .String ) {
167
- // NULLSAFE_FIXME[Parameter Not Nullable, Nullable Dereference]
168
- result [i ] = new StackFrameImpl (null , stack .getString (i ), -1 , -1 );
169
174
}
170
175
}
171
176
return result ;
@@ -214,19 +219,22 @@ public static StackFrame[] convertJsStackTrace(String stack) {
214
219
} else if (matcher1 .find ()) {
215
220
matcher = matcher1 ;
216
221
} else {
217
- // NULLSAFE_FIXME[Parameter Not Nullable]
218
- result [i ] = new StackFrameImpl (null , stackTrace [i ], -1 , -1 );
222
+ String unmatchedStackFrame = stackTrace [i ];
223
+ if (unmatchedStackFrame != null ) {
224
+ result [i ] = new StackFrameImpl (null , unmatchedStackFrame , -1 , -1 );
225
+ }
226
+ continue ;
227
+ }
228
+ String fileName = matcher .group (2 );
229
+ String methodName = matcher .group (1 ) == null ? "(unknown)" : matcher .group (1 );
230
+ String lineString = matcher .group (3 );
231
+ String columnString = matcher .group (4 );
232
+ if (methodName == null || fileName == null || lineString == null || columnString == null ) {
219
233
continue ;
220
234
}
221
235
result [i ] =
222
236
new StackFrameImpl (
223
- // NULLSAFE_FIXME[Parameter Not Nullable]
224
- matcher .group (2 ),
225
- matcher .group (1 ) == null ? "(unknown)" : matcher .group (1 ),
226
- // NULLSAFE_FIXME[Parameter Not Nullable]
227
- Integer .parseInt (matcher .group (3 )),
228
- // NULLSAFE_FIXME[Parameter Not Nullable]
229
- Integer .parseInt (matcher .group (4 )));
237
+ fileName , methodName , Integer .parseInt (lineString ), Integer .parseInt (columnString ));
230
238
}
231
239
return result ;
232
240
}
@@ -239,7 +247,6 @@ public static StackFrame[] convertJavaStackTrace(Throwable exception) {
239
247
result [i ] =
240
248
new StackFrameImpl (
241
249
stackTrace [i ].getClassName (),
242
- // NULLSAFE_FIXME[Parameter Not Nullable]
243
250
stackTrace [i ].getFileName (),
244
251
stackTrace [i ].getMethodName (),
245
252
stackTrace [i ].getLineNumber (),
0 commit comments