@@ -183,7 +183,7 @@ class JDIEval(
183
183
184
184
override fun getStaticField (fieldDesc : FieldDescription ): Value {
185
185
val field = findStaticField(fieldDesc)
186
- return mayThrow { field.declaringType().getValue(field) }.asValue()
186
+ return mayThrow { field.declaringType().getValue(field) }.ifFail(field). asValue()
187
187
}
188
188
189
189
override fun setStaticField (fieldDesc : FieldDescription , newValue : Value ) {
@@ -199,7 +199,7 @@ class JDIEval(
199
199
}
200
200
201
201
val jdiValue = newValue.asJdiValue(vm, field.type().asType())
202
- mayThrow { _class .setValue(field, jdiValue) }
202
+ mayThrow { _class .setValue(field, jdiValue) }.ifFail(field)
203
203
}
204
204
205
205
private fun findMethod (methodDesc : MethodDescription , _class : ReferenceType = methodDesc.ownerType.asReferenceType()): Method {
@@ -231,7 +231,7 @@ class JDIEval(
231
231
}
232
232
233
233
args.disableCollection()
234
- val result = mayThrow { _class .invokeMethod(thread, method, args, invokePolicy) }
234
+ val result = mayThrow { _class .invokeMethod(thread, method, args, invokePolicy) }.ifFail(method)
235
235
args.enableCollection()
236
236
return result.asValue()
237
237
}
@@ -240,7 +240,7 @@ class JDIEval(
240
240
val field = findField(fieldDesc)
241
241
val obj = instance.jdiObj.checkNull()
242
242
243
- return mayThrow { obj.getValue(field) }.asValue()
243
+ return mayThrow { obj.getValue(field) }.ifFail(field, obj). asValue()
244
244
}
245
245
246
246
override fun setField (instance : Value , fieldDesc : FieldDescription , newValue : Value ) {
@@ -288,7 +288,7 @@ class JDIEval(
288
288
val _class = (instance as NewObjectValue ).asmType.asReferenceType() as ClassType
289
289
val args = mapArguments(arguments, ctor.safeArgumentTypes())
290
290
args.disableCollection()
291
- val result = mayThrow { _class .newInstance(thread, ctor, args, invokePolicy) }
291
+ val result = mayThrow { _class .newInstance(thread, ctor, args, invokePolicy) }.ifFail(ctor)
292
292
args.enableCollection()
293
293
instance.value = result
294
294
return result.asValue()
@@ -302,7 +302,7 @@ class JDIEval(
302
302
}
303
303
304
304
args.disableCollection()
305
- val result = mayThrow { obj.invokeMethod(thread, method, args, policy) }
305
+ val result = mayThrow { obj.invokeMethod(thread, method, args, policy) }.ifFail(method, obj)
306
306
args.enableCollection()
307
307
return result.asValue()
308
308
}
@@ -403,11 +403,41 @@ class JDIEval(
403
403
}
404
404
}
405
405
406
- fun <T > mayThrow (f : () -> T ): T {
406
+ private sealed class JdiOperationResult <T > {
407
+ class Fail <T >(val cause : Exception ): JdiOperationResult<T>()
408
+ class OK <T >(val value : T ): JdiOperationResult<T>()
409
+ }
410
+
411
+ private fun <T > mayThrow (f : () -> T ): JdiOperationResult <T > {
407
412
try {
408
- return f()
413
+ return JdiOperationResult .OK (f())
414
+ }
415
+ catch (e: IllegalArgumentException ) {
416
+ return JdiOperationResult .Fail <T >(e)
409
417
}
410
418
catch (e: InvocationException ) {
411
419
throw ThrownFromEvaluatedCodeException (e.exception().asValue())
412
420
}
421
+ }
422
+
423
+ private fun memberInfo (member : TypeComponent , thisObj : ObjectReference ? ): String {
424
+ return " \n member = $member \n objectRef = $thisObj "
425
+ }
426
+
427
+ private fun <T > JdiOperationResult<T>.ifFail (member : TypeComponent , thisObj : ObjectReference ? = null): T {
428
+ return ifFail { memberInfo(member, thisObj) }
429
+ }
430
+
431
+ private fun <T > JdiOperationResult<T>.ifFail (lazyMessage : () -> String ): T {
432
+ return when (this ) {
433
+ is JdiOperationResult .OK -> this .value
434
+ is JdiOperationResult .Fail -> {
435
+ if (cause is IllegalArgumentException ) {
436
+ throwBrokenCodeException(IllegalArgumentException (lazyMessage(), this .cause))
437
+ }
438
+ else {
439
+ throwBrokenCodeException(IllegalStateException (lazyMessage(), this .cause))
440
+ }
441
+ }
442
+ }
413
443
}
0 commit comments